jamesdlin
jamesdlin

Reputation: 90125

Is it possible to make an existing git commit appear as a merge?

I have a git tree:

A--B---------E'--F--G (master)
    \       
     C--D--E

E' is a squashed version of C--D--E that was not committed via merge. Ideally it would have been committed as a no-fast-forward merge to look like:

A--B---------E'--F--G (master)
    \       /
     C--D--E

Is there any way to create the E to E' link without rebasing later commits (and changing their commit hashes)? (Primarily I just want the graph to look more accurate.)

(I've read Represent a Git merge in former commits (fake a merge), which is a very similar question, but it doesn't do what I want.)

Upvotes: 2

Views: 106

Answers (2)

ElpieKay
ElpieKay

Reputation: 30898

After reading Git Tools - Replace, I find it possible.

git checkout B
git merge E --no-ff
#let's say the created merge commit is O
git replace E' O

Or done by:

git replace --graft E' B E

git replace creates a ref as .git/refs/replace/E'. It can be pushed to another repository via git push <remote> refs/replace/E', and it should be if you expect the replacement to work in other repositories.

If you don't need it any longer, git replace -d E' will remove the replacement. git push <remote> :refs/replace/E will remove it in the remote repository.

Now git log --oneline --graph --decorate master or gitk master shows the graph you expect. So far as I know, the only shortcoming is that now git show E' or git log -1 E' -p gives no patch but only the data of the merge commit. According to the manual, there are some known bugs, so it's a bit risky to use this feature.

Upvotes: 2

1615903
1615903

Reputation: 34820

No, it is not possible to do that without affecting F and G commits. If you alter the commit's parents, the commit changes, so if E' is changed, then F must also be changed because it's parent is different.

Upvotes: 6

Related Questions