8

I need to insert a commit in the master branch of my git repository whist preserving the subsequent merges and commits.

I currently have something like this

A--B--C--D--E--F     master
       \     \  
        G--H  I--J   branches

and need to insert a commit K such that the new structure becomes

A--B--K              master
    \
     C--D--E--F      new branch
      \     \  
       G--H  I--J    old branches

I'm not even sure if this is possible. Any ideas?

3 Answers 3

9
git checkout master
git branch new_branch # copy current branch master to new_branch
git reset --hard B    # now master points to B
(hack, hack, hack)
git commit -m K       # K on B in master
Sign up to request clarification or add additional context in comments.

Comments

2

Rename the "master" branch to "new branch". Then checkout the commit B, start a new branch called "master" from there, and make your changes. Something like follows should do it (not tested).

git branch -m master new_branch
git branch master B
git checkout master

2 Comments

Thank you for your answer. This results in commit A being a member of "new branch" and not "master". The history for master then becomes "B--K" rather than the intended "A--B--K". This same structure could be achieved (with differing branch names) by simply branching from B and committing K.
@Jason I know I'm 4 years late to the party here, and your account seems to be gone; but commit A would be a member of both "new_branch" and "master". It is a tree, and many branches can share the same ancestors. Of course, it might be notated one way or the other (e.g. by git log --graph --oneline --all), but that's just a notation - git log master and git log new_branch will show both have A. It's also true of your suggestion for creating this same structure.
0
# git checkout -b new-master B

Now make your changes for K, commit them and voilá, there’s the structure you want. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.