您是否需要创建一个分支来检查特定的 git 修订版?

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

Do you need to create a branch to check out a specific git revision?

git

提问by Nick

A common thing I'd like to do is revert my working copy to a particular revision, do some testing, and then bring it back to the head of my current master. In the past I have naively done a "git checkout hash" only to lose my head. I've since learned I can create a branch and check that out, switch back and delete the branch, but it feels like too many steps for a simple check. In SVN parlance, is there a way to quickly revert and then quickly go back to the tip of trunk in git?

我想做的一件常见的事情是将我的工作副本恢复到特定的修订版,进行一些测试,然后将其带回我当前的主人的头上。过去,我天真地做了一个“git checkout hash”,结果却让我失去了理智。从那以后我学会了我可以创建一个分支并检查它,切换回来并删除该分支,但是对于一个简单的检查来说感觉步骤太多了。用 SVN 的话来说,有没有办法在 git 中快速还原然后快速返回到主干的尖端?

Edit:I think my confusion stems from the fact that when I checkout a hash and then git log, I don't see the changes that happened after the checked out hash (which is reasonable, when you think of it). But the accepted answer is correct; "git checkout branch" will restore the head to the previous branch.

编辑:我认为我的困惑源于这样一个事实,即当我签出哈希然后 git log 时,我没有看到签出哈希之后发生的更改(这是合理的,当你想到它时)。但接受的答案是正确的;“git checkout branch” 将头部恢复到上一个​​分支。

回答by bdonlan

Assuming you're already on a branch (which you always should be for changes you want to keep), you can just do

假设你已经在一个分支上(你总是应该在你想要保留的更改上),你可以这样做

git checkout <revision to check out>

This will take you off the topic branch you were working on into (no branch), in which the working copy refers directly to a commit ID rather than a branch name as normal.

这将使您脱离您正在处理的主题分支(无分支),其中工作副本直接引用提交 ID 而不是正常的分支名称。

Then to move back, simply:

然后移回,只需:

git checkout <old branch name>

A helpful way to think of it is this: git checkout never alters branches; it merely changes what your working copy is currently looking at (ie, HEAD), which might either be a branch (in which case commits will update the branch) or a random commit hash.

一个有用的思考方式是: git checkout 从不改变分支;它只是改变你的工作副本当前正在查看的内容(即 HEAD),它可能是一个分支(在这种情况下提交将更新分支)或随机提交哈希。

As such, as long as the changes you want to keep are on a branch, you don't have to worry about git checkout losing them.

因此,只要您想要保留的更改在一个分支上,您就不必担心 git checkout 丢失它们。

回答by Philip Oakley

You can create a branch at the the specific commit you want to checkout (from the manual page)

您可以在要签出的特定提交处创建一个分支(来自手册页)

git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]

so fill in the <start-point>as the SHA1 you want you new branch <branchname>to start at and you won't get your 'head' detached from an expanding branch.

所以填写<start-point>你希望你的新分支<branchname>开始的 SHA1 ,你不会让你的“头”从一个扩展的分支中分离出来。

There will be other things you probably want to do to keep some of your working files in the state you desire....

您可能还需要做其他事情来使您的某些工作文件处于您想要的状态......

回答by araqnid

Yes, you can visit any arbitrary revision with "git checkout" as you describe. If you ask for an arbitrary revision rather than a branch, git will not have any obvious way to keep track of what changes you make, though. You can see where you were before by consulting the reflog ("git reflog show") - but in general, you would have been on a branch before so presumably just want to change back to that with "git checkout master" or similar.

是的,您可以按照您的描述使用“git checkout”访问任意修订版。但是,如果您要求进行任意修订而不是分支,则 git 将没有任何明显的方法来跟踪您所做的更改。您可以通过查阅 reflog(“git reflog show”)来查看您之前所在的位置 - 但一般来说,您之前可能已经在一个分支上,所以大概只想用“git checkout master”或类似的方式改回那个分支。

Note that this method won't automatically deal with uncommitted changes in your workspace--- either commit or stash your changes before moving between branches, or use "git checkout -m" to carry them around as you move (and be prepared to deal with merge conflicts if the changes you're carrying aren't trivial).

请注意,此方法不会自动处理工作区中未提交的更改——要么在分支之间移动之前提交或隐藏更改,要么在移动时使用“git checkout -m”来携带它们(并准备好处理如果您进行的更改不是微不足道的,则合并冲突)。

I think recent git versions have introduced the shorthand "@{-1}" for "where I was before I last moved", which might actually be what you want in this case. (I haven't used it, just read about it in the release notes).

我认为最近的 git 版本引入了“@{-1}”的简写“我上次搬家之前的位置”,在这种情况下,这实际上可能是您想要的。(我还没有使用过它,请在发行说明中阅读它)。

回答by Esko Luontola

If you want Git to show the tip of some line of changes, you need to have it branched or tagged. But even if you do not make a branch, none of the commits is lost. It stays in the repository as a garbage node (i.e. not reachable through any branch or tag), and is removed only some days/weeks later when you do "git gc".

如果您希望 Git 显示某些更改行的提示,则需要对其进行分支或标记。但即使您不创建分支,也不会丢失任何提交。它作为垃圾节点保留在存储库中(即无法通过任何分支或标签访问),并且在您执行“git gc”时仅在几天/几周后被删除。

So if you want to have an easy access to the changes, it's best to create a temporary branch and work there. If you do commits outside a branch and then checkout another branch, then you will need to use the command "git reflog" or look into the logs (".git/logs" directory) to find out what was the hash of the changes that you lost. When you know the hash, you can do "git checkout hash" or "git checkout -b new_branch hash" and recover the tip.

因此,如果您想轻松访问更改,最好创建一个临时分支并在那里工作。如果你在一个分支外提交,然后签出另一个分支,那么你需要使用命令“git reflog”或查看日志(“.git/logs”目录)来找出更改的哈希值是什么你输了。当您知道哈希值时,您可以执行“git checkout hash”或“git checkout -b new_branch hash”并恢复小费。