我如何从我目前的状态转向在某个承诺上拍摄的截图?

如果我做 git 登录,那么我会得到以下输出:

$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me <me@me.com>
Date:   Thu Nov 4 18:59:41 2010 -0400

blah blah blah...

commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <me@me.com>
Date:   Thu Nov 4 05:13:39 2010 -0400

more blah blah blah...

commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <me@me.com>
Date:   Thu Nov 4 00:55:06 2010 -0400

And yet more blah blah...

commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <me@me.com>
Date:   Wed Nov 3 23:56:08 2010 -0400

Yep, more blah blah.

我如何转向11月3日的承诺,即承诺0d1d7fc?


当前回答

注意! 这个命令可能会导致失去承诺历史,如果用户错误地把错误的承诺。

我有一个类似的问题,我想回到以前的承诺,在我的情况下,我不感兴趣保持新的承诺,所以我使用了硬。

这就是我这样做的:

git reset --hard CommitId && git clean -f

这将转向本地存储库,在这里使用 git push -f 后将更新远程存储库。

git push -f

例如,如果你想完全忽略与名称的承诺执行非团体管理政策从下图

此分類上一篇

你会跑

git reset --hard dd52eb9 && git clean -f

跟随的

git push -f

接下来,你不会看到这个承诺(强制非团体管理政策)在那里

此分類上一篇

其他回答

在所有更改之后,当您按下所有这些命令时,您可能需要使用:

git push -f ...

不仅仅是Git Push。

有一个命令(不是核心Git的一部分,但它在Git-extra包中)具体用于逆转和排序旧命令:

git undo

对于男性页面,它也可以用作如下:

# Remove the latest three commits
git undo 3

最好的方式是:

git reset --hard <commidId> && git push --force

这将重新设置分支到具体的承诺,然后将远程服务器上传与您在当地的同样的承诺。

要小心 - 力量旗,因为它在选择的承诺后删除所有随后的承诺,而没有恢复它们的选项。

但是,为了完整性,我也想展示这些其他替代解决方案,也可以用来逆转一个承诺(在这个意义上,你创建一个新的承诺,阻止了以前的承诺的变化,就像Git的逆转一样)。

替代方案1:硬和软重组

这是查尔斯·贝利(Charles Bailey)的解决方案的一个非常轻微修改的版本,以便在Git中通过SHA的Hash转向一个承诺吗?

# Reset the index to the desired commit
git reset --hard <commit>

# Move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

# Commit the changes
git commit -m "Revert to <commit>"

这基本上是通过使用软重组将离开指数/重组区域的前承诺的状态,然后你可以承诺。

替代方案2:删除现有树,并用新树取代

git rm -r .
git checkout <commit> .
git commit

类似于替代 #1,这重复了当前工作副本中的 <commit> 状态. 首先需要做 git rm 因为 git checkout 不会删除自 <commit> 以来添加的文件。

Rogue 编码器?

工作自己,只是想要它工作吗? 遵循下面的指示,他们已经为我和许多其他人工作了多年。

阅读下面的评论,考虑其他答案,并与你的团队讨论,在你做一些漏洞之前。

将工作复制转换为最新的承诺

要转向以前的承诺,忽略任何变化:

git reset --hard HEAD

在哪里,头部是您当前分支的最后承诺

将工作副本转换为老年委托

要转向比最近的承诺更古老的承诺:

# Resets index to former commit; replace '56e05fced' with your commit code
git reset 56e05fced 

# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56e05fced"

# Updates working copy to reflect the new commit
git reset --hard

# Push your changes to respective branch
git push -f

信用去一个类似的 Stack Overflow 问题,转向一个承诺由一个 SHA hash 在 Git?.