How to merge branch and retain the authorship
let's say that my col开发者_如何学Pythonleage John has created a branch called 'john'. It has 10 committs by John. When it comes to merging back to master they ask me to do merging.
This is what I do
git checkout -b john origin/john
git rebase master
git checkout master
git merge john --squashed
git add .
git commit -m 'merged branch john'
However now what happens is that it is my id against the merged commit. And later people come asking me why did I change certain part of code.
How do I collapse all the comitts in john branch into one commit such that John is the author. I guess git commit interactive can help but could not quite understand.
The issue here is that git merge --squash
will apply the same changes as a normal merge, but without retaining the merge information. Then, when you commit, it's the same as any commit you make: it's attributed to you. You could change the commit's author information using git commit --author="Original Author <email@server>"
. See git-commit(1) for more information on the --author
switch.
But the question I have is: why are you squashing the merge? Why not just do a non-squashed merge? If anyone does a git blame
, it will be appropriately attributed to the commit by the original author.
You can also --amend
the authorship afterwards if you already did the merge. like that:
git checkout master
git merge my_branch
git commit --amend --author="My Nick <my.adress@email.com>"
git push origin master
This works as desired and adds the specified author to the merge commit. Simple as that.
If you do the following:
git checkout -b john origin/john
git rebase master
git checkout master
git merge --no-ff john # forces a merge commit to be recorded
you'll be able to both both retain the authorship of John's commits and be able to revert the merge by reverting the SHA of the merge commit.
The way I have just done this is by squashing via rebase:
git rebase --onto master -i master remote/branch
Then in the editor, mark all commits "squash". This yields a single one with the original author attached. Though there really is nothing you gain as opposed to copying the original author into --author. I just felt uncomfortable doing that.
For some reason, my HEAD was detached afterwards, so I reattached master to it via:
git checkout -B master HEAD
精彩评论