5

On OSX, often I go to the git log in order to find a commit, usually a few back, copy it with my mouse, then rebase off of that.

How can I do this easily without using my mouse or memorizing it?

wejrowski
  • 439
  • 5
  • 20

4 Answers4

7

On OSX, you can use pbcopy.

So to get the SHA1 of your last commit in your clipboard:

git log -1 --format="%H" | pbcopy
Kuhess
  • 2,571
  • 1
  • 18
  • 17
  • Hmm, normally I would be doing it like 4 commits back which I would need to count and then write that which seems kind of tedious.. is this what you do? – wejrowski Sep 17 '14 at 20:38
  • 1
    With `git log -1 --format="%H` you only get the SHA1 of the last commit. But you can also do it with `git rev-parse HEAD`. So for example to get the SHA1 4 commits back: `git rev-parse HEAD~3` (note that it is 3, not 4). – Kuhess Sep 17 '14 at 20:45
  • 1
    Thanks for the tip, very helpful. BTW there is a new line in the string after copied, I used this to remove that one: `git log -1 --format="%H" | tr -d '\n' | pbcopy` – Billy Chan Apr 27 '19 at 02:15
3

Just memorize the first couple letters/numbers.

Git does not need the full hash to rebase, it only needs the first couple characters of it.

For example:

git log

commit a64da17d5f674973ead1a0bcf0196f292313893f

commit 11be728caad156d5cb6ce336747aab4e5e3417b0

commit e63760a22b4e5919961e409a66fac09176a574b6

commit 3521260b7d04fc92eaeb9c70fa46999dc1ecda3d

commit ba4868bd6a6b4e9d9a77f66e77be189d37b1ffe8

(minus the extra commit stuff)

Now, lets say you want the second one, 11be728caad156d5cb6ce336747aab4e5e3417b0 You can simply rebase on the first couple characters.

git rebase 11be

Further info: technically git only needs a unique start of a hash. So in this case, git rebase 1 would be sufficient because no other commit hashes begin with a 1. However, in extreme cases you might need more than 4-5 characters (Very unlikely)

Also, feel free to use git log -n to get only the last n number of commits. By keeping this to a low number, the commit is still usually on your screen when you call rebase, so you have no need to memorize. Just manually copy the first couple characters. Hint: If git flushes the log output once you hit 'q' to quit, you can use the command git --no-pager log -n to get the output to "stick".


For added info on git and rebase, if you knew you wanted to rebase exactly 4 commits, you could just use the HEAD reference. Your current commit is HEAD and 1 commit ago is HEAD~1 etc. For example:

git rebase HEAD~4

would set 3521260b7d04fc92eaeb9c70fa46999dc1ecda3d as the new HEAD (since we are rebasing on ba4868bd6a6b4e9d9a77f66e77be189d37b1ffe8)

Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
0

An updated version if you want to check what you're copying

This will copy the short hash. This is version is useful if you need to paste your commit hash into a GitHub comment. GitHub auto links commits (references).

git log -1 --format="%h" | pbcopy | echo `git log -1 --format="%h"`

This will copy the long hash

git log -1 --format="%H" | pbcopy | echo `git log -1 --format="%H"`
Roger Perez
  • 2,807
  • 29
  • 31
0

To copy a commit hash from n number of commits ago, pass it as an argument to the log command.

git log HEAD~3 --format="%h" | pbcopy
git log HEAD~3 --format="%H" | pbcopy

Replace HEAD~3 with the number of commits back you want to go. One way to verify you copied the correct hash is to paste it into git show

git show <pasted hash>

If it's wrong change the parent number accordingly. The higher the number, the further back in history you're going.

Refer to this SO link for more info on traversing commit history using tilde ~ and carrot ^

NOTE: Wasn't sure if I should just reply to wejrowski as a comment but decided to post as another answer to build on previous answers. Thanks to Kuhess and Nick Humrich for the initial answer