Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get back to most recent version in Git?

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"?

like image 840
Nathan Long Avatar asked Aug 24 '10 17:08

Nathan Long


People also ask

How do you go back to the latest commit in git?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

How do I move my head to most recent commit?

We need to specify the HEAD or the relative reference of the head to the commit we want to move. We do this using git reset –hard HEAD^ which means resetting back to the commit before head, and git reset –hard HEAD~n, which means resetting back to the n commits before head.


2 Answers

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

like image 135
Ana Betts Avatar answered Oct 25 '22 08:10

Ana Betts


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 
like image 28
Thomio Avatar answered Oct 25 '22 09:10

Thomio