How to get back to most recent version in Git?

GitGit Checkout

Git Problem Overview


I have recently moved from SVN to Git and am a bit confused about something. I needed to run the previous version of a script through a debugger, so I did git checkout <previous version hash> and did what I needed to do.

Now I want to get back to the newest version, but I don't know the hash for it. When I type git log, I don't see it.

How can I do this? Also, is there an easier way to change versions than by typing out hashes - something like "go back two versions" or "go to the most chronologically recent"?

Git Solutions


Solution 1 - Git

git checkout master should do the trick. To go back two versions, you could say something like git checkout HEAD~2, but better to create a temporary branch based on that time, so git checkout -b temp_branch HEAD~2

Solution 2 - Git

When you checkout to a specific commit, git creates a detached branch. So, if you call:

$ git branch 

You will see something like:

* (detached from 3i4j25)
  master
  other_branch

To come back to the master branch head you just need to checkout to your master branch again:

$ git checkout master

This command will automatically delete the detached branch.

If git checkout doesn't work you probably have modified files conflicting between branches. To prevent you to lose code git requires you to deal with these files. You have three options:

  1. Stash your modifications (you can pop them later):

    $ git stash
    
  2. Discard the changes reset-ing the detached branch:

    $ git reset --hard
    
  3. Create a new branch with the previous modifications and commit them:

    $ git checkout -b my_new_branch
    $ git add my_file.ext
    $ git commit -m "My cool msg"
    

After this you can go back to your master branch (most recent version):

$ git checkout master

Solution 3 - Git

This did the trick for me (I still was on the master branch):

git reset --hard origin/master

Solution 4 - Git

Some of the answers here assume you are on master branch before you decided to checkout an older commit. This is not always the case.

git checkout -

Will point you back to the branch you were previously on (regardless if it was master or not).

Solution 5 - Git

To return to the latest version:

git checkout <branch-name> 

For example, git checkout master or git checkout dev

Solution 6 - Git

You can check out using branch names, for one thing.

I know there are several ways to move the HEAD around, but I'll leave it to a git expert to enumerate them.

I just wanted to suggest gitk --all -- I found it enormously helpful when starting with git.

Solution 7 - Git

I am just beginning to dig deeper into git, so not sure if I understand correctly, but I think the correct answer to the OP's question is that you can run git log --all with a format specification like this: git log --all --pretty=format:'%h: %s %d'. This marks the current checked out version as (HEAD) and you can just grab the next one from the list.

BTW, add an alias like this to your .gitconfig with a slightly better format and you can run git hist --all:

  hist = log --pretty=format:\"%h %ai | %s%d [%an]\" --graph

Regarding the relative versions, I found this post, but it only talks about older versions, there is probably nothing to refer to the newer versions.

Solution 8 - Git

When you go back to a previous version,

$ git checkout HEAD~2
Previous HEAD position was 363a8d7... Fixed a bug #32

You can see your feature log(hash) with this command even in this situation;

$ git log master --oneline -5
4b5f9c2 Fixed a bug #34
9820632 Fixed a bug #33
...

master can be replaced with another branch name.

Then checkout it, you'll be able to get back to the feature.

$ git checkout 4b5f9c2
HEAD is now at 4b5f9c2... Fixed a bug #34

Solution 9 - Git

With Git 2.23+ (August 2019), the best practice would be to use git switch instead of the confusing git checkout command.

To create a new branch based on an older version:

git switch -c temp_branch HEAD~2

To go back to the current master branch:

git switch master

Solution 10 - Git

A more elegant and simple solution is to use

git stash

It will return to the most resent local version of the branch and also save your changes in stash, so if you like to undo this action do:

git stash apply

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
QuestionNathan LongView Question on Stackoverflow
Solution 1 - GitAna BettsView Answer on Stackoverflow
Solution 2 - GitThomioView Answer on Stackoverflow
Solution 3 - GitaveraskoView Answer on Stackoverflow
Solution 4 - GitItai NoamView Answer on Stackoverflow
Solution 5 - GitReggie PinkhamView Answer on Stackoverflow
Solution 6 - GitJayView Answer on Stackoverflow
Solution 7 - GitharidsvView Answer on Stackoverflow
Solution 8 - GitkujiyView Answer on Stackoverflow
Solution 9 - GitVonCView Answer on Stackoverflow
Solution 10 - GitIlya GazmanView Answer on Stackoverflow