简体   繁体   English

在`git format-patch`,`git am`,`git pull`之后,在git历史中提交了两次提交

[英]Double commits in git history after `git format-patch`,`git am`, `git pull`

I develop code on my PC, and I have few computing nodes. 我在我的PC上开发代码,而且我的计算节点很少。

To keep my programs synced I decided to use git. 为了让我的程序保持同步,我决定使用git。 Formerly I used it in a one-way mode to "push-down" changes from PC to computing nodes. 以前我以单向模式使用它来“下推”从PC到计算节点的变化。 However from time to time I encountered small bugs that were specific to computing nodes and fixed it on-site. 但是,我不时遇到特定于计算节点的小错误并在现场修复它。 It was easier to test and develop, than writing on PC and committing a change that might or might not have fixed everything. 测试和开发比在PC上编写并提交可能会或可能不会修复所有内容的更改更容易。

I decided to use git utilities to send the changes back up-stream into main code base. 我决定使用git实用程序将更改重新上传到代码库。

I coded fix. 我编码修复。 Committed it on the computing note, used git format-patch origin . 在计算笔记上使用它,使用git format-patch origin Sent patch to my PC and applied it with git am . 发送补丁到我的电脑并用git am应用它。 Up to this point everything looked pretty sweet. 到目前为止,一切看起来都很甜蜜。

However, when I used git pull on the computing node it pulled the commit with patch from origin again and git log shows two commits with the same comments and authors. 但是,当我在计算节点上使用git pull时,它再次从origin中提取了patch,而git log显示了两个具有相同注释和作者的提交。

Have I used commands wrong, or my workflow is flawed? 我使用了错误的命令,或者我的工作流程有缺陷吗? Or this is acceptable? 或者这是可以接受的? (However it doesn't look good to me). (但它对我来说不好看)。

I worked on master branches in both cases. 在这两种情况下我都在master分公司工作。

git pull computing node: git pull计算节点:

commit 68710f82ddb2b2f191a9c29f088423853032a851  <--- git pull enforced merge
Merge: ce19df4 609b82b
Author: luk32 <luk32@computing.node>
Date:   Fri Dec 13 20:39:28 2013 +0100

    Merge branch 'master' of PC:~/projects/_cmake

commit 609b82bc96f88da956869cec2953e8621cbdcd93  <--- 2nd git pull after git am
Author: luk32 <luk32@computing.node>
Date:   Fri Dec 13 20:35:23 2013 +0100

    Changed to worki with MathGL version 2. Broken version 1 compatibility!

commit ce19df4760519eaf42269461f7bdcf94b65bdc48  <--- on-site fix
Author: luk32 <luk32@computing.node>
Date:   Fri Dec 13 20:35:23 2013 +0100

    Changed to worki with MathGL version 2. Broken version 1 compatibility!

commit ccc5b8a1854e4ca4089cf4c0945baff990288557  <--- after previous git pull
Author: lukas <luk32@PC>
Date:   Mon Jan 16 10:33:11 2012 +0100

    skeleton changed to introduce project stacks

git log on PC looks fine: git log PC看起来很好:

commit 609b82bc96f88da956869cec2953e8621cbdcd93
Author: luk32 <luk32@computring.node>
Date:   Fri Dec 13 20:35:23 2013 +0100

    Changed to worki with MathGL version 2. Broken version 1 compatibility!

commit ccc5b8a1854e4ca4089cf4c0945baff990288557
Author: lukasz kucharski <luk32@PC>
Date:   Mon Jan 16 10:33:11 2012 +0100

    skeleton changed to introduce project stacks

EDIT: computing.node:~/projects/_cmake$ git log --decorate --graph --oneline master 编辑: computing.node:~/projects/_cmake$ git log --decorate --graph --oneline master

*   68710f8 (HEAD, master) Merge branch 'master' of PC:~/projects/_cmake
|\  
| * 609b82b (origin/master, origin/HEAD) Changed to work with MathGL version 2. Broken version 1 compatibility!
| * ccc5b8a skeleton changed to introduce project stacks
* | ce19df4 Changed to work with MathGL version 2. Broken version 1 compatibility!
|/  
* 35d2eaa (origin/pushee) added eigen3 find module
* 39f4937 small bugfixes and slight changes in the reporting of what's going on

After you applied the patch to origin and before you ran git pull , your history looked like this: 将补丁应用到origin并在运行git pull之前,您的历史记录如下所示:

  * 609b82b (origin/master, origin/HEAD) Changed to work with MathGL version 2. Broken version 1 compatibility!
  * ccc5b8a skeleton changed to introduce project stacks
* | ce19df4 (HEAD, master) Changed to work with MathGL version 2. Broken version 1 compatibility!
|/
* 35d2eaa (origin/pushee) added eigen3 find module
* 39f4937 small bugfixes and slight changes in the reporting of what's going on

Your first commit, ce19df4 , has 35d2eaa as its parent. 你的第一个提交, ce19df4 ,有35d2eaa作为其父。 When you applied the patch, you made a second commit, 609b82b , with ccc5b8a as its parent. 当您应用补丁时,您进行了第二次提交, 609b82b ,其中ccc5b8a作为其父级。 Because they have different parents, they are different commits and have different hashes. 因为他们有不同的父母,他们是不同的提交,并有不同的哈希。 The identity of a commit is an agragate of 提交的身份是一个agragate

  • A snapshot of the repository (different in the second commit since the repository now has the changes from ccc5b8a in it). 存储库的快照(在第二次提交中不同,因为存储库现在具有来自ccc5b8a的更改)。
  • The commit message, author, time (the same since you used format-patch and am ). 提交消息,作者,时间(自使用format-patcham以来)。
  • The parent commit (different in the second commit). 父提交(在第二次提交中不同)。

So, what does git pull do? 那么, git pull做什么? I don't teach pull to new git users, because it's confusing. 我不教pull至新的Git用户,因为它是混乱。 It depends upon the branch you are on and only works if it's setup as a tracking branch ( master probably will be, but likely not your other branches unless you set them up yourself). 它取决于你所在的分支,只有当它被设置为跟踪分支时才有效( master可能会,但可能不是你的其他分支,除非你自己设置它们)。 It does two things: 它做了两件事:

  • Fetches remote changes (same as git fetch ). 获取远程更改(与git fetch相同)。
  • Tries to merge the remote branch with your current branch (same as git merge ). 尝试将远程分支与您当前的分支git merge (与git merge相同)。 It only knows what branch to try and merge with if your local branch is set up as a tracking branch. 如果您的本地分支设置为跟踪分支,它只知道尝试和合并的分支。

The merge is what's confusing. merge令人困惑。 It tries to do a fast-forward merge ( read more here ), but can't. 它试图进行快进合并( 在这里阅读更多 ),但不能。 So, instead, it creates a merge commit with your local branch as one parent and the remote branch as another parent and you get this: 所以,相反,它创建一个合并提交,您的本地分支作为一个父,而远程分支作为另一个父,您得到这个:

*   68710f8 (HEAD, master) Merge branch 'master' of PC:~/projects/_cmake
|\
| * 609b82b (origin/master, origin/HEAD) Changed to work with MathGL version 2. Broken version 1 compatibility!
| * ccc5b8a skeleton changed to introduce project stacks
* | ce19df4 Changed to work with MathGL version 2. Broken version 1 compatibility!
|/
* 35d2eaa (origin/pushee) added eigen3 find module
* 39f4937 small bugfixes and slight changes in the reporting of what's going on

While this is a reasonable thing for git merge to do, it's not what you want. 虽然这对git merge来说是合理的,但这不是你想要的。 Who cares that you first made the commit against 35d2eaa ? 谁在乎您是否首先对35d2eaa做出了承诺? You really just want master to point to the same commit as origin/master . 你真的只是希望master指向与origin/master相同的提交。 You can get there now with: 你现在可以到达那里:

git checkout master
git stash # If you have any uncommitted changes
git reset --hard origin/master
git stash pop

And then your history will look like this, just the way you want it. 然后你的历史就像这样,就像你想要的那样。

* 609b82b (HEAD, master, origin/master, origin/HEAD) Changed to work with MathGL version 2. Broken version 1 compatibility!
* ccc5b8a skeleton changed to introduce project stacks
* 35d2eaa (origin/pushee) added eigen3 find module
* 39f4937 small bugfixes and slight changes in the reporting of what's going on

That behavior is correct and expected when upstreaming through patches. 当通过补丁上游时,这种行为是正确的和预期的。 The commit IDs for the commits you did locally the the ones you pulled in from upstream should differ. 您在本地执行的提交的提交ID与从上游提取的提交ID应该不同。 It looks a little odd, but is the it reflects history correctly as you should see in the graphed view of your history. 它看起来有点奇怪,但它正确地反映了历史,正如您在历史的图表视图中所看到的那样。 The changes have been introduced on each branch and won't be deleted from either one. 这些更改已在每个分支上引入,不会从任何一个分支中删除。 Merge never changes existing history you just add more to your branch. 合并永远不会更改您只需向分支添加更多内容的现有历史记录。 If you want to avoid having commits "duplicated" you'd have to change to a 'git rebase' based flow instead of merging upstream into your dev branch. 如果你想避免让提交“重复”,你必须改为基于'git rebase'的流,而不是将上游合并到你的dev分支。

As additional reference, I found this blog article explaining merge vs. rebase which touches the patch problematic as well: http://blog.experimentalworks.net/2009/03/merge-vs-rebase-a-deep-dive-into-the-mysteries-of-revision-control/ 作为额外的参考,我发现这篇博客文章解释了合并与rebase,它也触及了补丁问题: http//blog.experimentalworks.net/2009/03/merge-vs-rebase-a-deep-dive-into-所述-奥秘-的-修订控制/

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

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