Blog

How I Undid My Big Fat Mess in Git

Placeholder Avatar
Rachelle LeQuesne
December 1, 2015

My Adventures as an Apprentice Developer

I am so proud to have won a position with reinteractive as an Apprentice Developer. In my first week I have learned many things. The most useful of which appears in my notes under the title: “How to Undo a Big Fat Mess in Git”. How did this big fat mess come about? You might well ask…

We are using Git Flow. I like it. It makes sense. But, for the first time in my life, I am jumping around on and off branches as well as the usual fetching from and pushing up to GitHub. Somehow, amongst all these goings on, I managed to commit my changes to the develop branch instead of to the feature branch.

In addition, I had recently completed the Git Real course on Code School and was feeling rather pleased at the new things I had learned. So I tried one of them out:

git commit -a -m "Awesome commit message"

Cool! I can now do in one step what I used to do in two. I’m well on my way to becoming a “real developer”! However, I failed to notice that the new files I had created were left behind as I hadn’t yet told git about them.

Because I was on develop, and there had been other commits pushed in the meantime, I had to pull these down before I could push mine up. In hindsight I should have inquired as to why but I was trying to prove that I was capable and could work independently. So I pulled and then pushed.

When the missing files were questioned, I discovered that I couldn’t simply push them up. Depending on the approach I tried to take, git would either complain that I was several commits ahead, or several commits behind. Go figure…

Luckily for me, my team mates are very smart and super-patient. One of the guys did a force push of his copy of the develop branch up to GitHub. At this point, only my local develop has my commit. It is no longer on GitHub.

Next I had to get that commit off develop and onto my feature branch.

The first step was to locate it. We achieved that by running the following command:

git log --pretty="%h - %s" | head -n 5

This shows the last five commits to my develop branch, including those that I pulled down from GitHub earlier. They are in reverse chronological order so it is easy to identify the last commit prior to mine. In this case it was “c08f8c5”.

By running

git reset --mixed c08f8c5

I effectively rolled back the commits to the point just before I did mine. My changes are still there but they are not added or committed anywhere.

Yay! Now I can re-do everything the way it should have been done in the first place:

git checkout feature/my-feature git add . git commit -m "awesome commit message" git push origin feature/my-feature

So, in a nutshell, the problem was solved by:

  • Resetting the develop branch on GitHub to the last state that we wanted
  • Undoing the commits on my local develop
  • Switching to my feature branch and committing them there
  • Finally, pushing to GitHub

I’d like to say that this will never happen again. But with many branches to keep track of, or the pressure of a big deploy looming, I have no doubt that these notes will come in handy again sometime in the future.