简体   繁体   中英

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

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?

On OSX, you can use pbcopy .

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

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

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 )

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"`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM