GIT - how to copy SHA without mouse on OSX?

On OSX, often I go to git log

to find a commit, usually a bit back, copy it with the mouse, and then undo that.

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

+3


source to share


2 answers


Just memorize the first couple of letters / numbers.

Git doesn't need a full hash for rebase, it only needs the first couple of characters.

For example:

git log

      

commit a64da17d5f674973ead1a0bcf0196f292313893f

commit 11be728caad156d5cb6ce336747aab4e5e3417b0

commit e63760a22b4e5919961e409a66fac09176a574b6

commit 3521260b7d04fc92eaeb9c70fa46999dc1ecda3d

commit ba4868bd6a6b4e9d9a77f66e77be189d37b1ffe8

(minus additional fixation items)

Now, let's say you want the second one, 11be728caad156d5cb6ce336747aab4e5e3417b0

you can just reinstall the symbols of the first pair.

git rebase 11be

      




Additional info: Technically, git only needs a unique hash start. So in this case git rebase 1

it would be enough, because no other hash-fixers started with 1. However, in extreme cases, you may need more than 4-5 characters (very unlikely)

Also, feel free to use git log -n

to get only the last number of numbers. By keeping this low, the commit is still usually on your screen when you rebase, so you don't have to remember. Just copy the first couple of characters manually. Hint: if git flushes the log output, as soon as you hit "q" to exit, you can use the command git --no-pager log -n

to get the "stick" output.


For more information on git and rebase, if you knew you wanted to rebase exactly 4 commits, you can simply use the link HEAD

. Your current commit HEAD

and 1 commit ago - HEAD~1

etc. For example:

git rebase HEAD~4

      

would set 3521260b7d04fc92eaeb9c70fa46999dc1ecda3d

as new HEAD

(as we reboot to ba4868bd6a6b4e9d9a77f66e77be189d37b1ffe8

)

+3


source


On OSX, you can use pbcopy

.

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



git log -1 --format="%H" | pbcopy

      

+3


source







All Articles