git如何提交 - 准确地工作? [英] How does git commit --amend work, exactly?

查看:143
本文介绍了git如何提交 - 准确地工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过 GIT提交 - 在detached head状态。这个问题要求答案比需要更复杂。我想了解 git commit --amend 在正常HEAD情况下​​是如何工作的。

解决方案

假设您处于干净的工作状态,并且您的回购看起来如下所示: $ b



如果您运行

  git commit --amend 

写一个提交消息,保存并退出编辑器,会发生以下情况:


  1. 您的暂存区 - 如果您尚未执行任何新更改,将与用于创建新提交的 f42c5 相同: 31b8e 。它的父节点将与您正在修改的提交的那些节点相同: f42c5

  2. master 分支引用移动到指向新提交( 31b8e )。

  3. HEAD 参考后面是 master


请注意,修改后的提交( f42c5 )现在无法从您的仓库中的任何参考(因此其图形上的透明样式)无法访问。它仍然存在于你的仓库的对象数据库中,但是当Git运行定期清理时,或者如果你通过运行 git gc (垃圾收集)。


$ b

附录(基于就可以了, f42c5 ,仍然存在于您的仓库中,您可以找到它的提交ID(例如,通过将它从 master 分支中捕获reflog),你仍然可以检查出来。运行

  git checkout master#只是为了确保master是当前分支
git reset --hard f42c5

或者(假设您还没有在 master ,reset master ,否则移动 master 分支引用)

  git checkout master#只是为了确保master是当前分支
git reset --hard master @ {1}

会让您处于以下情况:


但现在,提交 31b8e 将无法访问。





I have seen GIT commit --amend in detached HEAD state. The question requires the answer to be more complex than needs be. I'd like to understand just how git commit --amend works in a normal HEAD situation.

解决方案

Assume that you're in a clean working state and that your repo looks as follows:

If you then run

git commit --amend

write a commit message, save and quit your editor, the following happens:

  1. Your staging area—which, if you haven't staged any new changes, will be identical to commit f42c5—is used to create a new commit: 31b8e. Its parent(s) will be the same as that(those) of the commit you're amending: f42c5.
  2. The master branch reference is moved to point to that new commit (31b8e).
  3. The HEAD reference follows master.

Note that the amended commit (f42c5) is now unreachable from any reference in your repo (hence its "transparent" style on my graph). It still lives in your repository's object database, but will eventually be deleted for good, when Git runs its periodic housekeeping, or if you trigger it explicitly by running git gc (garbage collection).


Addendum (based on Jason Baker's comment): Note that, as long as the amended commit, f42c5, still exists in your repo and you have a way of finding out its commit ID (for example, by fishing it out of the master branch's reflog), you can still check it out. Running

git checkout master # just to be sure that master is the current branch
git reset --hard f42c5

or (assuming you haven't, in the meantime, made any new commit on master, reset master, or otherwise moved the master branch reference)

git checkout master # just to be sure that master is the current branch
git reset --hard master@{1}

would put you in the following situation:

But now, commit 31b8e would become unreachable.


这篇关于git如何提交 - 准确地工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆