I sometimes check out some previous version of the code to examine or test. I have seen instructions on what to do if I wish to modify previous commits -- but suppose I make no changes. After I've done e.g. git checkout HEAD^
, how do I get back to the tip of the branch?.. git log
no longer shows me the SHA of the latest commit.
If you want to test the previous commit just do git checkout <test commit hash> ; then you can test that last working version of your project. If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit.
git hard reset to head – We need to specify the HEAD or the relative reference of the head to the commit we want to move. We do this using git reset –hard HEAD^ which means resetting back to the commit before head, and git reset –hard HEAD~n, which means resetting back to the n commits before head.
If you know the commit you want to return to is the head of some branch, or is tagged, then you can just
git checkout branchname
You can also use git reflog
to see what other commits your HEAD (or any other ref) has pointed to in the past.
Edited to add:
In newer versions of Git, if you only ran git checkout
or something else to move your HEAD
once, you can also do
git checkout -
to switch back to wherever it was before the last checkout. This was motivated by the analogy to the shell idiom cd -
to go back to whatever working directory one was previously in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With