Just another Git Cheat Sheet

A running list of git tips and tricks.

Anurag Sinha
4 min readJan 16, 2018

Cherry Picking

The git cherry-pick command is used to pick a single commit or a range of commits (Git 1.7.2+) from one branch and apply it to another branch.

Here is how you can Cherry pick a range of commits from one branch (in a remote) and apply it to another branch (in the same or different remote):

  1. In case not already done add the remote from which the commits have to be cherry-picked and do a fetch to get the commit history.
git remote add <remote_name> <url>
git fetch <remote_name>
E.g.

$ git remote add origin https://github.com/sinhanurag/myrepo
$ git fetch originremote: Counting objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6
Unpacking objects: 100% (6/6), done.
From https://github.com/sinhanurag/myrepo
* [new branch] develop -> origin/develop
* [new branch] master -> origin/master
* [new branch] release -> origin/release

2. Obtain the commits you want to cherry pick from the remote branch.

git log <remote_name>/<branch_name>E.g.$ git log upstream/mastercommit fd7d0d7873461a264 (upstream/master)
Author: Author1 <author1@email.com>
Date: Fri Dec 29 10:17:55 2017 +0530
Terminal Commitcommit abba470a466776dbb
Author: Author1 <author1@email.com>
Date: Thu Dec 28 18:50:36 2017 +0530
Commit 3commit 42a9af7f08b36b21
Author: Author2 <author2@email.com>
Date: Thu Dec 28 18:12:29 2017 +0530
Commit 2commit 9beeb2f216d81f1d
Author: Author1 <author1@email.com>
Date: Thu Dec 28 17:29:37 2017 +0530
Commit 1commit 6e30dac520faef4a1
Author: Author3 <author3@email.com>
Date: Thu Dec 28 13:35:30 2017 +0530
Initial Commit

3. Assuming you want to cherry pick all commits between “Initial commit” . and the “Terminal Commit” use the following syntax. This will pick each commit between the 2 specified commits (excluding the range commits) and apply them to the current branch in your local.

git cherry-pick <initial_commit_hash>..<terminal_commit_hash>E.g.$ git cherry-pick 6e30dac520faef4a1..fd7d0d7873461a26On branch develop
You are currently cherry-picking commit d856905.

4. If you want to include the range commits the syntax for the cherry picking command is a little different.

git cherry-pick <initial_commit_hash>^..<terminal_commit_hash>

Please note that as each commit is applied to the current branch in case of any conflicts or a need of resolution git will pause the merging and ask you to resolve the issue for that particular commit.

You can resume the cherry-picking using the following command.

git cherry-pick --continue

Here is a list of all possible cherry-pick options

$ git cherry-pick
usage: git cherry-pick [<options>] <commit-ish>...
or: git cherry-pick <subcommand>
--quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
-n, --no-commit don't automatically commit
-e, --edit edit the commit message
-s, --signoff add Signed-off-by:
-m, --mainline <parent-number>
select mainline parent
--rerere-autoupdate update the index with reused conflict resolution if possible
--strategy <strategy>
merge strategy
-X, --strategy-option <option>
option for merge strategy
-S, --gpg-sign[=<key-id>]
GPG sign commit
-x append commit name
--ff allow fast-forward
--allow-empty preserve initially empty commits
--allow-empty-message
allow commits with empty messages
--keep-redundant-commits
keep redundant, empty commits

Squashing

Often there is a need to squash or club multiple commits into one in order to keep your commit history clean. Git allows you to modify the commit history.

Here is how you can use rebase to club last N commits into 1 using interactive rebase:

git rebase -i <branch_name>E.g $ git rebase -i masterpick 5477060 Test commit 1
pick a3d6192 Test commit 2
pick 67456dg Test commit 3
# Rebase b5081cc..a3d6192 onto b5081cc (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

The interactive prompt is pretty informative in how to go about modifying the commit history (or clubbing the commits). Assuming you want to combine the 2nd commit (Test Commit 1) and 3rd commits (Test commit 3) onto the 1st commit (Test Commit 2) change the command in front of the commits respectively to:

p 5477060 Test commit 1
s a3d6192 Test commit 2
s 67456dg Test commit 3

Once you save your changes to the file (wq! in vim) you should get the following prompt specify the commit message on top and save the file

SQUASHED COMMIT MESSAGE
# This is a combination of 3commits.
# This is the 1st commit message:
Test commit 1# This is the commit message #2:Test commit 2# This is the commit message #3:Test commit 3# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Jan 17 20:38:07 2018 -0800
#
# interactive rebase in progress; onto b5081cc
# Last commands done (2 commands done):
# p 5477060 Test commit 1
# s a3d6192 Test commit 2
# s 67456dg Test commit 3
# No commands remaining.
# You are currently rebasing branch 'master' on 'b5081cc'.
#
# Changes to be committed:
# modified: README.md

Voila! you should get a message confirming the successful rebase:

[detached HEAD d421532] SQUASHED COMMIT MESSAGE
Date: Wed Jan 17 20:38:07 2018 -0800
1 file changed, 2 insertions(+), 2 deletions(-)
Successfully rebased and updated refs/heads/master.

You can verify the commit history via a git log

commit d421532e982f1ee320052cfbaf37b5e43e0aadc0 (HEAD -> master)
Author: Author <author@mail.com>
Date: Wed Jan 17 20:38:07 2018 -0800
SQUASHED COMMIT MESSAGETest commit 1Test commit 2Test commit 3

Please note if you have already pushed to remote you might have to push again with -f flag to force rewrite the commit history on the remote again.

More to come..

--

--

Anurag Sinha

Engineering Manager, Google Shopping. Opinions expressed herein belong to him and not his employer. https://www.anuragsinha.me