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

GitVersion Control

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

git checkout master

master is the tip, or the last commit. gitk will only show you up to where you are in the tree at the time. git reflog will show all the commits, but in this case, you just want the tip, so git checkout master.

Solution 3 - Git

Came across this question just now and have something to add

To go to the most recent commit:

git checkout $(git log --branches -1 --pretty=format:"%H")

Explanation:

git log --branches shows log of commits from all local branches
-1 limit to one commit → most recent commit
--pretty=format:"%H" format to only show commit hash
git checkout $(...) use output of subshell as argument for checkout

Note:

This will result in a detached head though (because we checkout directly to the commit). This can be avoided by extracting the branch name using sed, explained below.


To go to the branch of the most recent commit:

git checkout $(git log --branches -1 --pretty=format:'%D' | sed 's/.*, //g')

Explanation:

git log --branches shows log of commits from all local branches
-1 limit to one commit → most recent commit
--pretty=format:"%D" format to only show ref names
| sed 's/.*, //g' ignore all but the last of multiple refs (*)
git checkout $(...) use output of subshell as argument for checkout

*) HEAD and remote branches are listed first, local branches are listed last in alphabetically descending order, so the one remaining will be the alphabetically first branch name

Note:

This will always only use the (alphabetically) first branch name if there are multiple for that commit.


Anyway, I think the best solution would just be to display the ref names for the most recent commit to know where to checkout to:

git log --branches -1 --pretty=format:'%D'

E.g. create the alias git top for that command.

Solution 4 - Git

Have a look at the graphical GUI ... gitk it shows all commits. Sometimes it is easier to work graphical ... ^^

Solution 5 - Git

You can use one of the following git command for this:

git checkout master
git checkout branchname

Solution 6 - Git

git reflog //find the hash of the commit that you want to checkout
git checkout <commit number>>

Solution 7 - Git

show all branches and commit
git log --branches --oneline

show last commit
git log --branches -1 --oneline

show before last commit
git log --branches -2 --oneline

Solution 8 - Git

If your latest commit is on the master branch, you can simply use

git checkout master

Solution 9 - Git

If you have a branch different than master, one easy way is to check out that branch, then check out master. Voila, you are back at the tip of master. There's probably smarter ways...

Solution 10 - Git

You can simply do git pull origin branchname. It will fetch the latest commit again.

Solution 11 - Git

For git versions >=2.33.0

git switch -d -

allows you check out to the previously checked out commit. So, you can go back-and-forth by switching between two commits. Please notice that -d flag allows you to surf among commits in a detached state.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeo AlekseyevView Question on Stackoverflow
Solution 1 - GitPhil MillerView Answer on Stackoverflow
Solution 2 - GitBruce WellsView Answer on Stackoverflow
Solution 3 - Git816-8055View Answer on Stackoverflow
Solution 4 - GittanasciusView Answer on Stackoverflow
Solution 5 - Githothead1000View Answer on Stackoverflow
Solution 6 - GitAtif MajeedView Answer on Stackoverflow
Solution 7 - GitabdesselamView Answer on Stackoverflow
Solution 8 - GitJackie XuView Answer on Stackoverflow
Solution 9 - GitConorRView Answer on Stackoverflow
Solution 10 - GitAnkit SinghView Answer on Stackoverflow
Solution 11 - GitsubmartingaleView Answer on Stackoverflow