如何 git commit --amend 作为分支基础的提交 [英] How to git commit --amend a commit that's the base of a branch

查看:111
本文介绍了如何 git commit --amend 作为分支基础的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 master/head 分支 foo.我想修改 master/head 并在分支 foo 上获取这些更改.我做了以下事情:

I have branch foo off of master/head. I wanted to ammend the master/head and have these changes picked up on branch foo. I did the following:

git checkout master
git add ...
git commit --amend
git checkout foo
git rebase master

问题是旧的未修改提交在修改后显示为分支 foo 的一部分,并且它重新基于 master.我做了一个 git rebase -i 并删除了旧的提交并且有效,但是有没有更简单/更安全的方法来修改作为分支基础的提交?是的,所有本地提交都没有被推送..

The problem was the old non-amended commit shows up as part of branch foo after the amend, and it got rebased onto master. I did a git rebase -i and deleted the old commit and that worked, but is there an easier/safer way to modify the commit that's the base of a branch? And yes, it's all local commits that haven't been pushed..

推荐答案

你需要做的就是告诉 git 从哪里开始 rebase:你可以这样做

All you needed to do was to tell git where to start the rebase from: You can do this

git rebase --onto master SOMESHA foo

其中 SOMESHA 是旧的主 SHA.那就是如果你的树现在看起来像这样

Where SOMESHA is the old master SHA. That is if your tree now looks like this

* aaaaaa - (master)
| * bbbbbb - (foo)
| * cccccc - (old master)
|/  
* ffffff

你也是

git rebase --onto master cccccc foo

你的树看起来像这样.

* dddddd - (foo)
|
* aaaaaa - (master)
|
* ffffff

旁白:如果您不喜欢使用 SHA 值,我们可以为 cccccc 提交使用不同的名称.

ASIDE: we can use different names for the cccccc commit if you dont like using SHA values.

> git reflog master

aaaaaa master@{0}: reset: moving to something
cccccc master@{1}: commit: change the froozle
ffffff master@{2}: commit: snarf the froozle 

这意味着我们可以将 cccccc 引用为 master@{1}(又名,master 之前的位置)

This means we can reference cccccc as master@{1} (AKA, the previous location of master)

所以我们可以把这个 rebase 写成

So we could write this rebase as

git rebase --onto master master@{1} foo

提交 cccccc 的另一个描述是 foo^(也就是 foo 的父级提交),所以我们也可以把它写成

Another description of the commit cccccc is foo^ (AKA, the parernt commit for foo), so we could also write this as

git rebase --onto master foo^ foo

它们都做完全相同的事情,你可能更喜欢在不同的情况下使用不同的.我通常发现 SHA 易于使用,因为在执行此类操作之前,我经常会拉出我的存储库图.

They all do exactly the same thing, and you may prefer to use different ones in different situations. I usually find the SHA simple to use, as I will often pull up a graph of my repository before performing this kind of operation.

这篇关于如何 git commit --amend 作为分支基础的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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