git 在不破坏当前提交的情况下返回上一次提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9088543/
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
going back to previous commit without destroying the current one
提问by xonegirlz
So I wanted to go back to my previous commit without destryoing the latest one I have now. How do I do this? Doing a git reset HARD will destroy my current commit right?
所以我想回到我之前的提交而不破坏我现在的最新提交。我该怎么做呢?执行 git reset HARD 会破坏我当前的提交吗?
回答by Daniel Pittman
Assuming you don't have any changes that you have not checked in, git checkout $OLD_COMMIT
will take you there, and git checkout $BRANCH
will take you back.
假设您没有任何未签入的更改,git checkout $OLD_COMMIT
将带您到那里,并git checkout $BRANCH
带您返回。
That will put you, potentially, on a detached head; see git checkout --help
for the details, but if all you want is to run tests on the old version or something that should be fine.
这可能会让你处于超然状态;查看git checkout --help
详细信息,但如果您只想在旧版本上运行测试或应该没问题的东西。
You might also be interested in:
git stash
- to store away uncommitted changes temporarily
git show $SHA1
to show the changes of the old commit as a diff
git show ${REF}:${PATH}
to show the content of $PATH in $REF
您可能还对:
git stash
- 暂时存储未提交
git show $SHA1
的更改以将旧提交的更改显示为差异
git show ${REF}:${PATH}
以显示 $REF 中 $PATH 的内容
(Ref can be a sha, or a branch, or whatever, in the last git show invocation.)
(在最后一次 git show 调用中,Ref 可以是 sha、分支或其他任何内容。)
回答by Justin ??????
Use git revert
. This allows you to undo a specific commit without upsetting commits since.
使用git revert
. 这允许您撤消特定的提交,而不会打乱此后的提交。
If I understand your case correctly, you have:
如果我正确理解你的情况,你有:
* abc123de a_good_commit
* bc123def a_bad_commit
* c123def4 another_good_commit
You can either git revert HEAD^
or use a specific hash: git revert bc123def
.
您可以git revert HEAD^
或使用特定的哈希:git revert bc123def
.
回答by antak
Doing any kind of reset
to a previous commit will remove your later commitsfrom your branch such that they're no longer part of it. A commitbeing the items you see when you do git log
.
reset
对以前的提交做任何类型的操作都会从你的分支中删除你以后的提交,这样它们就不再是它的一部分。一个犯被你看到,当你做的项目git log
。
This doesn't mean those commits are instantly destroyed, but as your intention isn't to get rid of them, you'd probably want to create another branch and use that to look at your previous commit so that your original branch is left with the latest commits intact. (Keywords for creating a new branch are: git branch <name>
or git checkout -b <name>
. **)
这并不意味着这些提交会立即被销毁,但是由于您的意图不是摆脱它们,您可能想要创建另一个分支并使用它来查看您之前的提交,以便您的原始分支保留最新的提交完好无损。(创建新分支的关键字是:git branch <name>
或git checkout -b <name>
。**)
Hard vs soft reset has more to do with what happens to the files in your working tree, and more importantly to uncommited changes. In essense, the only things destroyed when you specify --hard
are your uncommited changes. As long as you've left a branch at your latest commit (or you remember the commit id), you can always recover the files by switching back to that branch or commit. (i.e. git checkout
)
硬重置与软重置更多地与工作树中的文件发生的情况有关,更重要的是与未提交的更改有关。从本质上讲,当您指定时,唯一被破坏的--hard
是您未提交的更改。只要您在最近一次提交时离开了一个分支(或者您记得提交 ID),您始终可以通过切换回该分支或提交来恢复文件。(即git checkout
)
** You can actually look around without creating an extra branch and Daniel Pittman's answer describes that.
** 您实际上可以在不创建额外分支的情况下环顾四周,Daniel Pittman 的回答对此进行了描述。