amending a commit to add a file, cause orphan branches

59 Views Asked by At

Based on the following git scenario:

 C0 (master)
   \
    \
     C1-------------C5 (develop)
      \            /
       \          /
        C2--C3--C4 (feature)

I have a new uncommitted file that I want to add into master as part of commit C0. So what I did is to checkout master and then amend in order to add the file into C0.

Everything looks find but the problem I'm experiencing is that C0 hash changed and caused C1 to lose the reference.

 C0 (master)


     C1-------------C5 (develop)
      \            /
       \          /
        C2--C3--C4 (feature)

I'm basically new to git and I'm sure I did something wrong. I would appreciate any help on how to "link" C0 and C1 again.

Probably the best practice, should have been to create a new commit (C6) in master and then merge in develop, but I want to know if there is a way to solve this type of issues for future cases where, maybe "amending" into current commit is the best option.

1

There are 1 best solutions below

0
Nitsan Avni On

assuming that at least one of the branches master, develop and feature are used by other people too, this kind of re-writing of git commit history should be avoided, because other's work is already based on the exact commit shared history (commit graph).

amending a commit is a history re-writing action.

for the sake of completeness, and in the case that all of these branches are private to your local repository, the way to amend commit C0 without loss of graph connectivity is to use a proper git history re-writing tool e.g. git-filter-repo

simpler history (more linear) re-writings can be achieved using git rebase -i