Skip to content

Instantly share code, notes, and snippets.

@toff63
Last active October 24, 2016 14:06
Show Gist options
  • Save toff63/fe1dfce52158a4b58a25134bc9f44be9 to your computer and use it in GitHub Desktop.
Save toff63/fe1dfce52158a4b58a25134bc9f44be9 to your computer and use it in GitHub Desktop.
Gives a way to push a clean commit history before sending a Pull Request

Get the difference between your branch and the one you are going to do the PR:

git log --oneline my_branch...master
# Should return something like
# 5bf92dc Merge remote-tracking branch 'origin/master' into my_branch
# 51a2689 Move some code in try/catch to avoid crashing daemon
# 481b548 Fix my_process status
# 6c0747b Fix linux service failing to stop properly and then start

Write those lines down in your favorite text editor for future use.

Retrieve the common ancestor between your branch and the one you want do a PR

git merge-base my_branch master
# for me it returned f1ec4a0f8a41e374d49939384561c33d5b5d0370

Create a branch from this commit

git checkout f1ec4a0f8a41e374d49939384561c33d5b5d0370 -b my-clean-branch

Add commits from my_branch to my-clean-branch. No need to add Merge commits.

git cherry-pick 6c0747b 481b548 51a2689

Now let's re-write the history using git rebase -i. As we only want to rewrite the history of our 3 commits, we will pass HEAD~3 as revision range.

git rebase -i HEAD~3 

It should a file with the list of commits (note the order is chronological: older commits first):

pick 6c0747b Fix linux service failing to stop properly and then start
pick 481b548 Fix my_process status
pick 51a2689 Move some code in try/catch to avoid crashing daemon

We want to merge the 2 first commits into 1 so we update it to this and save it:

pick 6c0747b Fix linux service failing to stop properly and then start
squash 481b548 Fix my_process status
pick 51a2689 Move some code in try/catch to avoid crashing daemon

It will start the rebase and open a file to give you the opportunity to update the commit message of your new commit.

If you now compare master with your clean branch you should see only 2 commits. You are ready to send you PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment