简体   繁体   English

从master分支中提取其他提交后,Git squash提交

[英]Git squash commits after pulling other commits from master branch

Suppose I have the following commits on my local branch A, which I then push to the remote branch. 假设我在本地分支A上进行了以下提交,然后将其提交到远程分支。

commit 1
commit 2
commit 3
commit 4

Now, I pull from the remote master, and the commit history looks like this - 现在,我从远程主服务器中拉出,提交历史如下所示:

//From branch A
commit 1
commit 2
commit 3
commit 4
//From master
commit 5
commit 6

If I now want to squash commits 2 and 3 using git rebase -i and git push -f, will that rewrite commits 5 and 6 as well? 如果我现在想使用git rebase -i和git push -f压缩提交2和3,那是否也将重写提交5和6? If yes, is there a way I can squash my earlier commits without rewriting the commits I pulled from the master branch? 如果是,是否有一种方法可以压缩以前的提交而无需重写从master分支提取的提交? I'm new to Git, so please excuse me if I'm missing something very basic. 我是Git的新手,所以如果我缺少一些基本的知识,请原谅。

Rebase won't rewrite those commits if you are in the feature branch and rebasing onto master. 如果您在功能分支中并重新定型到母版,则Rebase不会重写那些提交。 If your rebase range happened to run over commits 5 and 6, then they would get copied and modified, but you wouldn't lose anything. 如果您的基准范围碰巧发生在提交5和提交6上,那么它们将被复制和修改,但您不会丢失任何东西。 This is a normal operation, git doesn't change commits in-place, new ones get created with any required changes. 这是正常操作,git不会就地更改提交,而任何必要的更改都会创建新的提交。 Running git rebase -i master while inside your feature branch will copy the unique commits of your feature branch, and then re-apply them on an updated version of your branch. 在功能分支内部运行git rebase -i master将复制功能分支的唯一提交,然后将其重新应用到分支的更新版本中。

Assuming commit 6 is your HEAD, commits 5 and 6 are essentially removed and reappear previous to commit 1 when the base of your branch is updated. 假设提交6是您的HEAD,则在分支的基础更新时,提交5和6本质上将被删除并重新出现在提交1之前。 Then commits 1-4 are replayed according to your instructions. 然后根据您的指示重播提交1-4。

If, however, commit 1 was your HEAD, git rebase -i master will undo commits 1-4, and replay them with your squash instruction, resulting in a new history: 6 <- 5 <- 4 <- 1', where commits 2 and three have been squashed, and 5 and 6 are unaffected. 但是,如果提交1是您的HEAD,则git rebase -i master将撤消提交1-4,并使用壁球指令重播它们,从而产生新的历史记录:6 <-5 <-4 <-1',其中提交2和3已被压扁,而5和6不受影响。

More details and better explanation on rebasing can be found here . 有关重新定级的更多详细信息和更好的解释,请参见此处

If you do what you suggest then content of commits 5 and 6 will remain unchanged (if there will be no rebase conflict), however their hash will be different, because their parents will change. 如果你有什么建议,然后提交的内容 56将保持不变(如果不会有变基冲突),但他们的哈希会有所不同,因为他们的父母会发生变化。 So it won't be commits 5 and 6 anymore, but different one with the same content. 因此不再是提交56 ,而是内容相同的另一个。 Force pushing is ok, when you are sure nobody else is using that remote branch, otherwise you rewrite something he/she already pulled from the remote . 力推是确定的,当你确信没有其他人使用该远程分支,否则你重写东西,他/她已经从拉remote I personally wouldn't do it on remote/master . 我个人不会在remote/master上这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM