本文翻译自: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. 我最近从SVN搬到了Git,对某些事情感到有些困惑。 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. 我需要通过调试器运行以前版本的脚本,所以我做了git checkout <previous version hash>
并做了我需要做的事情。
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. 当我输入git log
,我看不到它。
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"? 此外,是否有更简单的方法来更改版本,而不是键入哈希 - 类似“返回两个版本”或“按时间顺序排列最近”?
#1楼
参考:https://stackoom.com/question/eVSs/如何回到Git中的最新版本
#2楼
这对我有用(我仍然在主分支上):
git reset --hard origin/master
#3楼
When you checkout to a specific commit, git creates a detached branch. 当您签出特定提交时,git会创建一个分离的分支。 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. 如果git checkout
不起作用,您可能修改了分支之间冲突的文件。 To prevent you to lose code git requires you to deal with these files. 为了防止你丢失代码git需要你处理这些文件。 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
#4楼
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
#5楼
git checkout master
should do the trick. git checkout master
应该做的伎俩。 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
要返回两个版本,你可以说git checkout HEAD~2
类的东西,但最好根据那个时间创建一个临时分支,所以git checkout -b temp_branch HEAD~2
#6楼
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. 我知道有几种方法可以移动HEAD,但是我会把它留给git专家来枚举它们。
I just wanted to suggest gitk --all
-- I found it enormously helpful when starting with git. 我只是想建议gitk --all
- 我发现在使用git时它非常有用。