git 回到 Gitkraken 中的先前提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45683540/
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 a previous commit in Gitkraken
提问by Joymaker
My programming partner and I, being fairly new to Git and Gitkraken, made some mistakes, did some pushes pulls and commits in the wrong order, and got things fairly bungled up. I would like to go get a snapshot from several commits back, and manually compare it to the current state of the code. A command along the lines of
我和我的编程伙伴,对 Git 和 Gitkraken 相当陌生,犯了一些错误,以错误的顺序进行了一些推拉和提交,并且把事情搞砸了。我想从几次提交中获取快照,并手动将其与代码的当前状态进行比较。沿线的命令
"Take the working directory state to thishistoric commit" or better yet, "give me the contents of thishistoric commit into a different working directory, which I will name"
“将工作目录状态带到这个历史提交”或者更好的是,“给我这个历史提交的内容到一个不同的工作目录,我会命名”
The options given to me don't look that way, exactly. I see "reset master to this commit" in several flavors, but none of those titles convince me that I'm going to get to look at the previous state without damaging the path back to the current state. Am I missing something? What is the best way to do what I'm trying to do?
给我的选项看起来并不是那样。我看到了几种风格的“重置主提交”,但这些标题都没有让我相信我将在不破坏返回当前状态的路径的情况下查看以前的状态。我错过了什么吗?做我想做的事情的最佳方法是什么?
回答by Christopher Díaz Riveros
I suggest you tu use git reflog
, this command contains a local history of your commits and changes, with this log you will see all the events from your local repo.
我建议您使用git reflog
,此命令包含您提交和更改的本地历史记录,通过此日志,您将看到本地存储库中的所有事件。
After finding the exact commit that you want to recover, you can set that commit as starting point with git reset --soft HEAD@{n}
找到要恢复的确切提交后,您可以将该提交设置为起点 git reset --soft HEAD@{n}
what --soft
does is
什么--soft
是
Does not touch the index file or the working tree at all (but resets the
head to <commit>, just like all modes do). This leaves all your changed
files "Changes to be committed", as git status would put it.
After this, you'll have all the changes in your stage area, then you can either decide if commit them again or change something.
在此之后,您将拥有舞台区域中的所有更改,然后您可以决定是再次提交它们还是更改某些内容。
I'd suggest you to read thissection from Git's bookand specially thissection about undoing things.
PS. as an extra, you can use git reset
as many times as you want, because all the commands that you use are saved in git reflog
and because of that, if you do something that you don't want to, you can just undo it too.
附注。另外,您可以git reset
根据需要多次使用,因为您使用的所有命令都保存在其中git reflog
,因此,如果您做了一些您不想做的事情,您也可以撤消它。
Hope it helps
希望能帮助到你
回答by kowsky
...but none of those titles convince me that I'm going to get to look at the previous state without damaging the path back to the current state...
...但是这些标题都没有让我相信我将在不破坏返回当前状态的路径的情况下查看以前的状态...
First of all, you can always go back. GitKraken provides the handy Undobutton that lets you revert most things you do with a simple button klick. If you want to be sure you can go back easily, you can just create a backup
branch. If things go awry, check it out and you're back.
首先,你可以随时回去。GitKraken 提供了方便的“撤消”按钮,让您只需单击一下按钮即可还原您所做的大部分事情。如果您想确保可以轻松返回,则可以创建一个backup
分支。如果出现问题,请检查一下,然后您就回来了。
Thus, you can relax and just try things out without the need to worry about breaking anything. The question now is what you actually want to do:
因此,您可以放松并尝试一下,而无需担心破坏任何东西。现在的问题是你真正想要做什么:
"Take the working directory state to this historic commit"
If you just want to return to a previous state (i.e. commit), you can use the
reset <branch> to this commit - hard
. You'll be back at the selected commit as if nothing happened. If your prevous state is still reachable from any branch (this is the case if you created a backup branch), you can still look at all the commits and their changes.Be aware that you loose unstaged and uncommited changes when doing a hard reset.
"give me the contents of this historic commit into a different working directory, which I will name"
Git does only know one working directory, so this is not directly possible. You can, however, compare your current state to a previous one, in the way @Christopher Díaz Riveros suggested. Instead of a
hard
reset, you can perform asoft
one. This will reset your working directory to the selected commit, but keep all changes (compared to your previous state) as staged changes. This lets you easly review them, since they appear in the bottom right part of GitKraken and you can discard or commit as needed.
“将工作目录状态带到这个历史性提交”
如果您只想返回到之前的状态(即提交),您可以使用
reset <branch> to this commit - hard
. 您将回到选定的提交,就好像什么也没发生一样。如果您的前一个状态仍然可以从任何分支访问(如果您创建了一个备份分支就是这种情况),您仍然可以查看所有提交及其更改。请注意,在进行硬重置时,您会丢失未暂存和未提交的更改。
“给我这个历史性提交的内容到一个不同的工作目录,我会命名”
Git 只知道一个工作目录,所以这不是直接可能的。但是,您可以按照@Christopher Díaz Riveros 建议的方式将您当前的状态与之前的状态进行比较。
hard
您可以执行一个重置,而不是重置soft
。这会将您的工作目录重置为选定的提交,但将所有更改(与您之前的状态相比)保留为暂存更改。这让您可以轻松查看它们,因为它们出现在 GitKraken 的右下角,您可以根据需要丢弃或提交。