Git Branch Clean-up Adventures
Join the DZone community and get the full member experience.Join For Free
git is magic, but sometimes it drives my crazy with all that power causing strange situations and clean-up work. the magic of git is that it is (nearly?) always possible to clean up the mess and get back to a good state. the problem is that there are a lot of commands to master. this post is a story about cleaning up a pull request to prepare it for merging into master.
the pull request contains functionality that i do want to merge, but also workarounds for cultural sensitive unit tests that are obsolete and should not be merged. there’s also a rebase that has been done in the wrong way and a merge from master of the forked repo – which is no longer in sync with the main repo’s master branch.
keeping a backup
before doing anything at all i want to make sure that i don’t screw up more. the git way to do that is to make a separate branch. remember that git commits are immutable. although git permits rewriting of history, the actual commits are never changed. changes to a branch are done by creating a new series of commits and moving the branch pointer. that means that if i do my fixups on a separate branch, only that branch will be affected and the original pull request branch will still be intact.
ps c:\git\authservices> git checkout 39signval switched to branch '39signval' ps c:\git\authservices> git checkout -b 39signvalfixed switched to a new branch '39signvalfixed' ps c:\git\authservices>
changing merge to rebase
looking at the revision graph, there have been two branches that are now merged with a merge commit. one of the branches contains a commit that i want to remove entirely. it would also look better if the branches were rebased instead of merged. the first step is to get branches for each of the parents. every commit in git can have several parents. they are reached as
, so what i want is one branch each for
. that is done by using
git reset --hard
to move the current branch pointer to another commit.
ps c:\git\authservices> git reset --hard head^2 head is now at a968ce6 fixed tests to be more culture robust ps c:\git\authservices> git checkout 39signval switched to branch '39signval' ps c:\git\authservices> git checkout -b 39signvalbase switched to a new branch '39signvalbase' ps c:\git\authservices> git reset --hard head^1 head is now at 63168ca rebased from original master ps c:\git\authservices>
the result is that i’ve moved the
branch pointer back and created a new
branch pointer. this enable individual clean-up of each of the two branches, before rebasing them.
to be honest, things didn’t go as smooth as i show here when i did all this work. several times i went down the wrong path, but i only present the way i ended up doing here (don’t want to bore you with all my errors). one thing i found out when working with all this is the importance to check that every step compiles and the tests run before going on with the next operation. at first i tried a one-step rebase, but the resulting conflicts were too hard to sort out.
removing a commit from the base branch
branch contains two commits. the first one is an incorrect fix of the cultural sensitiveness of the unit tests. i want to drop that commit entirely. that is done with an interactive rebase.
ps c:\git\authservices> git rebase -i head~2 successfully rebased and updated refs/heads/39signvalbase. ps c:\git\authservices>
head~2 means that i want to go back to the grand-parent of the current head. the
is short for interactive, which makes git bring up my default editor with a text file where i can mess around with the commits affected by the rebase.
#pick 9b30d26 fixed a unit test pick 63168ca rebased from original master
i’ve commented out the commit i want to remove. when i hit save and exit git catches the change and perform the rebase. the resulting branch (
) contains some issues: an xml comment is incorrect and one test is not running (and it doesn’t test some new functionality). that is fixed by an ordinary commit to that branch.
before moving on i also review the code of
and fix some issues, so that
is no in proper shape.
in the original branch sent as a pull request the final commit was a merge. now when the base branch is cleaned up, it is now time to replicate that, but not using merge. instead i’ll rebase the latter branch on top of the base branch. that will give me one nice straight line in the commit history.
before rebasing there’s a small issue to fix though: the
branch also contains the “fixed a unit test” commit that i previously removed from the
branch. that commit is dropped with
in exactly the same way as for the
then it’s time for the rebase, to stitch those branches together. i imagine a rebase much like cutting a branch off a tree and then stitching it back somewhere else. when rebasing, it is the branch you’re sitting on that is sawed off (and that is perfectly fine).
ps c:\git\authservices> git checkout 39signvalfixed switched to branch '39signvalfixed' ps c:\git\authservices>git rebase 39signvalbase
git now finds the latest common ancestor of the current branch (
) and the given branch (
). the commits of the current branch are then replayed on top of the base branch. in this case i got a lot of conflicts at each step. this is one of those cases where git really shines over other version control systems i’ve used; git drops back to the console when there is a conflict. i can use
to resolve the conflicts, but also fire up my ide and build the solution at each step as well as running the tests. fixing the compilation errors at each conflict resolution step is so much easier than getting a giant merge conflict from all commits to sort out. when one conflict is resolved,
git rebase --continue
is used to continue the rebase.
rebase was wasted work
when the rebase is done the result can be inspected: whitespace changes only. there were no new changes in the
branch, they were just a result of a previous failed rebase/merge attempt. so let’s get rid of that wasted work and drop the the
ps c:\git\authservices> git reset --hard head~2 head is now at aa7e538 cleanups to saml2response ps c:\git\authservices> git branch -d 39signvalbase deleted branch 39signvalbase (was aa7e538). ps c:\git\authservices>
now everything is fine – there’s a
branch that is in a clean up state. it’s just one more thing to do before sending this off as a pull request.
rebasing on master
any pull request should be rebased on the current
before being sent. that way the maintainer won’t have any conflicts to sort out when merging the pull request. i’m still on the
branch – which is the one i want to cut off and stitch back on top of
(remember, in git the correct way is to saw of the branch you’re sitting on).
ps c:\git\authservices> git rebase master first, rewinding head to replay your work on top of it... applying: rebased from original master applying: updated test xmldocumentextensions_sign applying: minor fixes to saml2responsetests. applying: cleanups to saml2response ps c:\git\authservices>
the result is finally something that is easy to merge to master. in fact: a merge now will just move the master branch pointer to the node pointed at by
i certainly learnt a lot of git work by cleaning up this pull request. i hope you did too by reading about my adventures.
but there’s a more important lesson to be learned from this. the reason that we had this mess in the first place: the original work had been done on the
branch of the fork. that made a rebase hard to do.
never, ever work on master. always create a feature branch.
Published at DZone with permission of Anders Abel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.