How to declare a git branch as merged without adopting the changes in it?

Viewed 208

Suppose we have two git branches, a and b (say they're both local to keep things simple). Branch a is mine to work with, branch b isn't; I can't touch it.

I want to "declare" or "mark" a merge of b into a, without actually making any changes to the code in branch a. I don't mind making an empty commit to signify the merge. The motivation is that redundant work has happened on branch b which is subsumed by the work done on a since they diverged.

Assuming this is a "legitimate" thing to do - what's the idiomatic way to do it?

3 Answers

I think it can be easily made like this:

git commit-tree -p a -p b -m "not pulling b changes" a^{tree}

With this, you are asking git to create a new revision object that has a and b as parents, that has as tree as its tree (so no differences against a) that has the comment that I provided there (adjust at will). The object will be created and the id will be printed on the stdout. You can check it to make sure it is what you want:

git diff that-id a
git log that-id
gitk that-id

There should be no differences. Then you can move a if you liked it:

git branch -f a that-id

Here's how I do it now:

  1. git br a
  2. git merge --no-commit b (or git pull --no-commit remote-name-here b if b is remote)
  3. Unadding everything
  4. Editing the commit conflicts to only keep the a version
  5. git commit and changing the comment to say that I'm marking-as-merged without adopting changes.

There is no idiomatic way to do it, because it is not a common use case. Assuming, that you just don't want to have unmerged branches, there are some workarounds, that might work for you, depending on your circumstances:

  1. Delete the branch
  2. Merge the branch and then revert the merge commit (it is then merged, but the changes are not present)
  3. Revert all commits on the branch and then merge it.
Related