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

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


当前回答

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

暂时转换到另一个承诺

# 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 rm -r --cached. && git clean -f -d 将工作树带到我们想要的状态 **. git checkout 0d1d7fc3. 创建逆转承诺. git add --all & git commit -m "revert to 0d1d7fc3"


此外,它不会从历史中删除任何东西(压抑或压抑),它产生一个纯粹的承诺,代表我们想要回归的状态。


**当路径被指定(在这里: )时,支票单独留下头部。

我发现最不复杂的方式,把一个分支转向任何特定的承诺,在那里你不能改变历史,就是:

查看承诺或分支您的愿望转向. 编辑.git/HEAD 并更改转向您转向的分支。

如如:

echo 'ref: refs/heads/example' > .git/HEAD

如果你做 git 状态,你应该看到你所在的分支和你想转向的分支之间的所有变化。

如果一切都看起来很好,你可以承诺. 你也可以使用 git diff revert..example 确保它是相同的。

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

暂时转换到另一个承诺

# 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的本地变化,如果你只是想逆转到最近的承诺状态,这似乎是最好的。

git add . && git checkout master -f

简短的描述:

它不会创建任何命令,因为 git revert 做. 它不会分开你的头像 git checkout <commithashcode> 做. 它将超越所有的本地变更,并删除所有的添加文件,因为最后的命令在分支。

我找到了一个更方便和简单的方式来实现上面的结果:

git add . && git reset --hard HEAD

HEAD 指向您当前分支的最新承诺。

它与 boulder_ruby 提出的代码相同,但我添加了 git add. 之前 git reset --hard HEAD 删除所有新的文件创建自最后的承诺,因为这是大多数人期望我相信,当转向最新承诺时。

这里是一个更简单的方式,回到以前的承诺(并保持它在一个不承诺的状态,与它做任何你喜欢):

git reset HEAD~1

所以,不需要承诺ID等等:)