Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge branch and retain the authorship

Tags:

git

let's say that my colleage 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.

like image 706
Nick Vanderbilt Avatar asked Jul 22 '10 01:07

Nick Vanderbilt


People also ask

What happens to branches after merging?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.

How do I merge two branches in a new one?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

Can I merge the same branch twice?

Merging a branch multiple times into another works fine if there were changes to merge. Save this answer. Show activity on this post. Actually yes you totally can, when you merge again it will bring over any commits that don't exist on production.


2 Answers

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.

like image 93
Joseph Spiros Avatar answered Nov 13 '22 13:11

Joseph Spiros


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 <[email protected]>"
git push origin master

This works as desired and adds the specified author to the merge commit. Simple as that.

like image 38
Afr Avatar answered Nov 13 '22 12:11

Afr