Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT - how do I copy SHA without mouse on OSX?

Tags:

git

macos

pbcopy

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?

like image 208
wejrowski Avatar asked Jan 10 '23 19:01

wejrowski


2 Answers

On OSX, you can use pbcopy.

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

git log -1 --format="%H" | pbcopy
like image 144
Kuhess Avatar answered Jan 19 '23 01:01

Kuhess


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)

like image 34
Nick Humrich Avatar answered Jan 19 '23 00:01

Nick Humrich