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.
开发者_StackOverflow中文版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 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
This did the trick for me (I still was on the master branch):
git reset --hard origin/master
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).
To return to the latest version:
git checkout <branch-name>
For example, git checkout master
or git checkout dev
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.
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.
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
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
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
精彩评论