gitmergegit-rewrite-history

git proper way to fix topic after branch merge


I'm new to Git and for now I'm only working with a local repository but I will eventually share my work with other developers. I'm trying to figure out what is the proper way of fixing code on a topic branch after it's been merged back to the master branch. Here's what I have:

enter image description here

My topic was implemented in C2 in a topic branch and then merged on the master branch. There's been another commit C5 on master for a different topic. Now I just found an issue in with the feature implemented in C2. Should I fix it in the topic branch as C6 then merge again on the master branch? Or should I just get rid of the topic branch since it's been already merged and just fix it directly in the master branch?

Ideally I would like to keep the C2 and C6 in a single commit so that when we implement remote repository I can push a clean history. But I don't think I can just squash C2 and C6 because of the merge. Is there a way to do this?

What would be considered good practice to fix a topic after it's been merged?


Solution

  • It will be helpful for you to learn about git rebase for managing your local history. For example, consider this history of commands, which I think roughly recreates your situation and and a possible resolution using git rebase -i to rearrange your local history:

    mkdir tmpfoo
    cd tmpfoo/
    git init
    touch bar
    git add .
    git commit -am 'c1'
    git checkout -b topic
    touch baz
    git add .
    git commit -am 'c2'
    git checkout master
    touch foo 
    git add .
    git commit -am 'c3'
    git merge topic
    touch quux
    git add .
    git commit -am 'c5'
    git checkout topic 
    echo b >> baz 
    git commit -am 'c6'
    git checkout master
    git merge topic
    git log --oneline --graph --decorate --all
    *   577f974 (HEAD, master) Merge branch 'topic'
    |\
    | * 6750b0d (topic) c6
    * | 08ebbf2 c5
    * |   0cef647 Merge branch 'topic'
    |\ \
    | |/
    | * f1e6882 c2
    * | 0e19228 c3
    |/
    * 90e6149 c1
    19933  git rebase -i 90e6149
    >>edit the sequence of commits to something like the following:
    pick 0e19228 c3
    pick 08ebbf2 c5
    pick f1e6882 c2
    s 6750b0d c6
    19934  git log --oneline --graph --decorate --all
    * 0e3afe0 (HEAD, master) c2
    * 7611fb9 c5
    * 0e19228 c3
    | * 6750b0d (topic) c6
    | * f1e6882 c2
    |/
    * 90e6149 c1
    git diff ORIG_HEAD # no output here, the rebase didn't change the working tree's state