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.
11 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.
-
40I'd like to mention that the typical example would be "git checkout master". One of the difficulties I had learning to use git was not knowing what specific keywords (e.g. "master") I can actually substitute in for placeholder words like "branchname". Feb 2, 2012 at 7:19
-
9
master
isn't really any sort of keyword, the wayHEAD
is. It is just the default branch name in a new repository. You can rungit branch
to get a list of branches in your repository, andgit tag -l
for a list of tags. Similarly,origin
is the default name of the remote that a repository is cloned from, but there's nothing special about it. Feb 3, 2012 at 21:54 -
4If it wasn't clear,
git reflog
gives you a list of hashes, at which point you can usegit checkout [commit-hash]
.– jbnunnJan 30, 2014 at 23:42 -
I deleted a file and tried the command, but it doesn't completely reset it. First of all, it's not recursive. Then when I try this on the file i deleted, git afterwards tells me that HEAD is detached. What to do? May 13, 2014 at 12:58
-
1@DanielS.: The question and my answer were about commits, while you're talking about files. Git treats them rather differently. May 17, 2014 at 17:33
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.
-
5
-
Git has changed the default branch name from
master
tomain
. Thus, nowadaysgit checkout main
will get you what you want Aug 31, 2022 at 9:42
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.
-
1Somehow I goofed up the second command... Fixed it, it will now correctly check you out ot the branch of the most recent commit.– 816-8055Jan 29, 2019 at 14:53
-
Bash/zsh alias for the command to return back to head
alias git-head='git checkout $(git log --branches -1 --pretty=format:"%D" | sed "s/.*, //g")'
– Hi-AngelFeb 4, 2019 at 11:14
Have a look at the graphical GUI ... gitk
it shows all commits. Sometimes it is easier to work graphical ... ^^
-
yes, this is actually how I've done it in the past -- but I don't have the GUI available at the moment Mar 11, 2010 at 17:56
You can use one of the following git command for this:
git checkout master
git checkout branchname
git reflog //find the hash of the commit that you want to checkout
git checkout <commit number>>
If your latest commit is on the master branch, you can simply use
git checkout master
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
-
Will the first command actually commit what code is currently staged, or am I misreading this? Mar 27, 2020 at 19:42
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...
You can simply do git pull origin branchname
. It will fetch the latest commit again.
-
1I don't recommend this if you don't want to pull from upstream. One should handle merge conflicts only when they are ready to. Mar 27, 2020 at 19:40
-
Well, in that case, the head will remain detached state even if you checkout to the latest commit. Mar 28, 2020 at 8:04
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.
-
-
I forgot to add
-d
flag. I am correcting the answer. Thank you @HomeroEsmeraldo for the feedback. Sep 28, 2021 at 10:30
git log
sentence of your question, you can always rungit log --all
(or more usefully,git log --oneline --graph --all
).git log
command: git adog