如何在 Git 中返回到最新版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3559076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:56:54  来源:igfitidea点击:

How to get back to most recent version in Git?

gitgit-checkout

提问by Nathan Long

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.

我最近从 SVN 转移到 Git,对某些事情有点困惑。我需要通过调试器运行以前版本的脚本,所以我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"?

我怎样才能做到这一点?此外,是否有比输入哈希更简单的方法来更改版本 - 诸如“返回两个版本”或“转到按时间顺序排列的最新版本”?

回答by Ana Betts

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

git checkout master应该做的伎俩。要返回两个版本,您可以这样说git checkout HEAD~2,但最好根据那个时间创建一个临时分支,所以git checkout -b temp_branch HEAD~2

回答by Thomio

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

当您检出特定提交时,git 会创建一个分离的分支。所以,如果你打电话:

$ 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:

要返回 master 分支,您只需要再次结帐到您的 master 分支:

$ git checkout master

This command will automatically delete the detached branch.

此命令将自动删除分离的分支。

If git checkoutdoesn'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:

如果git checkout不起作用,您可能修改了分支之间冲突的文件。为了防止你丢失代码,git 需要你处理这些文件。您有三个选择:

  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"
    
  1. 隐藏你的修改(你可以稍后弹出它们):

    $ git stash
    
  2. 丢弃更改重置分离的分支:

    $ git reset --hard
    
  3. 使用先前的修改创建一个新分支并提交它们:

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

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

在此之后,您可以返回到您的主分支(最新版本):

$ git checkout master

回答by averasko

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

这对我有用(我仍然在主分支上):

git reset --hard origin/master

回答by Reggie Pinkham

To return to the latest version:

要返回到最新版本:

git checkout <branch-name> 

For example, git checkout masteror git checkout dev

例如,git checkout mastergit checkout dev

回答by Jay

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 时非常有帮助。

回答by haridsv

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 --allwith 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.

我刚刚开始深入研究 git,所以不确定我是否理解正确,但我认为 OP 问题的正确答案是您可以git log --all使用如下格式规范运行:git log --all --pretty=format:'%h: %s %d'. 这将当前签出的版本标记为(HEAD),您可以从列表中获取下一个。

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

顺便说一句,.gitconfig使用稍微更好的格式向您添加这样的别名,您可以运行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.

关于相对版本,我找到了这个帖子,但它只讨论旧版本,可能没有提及新版本。

回答by Itai Noam

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.

这里的一些答案假设您在决定签出较旧的提交之前在 master 分支上。这并非总是如此。

git checkout -

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

将指向您之前所在的分支(无论它是否是主分支)。

回答by kujiy

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
...

mastercan be replaced with another branch name.

master可以替换为另一个分支名称。

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

回答by VonC

With Git 2.23+ (August 2019), the best practice would be to use git switchinstead of the confusing git checkoutcommand.

对于 Git 2.23+(2019 年 8 月),最佳实践是使用git switch而不是使用令人困惑的git checkout命令。

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

回答by Ilya Gazman

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:

它将返回到分支的最反感的本地版本,并将您的更改保存在 stash 中,因此如果您想撤消此操作,请执行以下操作:

git stash apply