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.

- 36,389
- 13
- 67
- 90

- 12,893
- 5
- 44
- 44
-
18Regarding the `git log` sentence of your question, you can always run `git log --all` (or more usefully, `git log --oneline --graph --all`). – Wildcard Mar 17 '16 at 09:56
-
1Expanding on @Wildcard s comment, you can create a wonderful alias out of that `git log` command: [git adog](https://stackoverflow.com/a/35075021/4742889) – andschar Sep 15 '21 at 07:45
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.

- 36,389
- 13
- 67
- 90
-
41I'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". – AbePralle Feb 02 '12 at 07:19
-
10`master` isn't really any sort of keyword, the way `HEAD` is. It is just the default branch name in a new repository. You can run `git branch` to get a list of branches in your repository, and `git 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. – Phil Miller Feb 03 '12 at 21:54
-
4If it wasn't clear, `git reflog` gives you a list of hashes, at which point you can use `git checkout [commit-hash]`. – jbnunn Jan 30 '14 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? – Daniel S. May 13 '14 at 12:58
-
1@DanielS.: The question and my answer were about *commits*, while you're talking about files. Git treats them rather differently. – Phil Miller May 17 '14 at 17:33
-
Found via reddit: https://sethrobertson.github.io/GitFixUm/fixup.html "A "choose your own adventure" on fixing git commits" – Phil Miller Dec 27 '15 at 20:00
-
-1 this does not work. I did this: git checkout 9896ce318e4f4752812e7a5d6bdefaa430b290be . git checkout And now have big issue – Daniel Viglione Sep 21 '18 at 19:05
-
The `git checkout -` for the specific usecase of moving to latest commit may or may not work. E.g. if you did more than checkout in the row, it not going to work. – Hi-Angel Jan 29 '19 at 12:28
-
wow i learned two things, cd previous, and git checkout previous, yay hyphens – Akin Hwan Apr 29 '19 at 21:03
-
When you are in a 'detached HEAD' state, you can get back to the latest commit using `git switch -`. – Carlos Garcia-Vaso Sep 20 '21 at 15:16
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.

- 626
- 5
- 6
-
6
-
Git has changed the default branch name from `master` to `main`. Thus, nowadays `git checkout main` will get you what you want – codeananda Aug 31 '22 at 09:42
-
1@codeananda Unless you're working in one of the _many_ repos that didn't change from `master`. In other words, you can't assume it will be one or the other (although I assume if you get it wrong, Git will tell you - I'm a primarily Mercurial user so I can't say for sure). – Clonkex Mar 31 '23 at 04:17
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.

- 509
- 7
- 15
-
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-8055 Jan 29 '19 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-Angel Feb 04 '19 at 11:14
Have a look at the graphical GUI ... gitk
it shows all commits. Sometimes it is easier to work graphical ... ^^

- 36,389
- 13
- 67
- 90

- 53,078
- 22
- 114
- 136
-
yes, this is actually how I've done it in the past -- but I don't have the GUI available at the moment – Leo Alekseyev Mar 11 '10 at 17:56
You can use one of the following git command for this:
git checkout master
git checkout branchname

- 18,194
- 6
- 57
- 79

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

- 1,021
- 17
- 14
If your latest commit is on the master branch, you can simply use
git checkout master

- 117
- 1
- 2
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

- 1,001
- 10
- 10
-
Will the first command actually commit what code is currently staged, or am I misreading this? – Lazerbeak12345 Mar 27 '20 at 19:42
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.

- 715
- 7
- 16
-
-
I forgot to add `-d` flag. I am correcting the answer. Thank you @HomeroEsmeraldo for the feedback. – submartingale Sep 28 '21 at 10:30
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...

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

- 922
- 9
- 16
-
2I don't recommend this if you don't want to pull from upstream. One should handle merge conflicts only when they are ready to. – Lazerbeak12345 Mar 27 '20 at 19:40
-
1Well, in that case, the head will remain detached state even if you checkout to the latest commit. – Ankit Singh Mar 28 '20 at 08:04