Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get back to the latest commit after checking out a previous commit?

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.

like image 553
Leo Alekseyev Avatar asked Mar 11 '10 17:03

Leo Alekseyev


People also ask

How do I go back to the last commit in checkout?

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.

How do I bring my head to the latest 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.


1 Answers

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.

like image 153
Phil Miller Avatar answered Oct 18 '22 14:10

Phil Miller