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"?
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.
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.
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
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:
Stash your modifications (you can pop them later):
$ git stash
Discard the changes reset-ing the detached branch:
$ git reset --hard
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With