gitrebasebranching-and-merginggit-rewrite-history

Add commit to an already-merged branch


This is my project's log:

nubarke@linux0:~/CLionProjects/untitled$ git log --graph --all --oneline 
* 3ee3af6 (HEAD -> master) Rename project to Hard Disk Drive Stress Test
* 36f6c14 Off-by-one error in blkvec.c
*   d23cd85 Merge branch 'randomio'
|\  
| * 61f641f (randomio) Add the random I/O implementation (read&write)
| * 27146b6 Changed hash function signature
| * d6078c8 Add very ugly timespec manipulation functions
| * 88fd131 Add hash routine and test program
| * 7c701ee Add feistel test programs
| * ccf9d4a Remove randwalk "upper bound" capability
| * eee6b9b Add the random walk functions
| * 968ce05 Add opt parse support for sequential/random I/O
|/  
* fc4833c Lower the maximum amount of bad blocks we'll track.

I've already merged the random I/O feature branch into master. Now I've realized that there's a bit more work I'd like to do in the logical context of that branch's effort (let's say, deleting a stray file), so I'd need a new commit after 61f641f.

What's the way to go about it? I'm deliberately looking for solutions that rewrite history; I want the project's history to read linearly as much as reasonable.


Solution

  • First, roll back time so that the merge never happened and you are still on randomio:

    git switch master
    git reset --hard fc4833c
    git switch randomio
    

    Now make your new changes, and add-and-commit. You have now appended a commit to randomio.

    Now merge:

    git switch master
    git merge randomio
    

    OK, great, but there are still the two commits that you made after the merge on master. Restore them:

    git cherry-pick 36f6c14
    git cherry-pick 3ee3af6