Pages

Friday, September 12, 2014

Simple git rebase example

Situation:

So I forked the Google Kubernetes project.  Then I created a fedora_gs_guide branch.  I made some changes to the getting started guide and then I submitted a pull request.  I asked someone to review it and they had a couple of changes.  So, I make the changes, and commit.  Now when I look at the PR, I see multiple commits.  I think it's best practice to squash all those commits into one if possible.  So, how do you do that.  Well, here's how I did it.  I'm sure there are other ways to do this, probably a lot more efficiently (comments welcome).  But, it worked.  These are my notes from the process.

I needed to make sure my master and fedora_gs_guide branch were clean and rebased to upstream master.

Make sure I'm on my local master.

$ git checkout master



Add a remote branch:

$ git remote add upstream https://github.com/GoogleCloudPlatform/kubernetes.git

Then fetch all updates from the 'upstream' branch:

$ git fetch upstream master

Since my master was behind the upstream master, I needed to update my copy on github:

$ git push origin master

Now my master is clean.  So now I need to make sure my feature branch is caught up with master:

$ git checkout fedora_gs_guide
$ git merge master

I push my current feature branch up to github to bring the remote branch up to date:

$ git push origin fedora_gs_guide

Now that I'm all merged and clean up, I can start the rebase.  From what I understand, it's best to separate those two steps. So, I do an interactive rebase in order to squash the commits that I want.  When you go into interactive mode like this, it will show you all the commits that are in that feature branch that are available for squashing.  In order to squash a commit into the previous commit, you just replace the word "pick", with "squash" at the start of the line.

$ git rebase -i upstream/master

After squashing the commits, I need to push the new squashed commit to my feature branch.

$ git push -ff origin fedora_gs_guide

Now when I check the PR on github, I only see the one commit.  Which is what I intended.

No comments:

Post a Comment