我如何从我目前的状态转向在某个承诺上拍摄的截图?
如果我做 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 的能力正如你想要的那样创作承诺,这意味着有时你需要在 git 添加中进行的更改中取消更改。
Git 不知道这些变化,因为它们从未被承诺,一旦你运行这个命令,就没有办法恢复这些变化。
最后,您可以使用 --mixed 重新设置工作树,而无需排序任何变更。
回归承诺
你可以使用 -no-commit,或者你可以使用 -n 告诉 Git 进行逆转,但停止承诺改变。
否则,您可能会通过尝试转换尚未存在的代码来混淆 Git。
首先,什么是头?
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 也。
此分類上一篇
但是,为了完整性,我也想展示这些其他替代解决方案,也可以用来逆转一个承诺(在这个意义上,你创建一个新的承诺,阻止了以前的承诺的变化,就像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> 以来添加的文件。
好吧,回到以前的承诺在Git是相当容易的......
返回,不遵守变更:
git reset --hard <commit>
返回,保持变更:
git reset --soft <commit>
解释:使用 git 重新设置,您可以重新设置到一个特定的状态。
但当你看到区别是使用两个旗帜 - 软和 - 硬,通过默认 git 重新设置使用 - 软旗,但这是一个很好的做法,总是使用旗帜,我解释每个旗帜:
软
默认旗帜如所述,不需要提供它,不改变工作树,但它添加了所有更改的文件准备承诺,所以你回到承诺状态,这些变更到文件失踪。
硬
要小心这个旗帜,它恢复了工作树和跟踪的文件的所有变化,一切都将消失!
我还创建了下面的图像,这可能发生在与Git合作的现实生活中:
此分類上一篇