临时将工作副本切换到特定的 Git 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10230469/
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
Temporarily switch working copy to a specific Git commit
提问by Paul
How to switch to specific Git commit without losing all the commits made after it?
如何切换到特定的 Git 提交而不丢失之后所做的所有提交?
I want that local files will be changed, but commits' database will remain intact, only the current position pointer is set to currently selected commit.
我希望本地文件将被更改,但提交的数据库将保持不变,只有当前位置指针设置为当前选定的提交。
I want to change files' state to specific commit, run project and, when finished, restore files back to last commit.
我想将文件的状态更改为特定的提交,运行项目,并在完成后将文件恢复到上次提交。
How to do this without zipping the whole project's folder?
如何在不压缩整个项目文件夹的情况下执行此操作?
回答by Alexander Pavlov
If you are at a certain branch mybranch
, just go ahead and git checkout commit_hash
. Then you can return to your branch by git checkout mybranch
. I had the same game bisecting a bug today :) Also, you should know about git bisect.
如果您在某个分行mybranch
,请继续并git checkout commit_hash
。然后您可以通过 返回您的分行git checkout mybranch
。我今天有同样的游戏平分一个错误 :) 另外,你应该知道git bisect。
回答by Femaref
First, use git log
to see the log, pick the commit you want, note down the sha1 hash that is used to identify the commit. Next, run git checkout hash
. After you are done, git checkout original_branch
. This has the advantage of not moving the HEAD, it simply switches the working copy to a specific commit.
首先,使用git log
查看日志,选择您想要的提交,记下用于标识提交的 sha1 哈希。接下来,运行git checkout hash
。完成后,git checkout original_branch
。这具有不移动 HEAD 的优点,它只是将工作副本切换到特定的提交。
回答by Benjohn
In addition to the other answers here showing you how to git checkout <the-hash-you-want>
it's worth knowing you can switch back to where you were using:
除了这里的其他答案向您展示如何git checkout <the-hash-you-want>
值得知道您可以切换回您使用的位置:
git checkout @{-1}
This is often more convenient than:
这通常比:
git checkout what-was-that-original-branch-called-again-question-mark
As you might anticipate, git checkout @{-2}
will take you back to the branch you were at two git checkout
s ago, and similarly for other numbers. If you can remember where you were for bigger numbers, you should get some kind of medal for that.
正如您可能预期的那样,git checkout @{-2}
将带您回到git checkout
两秒前所在的分支,其他数字也是如此。如果你能记住你在哪里获得更大的数字,你应该为此获得某种奖牌。
Sadly for productivity, git checkout @{1}
does not take you to the branch you will be on in future, which is a shame.
可悲的是,对于生产力,git checkout @{1}
不会带您到将来要使用的分支,这是一种耻辱。