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

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

如果不需要从前的承诺到主,并只是拒绝所有变更,做:

git reset --hard <SHA>

其他回答

想法:基本上你想用以前的承诺取代当前的工作树状态,然后从中创建承诺。

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


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


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

首先,什么是头?

HEAD 仅仅是指当前的承诺(后者)在当前的分支上,在任何时间(不包括 git 工作)只能有一个 HEAD。


脱头头

如果你不是最近的承诺 - 意思是,头指的是历史上以前的承诺,它被称为分开的头。

此分類上一篇

此分類上一篇



去支票

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

在此点上,您可以创建一个分支,并从此开始工作:

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>

git reflog 将显示任何更改,更新了 HEAD 并检查所需的 reflog 输入将设置 HEAD 返回此承诺。

git reflog
git checkout HEAD@{...}


“移动”你的头回到所需的承诺。

# 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.

注意: (由于 Git 2.7) 你也可以使用 git rebase --no-autostash 也。


此分類上一篇

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?.

我已经尝试了很多方式来逆转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 reflog

选择 git reflog 的 HEAD(s) 号码,您要转向和做(为此示例,我选择 12):

git reset HEAD@{12} --hard