Leo Alekseyev
Leo Alekseyev

Reputation: 13483

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.

Upvotes: 753

Views: 544079

Answers (11)

submartingale
submartingale

Reputation: 755

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.

Upvotes: 3

Ankit Singh
Ankit Singh

Reputation: 922

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

Upvotes: 1

Abdesselam
Abdesselam

Reputation: 1011

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

Upvotes: 4

816-8055
816-8055

Reputation: 557

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.

Upvotes: 28

Atif Majeed
Atif Majeed

Reputation: 1071

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

Upvotes: 10

ConorR
ConorR

Reputation: 487

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...

Upvotes: 1

Jackie Xu
Jackie Xu

Reputation: 127

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

git checkout master

Upvotes: 7

hothead1000
hothead1000

Reputation: 107

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

git checkout master
git checkout branchname

Upvotes: 9

Phil Miller
Phil Miller

Reputation: 38108

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.

Upvotes: 933

Bruce Wells
Bruce Wells

Reputation: 654

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.

Upvotes: 48

tanascius
tanascius

Reputation: 53924

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

Upvotes: 9

Related Questions