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

如果我做 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?


当前回答

假设你正在谈论大师和相应的分支(也就是说,这可能是你所关心的任何工作分支):

# Reset local master branch to November 3rd commit ID
git reset --hard 0d1d7fc32e5a947fbd92ee598033d85bfc445a50

# Reset remote master branch to November 3rd commit ID
git push -f origin 0d1d7fc32e5a947fbd92ee598033d85bfc445a50:master

我在博客文章中找到答案(现在不再存在)

请注意,这就是重定向和强迫变更到远程,所以如果你的团队中的其他人已经吸引了Git,你会为他们造成问题。

使用逆转(见其他答案)更好,而不是重新设置. 如果你是一个单人团队,那么这可能并不重要。

其他回答

返回(或逆转):

1. git revert --no-commit "commit-code-to-remove" HEAD
(e.g. git revert --no-commit d57a39d HEAD)
2. git commit
3. git push

尝试上面的两个步骤,如果你发现这是你想要的,那么 git push。

如果你发现什么是错误的,做:

git revert --abort

但是,为了完整性,我也想展示这些其他替代解决方案,也可以用来逆转一个承诺(在这个意义上,你创建一个新的承诺,阻止了以前的承诺的变化,就像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> 以来添加的文件。

这很大程度上取决于你指的是“逆转”。

暂时转换到另一个承诺

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32

git checkout -b old-state 0d1d7fc32

要回到你所在的地方,只需检查你所在的分支再次(如果你做了变化,如往常交换分支时,你将不得不处理它们,你可以重新设置,把它们扔掉;你可以打破,支票,打破,把它们带到你身边;你可以承诺它们到一个分支,如果你想要一个分支在那里。

硬删除未发表的承诺

如果,另一方面,你真的想摆脱你从那时起所做的一切,有两种可能性。

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

如果你混乱,你已经扔掉了你的地方变化,但你至少可以通过重新设置回到你以前的地方。

下一篇: 与新承诺发布的承诺

# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

#Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

您也可以在此情况下找到这个答案有用:我如何将头转回以前的位置?(解密头) & Undo 承诺

还有另一个最简单的解决方案;你必须改变分支来做到这一点,但后来你只能运行:

git branch -f <<branchname>> 0d1d7fc32e5a947fbd92ee598033d85bfc445a50

尝试重定向到所需的承诺:

git reset <COMMIT_ID>

查看 COMMIT_ID 使用:

git log

这将将所有更改的文件重定向到未添加状态。

现在,您可以查看所有未添加的文件

git checkout .

要检查您的使用变更:

git log

更新

如果你有一個,只承諾在你的repo,嘗試

git update-ref -d HEAD