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?
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"`
On OSX, you can use pbcopy
.
So to get the SHA1 of your last commit in your clipboard:
git log -1 --format="%H" | pbcopy
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
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:
(minus the extra commit stuff)
Now, lets say you want the second one,
11be728caad156d5cb6ce336747aab4e5e3417b0
You can simply rebase on the first couple characters.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 commandgit --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 isHEAD
and 1 commit ago isHEAD~1
etc. For example:would set
3521260b7d04fc92eaeb9c70fa46999dc1ecda3d
as the newHEAD
(since we are rebasing onba4868bd6a6b4e9d9a77f66e77be189d37b1ffe8
)