Howto: Rebase your feature branch

There are several reasons to do a rebase of your feature branch (for a pull request etc.) against master.

  1. The master has drifted so far away so it cannot be merged automatically (makes it easier for the receiver of the pull request).
  2. The master has code that you need.

In Hugo right now master contains Windows fixes needed to get the build go green.

So with my pull request Angled quotes, here is what I did:

Note, here is my remote setup (this is my preferred setup; use git remote … to get there):

bjornerik    git@github.com:bjornerik/hugo.git 
origin    git@github.com:spf13/hugo.git 

So I start by getting the latest from spf13:

git checkout master
git pull
git checkout feature/quotes-of-angles
git rebase master

At this point either all is OK or you have some merge conflict.

If conflict edit the file(s) in conflict manually or use a conflict solver tool. In my case it was some messed up imports in page.go.

Resolve conflict by:

git add .
git rebase --continue

Then force push to GitHub (force pushing is usually a bad thing, but in this case just the right medicine)

git push -f  bjornerik feature/quotes-of-angles

And viola, my PR goes green:

With Git there are several ways to Rome, but the above works for me.

3 Likes

A related tip if you do a lot of forking and pull requests on GitHub:

Great “git wrapper”.

1 Like

A lengthy answer I gave in another context about the same topic:

My normal process for pull requests:

git checkout -b mybranch
echo changed1 >> README.md
git commit -a -m 'Change 1'
echo changed2 >> README.md
git commit -a -m 'Change 2'
git diff master
git log # shows 2 commits
git rebase -i master
=> change pick to f (fix) for all but the first commit

Then I often do a

git commit --amend

To fixup the commit message

Then I push it to GitHub and create a PR.

git push bep mybranch

If I need to change it later, I make the changes on that branch then

git commit -a --amend
git push -f bep mybranch

Note the -f

For long living PRs I often need to rebase it against master:

git rebase master
# fix any issues, then:
git add .
git push bep mybranch

2 Likes