Fast fix diverged branches

You made a pull-request, but now some other changes got merged and branches have diverged. There’s a small tip I use to keep history while re-applying commits.

Make sure to also check out my Git Rebase guide.

You’re on a patch branch, and the history looks like this

commit 1e227021e414f25b664a4388142f1db580df7649
Author: Foo
Date:   Wed Jun 14 14:10:20 2016 +0200

    Foo made something

commit b6a7c7532e783d853233cebdaccf657aecffac12
Author: Owner
Date:   Wed Jun 12 19:17:17 2016 +0200

    Merge pull request #12 from some/project

    Foo made something

commit 3bf53233d053463931124bae5b81490733c1fb4a
Author: You
Date:   Tue Jun 24 18:14:29 2016 +0200

    I fixed this blabla

Now master changed, which you should see after sending git fetch:

   b6a7c753..2607346  master     -> origin/master

I usually try to keep one commit if I send a pull request, and instead of merging/rebasing I replay my commit by using stash.

For example, first reset to the same commit (origin/master) you based your patch branch (me/mybranch):

git checkout mybranch
# copy your commit message (%s is subject only)
git --no-pager log -n 1 --pretty=format:%s | pbcopy # pipe to `xclip -selection clipboard` on ubuntu, you can also save this to a file or whatever
# reset your commit
git reset HEAD~1
git stash
# now you can merge with no issues
git merge origin/master
git stash apply
# resolve conflicts if any, then commit again
git add what you want to add
git commit -m $(pbpaste) # or `xclip -selection clipboard -o`

Now git log would look like this:

commit 1e227021e414f25b664a4388142f1db580df7649
Author: Foo
Date:   Wed Jun 14 14:10:20 2016 +0200

    Foo made something

commit b6a7c7532e783d853233cebdaccf657aecffac12
Author: Owner
Date:   Wed Jun 12 19:17:17 2016 +0200

    Merge pull request #12 from some/project

    Foo made something

commit 2607346428f93267a58ad439ad2dab86a0f6ec88

Author: Owner
Date:   Wed Jun 12 23:10:41 2016 +0200

    Owner fixed something else

commit 3bf53233d053463931124bae5b81490733c1fb4a
Author: You
Date:   Tue Jun 24 18:14:29 2016 +0200

    I fixed this blabla

You might need to force push the branch if you already pushed the commits: git push -fu origin mybranch.