如何将 HEAD 移动到 git 中的最新日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17289745/
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 16:25:19  来源:igfitidea点击:

How to move the HEAD to the latest date in git?

githead

提问by max

What I wish to do is to get the version of the file that has a specific comment, get it and use it, then change it to the latest code.

我希望做的是获取具有特定注释的文件版本,获取并使用它,然后将其更改为最新代码。

git log -g --grep="code submitted version 0.1.2.3"

This returns a hash 123456, then I do:

这将返回一个 hash 123456,然后我做:

git checkout 123456

and use the older version.

并使用旧版本。

Now I want to change the HEADback to the latest. This I could not do. I have tried:

现在我想改HEAD回最新的。这是我做不到的。我试过了:

git reset --hard
git clean -f
git pull

Any ideas?

有任何想法吗?

回答by Micha Wiedenmann

When you called git checkout 123456you moved your HEADfrom the commit you were currently on (most likely the head of the masterbranch) to the commit 123456. Therefor you are looking for a way to move HEADback to the branch you were previously on, which you can do with:

当您打电话时,git checkout 123456您将您HEAD的提交从您当前所在的提交(很可能是master分支的负责人)转移到了提交123456。因此,您正在寻找一种HEAD返回到您之前所在的分支的方法,您可以这样做:

git checkout master

If you want to take a look at a specific revision of a file, you can either just view it using

如果您想查看文件的特定修订版,您可以使用

git show 123456:/txt/file.txt

or temporarily check out only this file with

或者暂时只签出这个文件

git checkout 123456:/txt/file.txt
// use it
git checkout :/txt/file.txt

Explanation of your tries:

您尝试的解释

git reset --hard

git reset --hard

Reverts all changes of the current HEAD, but does not move HEAD. After a reset, git statusshows that everything is "clean".

恢复当前的所有更改HEAD,但不移动HEAD。重置后,git status显示一切“干净”。

git clean

git clean

Removes all untracked files from the working tree, again HEADis not moved.

从工作树中删除所有未跟踪的文件,同样HEAD不会移动。

git pull

git pull

Fetches the upstream changes and merges them in. Not what you want.

获取上游更改并将它们合并。不是你想要的。