你需要创建一个分支来检出特定的git修订版吗? [英] Do you need to create a branch to check out a specific git revision?
问题描述
编辑:我想我的困惑源于这样一个事实:当我签出一个散列并且然后签入git日志时,我看不到在签出散列之后发生的变化(当你想到它时这是合理的)。但接受的答案是正确的; git checkout branch会将头部恢复到前一分支。
解决方案假设你已经在一个分支上(你总是应该为你想要保留的变更),你可以做
git checkout<修改以检出>>
这将带您离开您正在研究的主题分支(无分支),其中工作副本直接指向提交ID而不是正常的分支名称。
然后返回,简单地说:
git checkout<旧分行名称>
一个有用的方法是:git checkout永远不会改变分支;它只是改变你的工作副本当前正在查看的东西(即HEAD),它可能是一个分支(在这种情况下,提交会更新分支)或随机提交哈希。
因此,只要您想保留的更改位于分支上,您就不必担心git checkout会丢失它们。
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?
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.
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.
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.
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修订版吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!