你需要创建一个分支来检查特定的git修订吗?

时间:2021-01-21 07:32:15

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中的trunk端?

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日志时,我没有看到签出哈希之后发生的变化(这是合理的,当你想到它)。但接受的答案是正确的; “git checkout branch”将恢复到前一个分支的头部。

4 个解决方案

#1


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会丢失它们。

#2


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.

所以填写 作为SHA1你希望你的新分支 开始,你不会让你的'头'脱离扩展分支。

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

你可能还想做其他事情来保持你工作的文件处于你想要的状态....

#3


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}”的缩写,“我上次移动之前的位置”,这可能实际上就是你想要的。 (我没有使用它,只是在发行说明中阅读它)。

#4


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”并恢复提示。

#1


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会丢失它们。

#2


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.

所以填写 作为SHA1你希望你的新分支 开始,你不会让你的'头'脱离扩展分支。

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

你可能还想做其他事情来保持你工作的文件处于你想要的状态....

#3


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}”的缩写,“我上次移动之前的位置”,这可能实际上就是你想要的。 (我没有使用它,只是在发行说明中阅读它)。

#4


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”并恢复提示。