5.8. Git Recipes

5.8. Git Recipes

This recipe involves a situation where you have 4 commits, with 1,2, and 4 being related and commit 3 just in the middle. Ideally, before you submit this, you would like to combine 1,2, and 4 into a single commit and then have commit 3 and a second, isolated commit.

Run the following to prime you repository with this scenario:

cd ~
mkdir git-test
cd git-test
git init
touch a.txt
git add a.txt
git-commit -m "Adding one file"
touch b.txt
git add b.txt
git-commit -m "Adding another file"
touch different.txt
git add different.txt
git-commit -m "Commiting something entirely different"
touch c.txt
git add c.txt
git-commit -m "Adding yet another file"

Correcting the problem:

  1. Look at the log history and get the id of the initial commit revision: git log

  2. Start a working branch and check it out to serve as the cleanup branch, starting at the initial commit: git checkout -b cleanup [REVISION]

  3. Confirm you are now working on the new branch: git status

  4. Confirm the log history only contains the initial commit: git log

  5. Now, since we want to add two more things to this commit and re-commit it. First, you need to get the commit revisions from the master branch: git log master

  6. Now, you need to cherry pick commits 2 and 4 without commiting:

    git-cherry-pick -n [REVISION 2]
    git-cherry-pick -n [REVISION 4]
    
    

  7. Now you need to revise the current commit comment to include what was done in revisions 2 and 4 git-commit --revise

  8. At this point, you have combined 1,2, and 4 into a single commit. The last step is to cherry pick and commit revision 3: git-cherry-pick [REVISION 3]

  9. Now you can push the changes from this branch to the desired public repository.