It’s Friday the 13th!

Git’s an amazing tool, but it has some sharp edges like any powerful tool. Just like the killer from a horror movie, those sharp edges can cause some problems over and over again.

Rally to the rescue!

If there’s a mistake to be made with git, we’ve made it. We’ve been the curious and terrified idiots who stray from the group, only to be singled out and eliminated by the masked killer. Luckily, we’ve also been the conquering hero who discovers the killer’s weakness and uses it to live on to the next movie.

Killer #1 => Commit to Wrong Branch
So you heard about a production bug and you fixed. The problem is, you accidentally fixed it on a development branch that has work that’s not ready for prime-time yet. There are other commits you don’t want, so you can’t just merge your branch into the release branch.

Weakness: Cherry Pick
What you need to do is cherry pick your commit to the release branch:
git log <em># copy the SHA of the commit you want</em><br> git checkout release_branch<br> git cherry pick <em><SHA_from_log></em><br>

The key here is that you need to revert the commit on your development branch to avoid a merge conflict when you merge to the release branch.

Killer #2 = > Delete the Wrong Branch
You typed this:
git branch

And it showed you about 30 branches. You got pissed that you hadn’t cleaned branches in a while and then typed this:
git branch -D seemingly_innocuous_branch


Weakness: Reflog
The silver bullet is:
git reflog

From there, find the reflog entry that contains the changes you want. Grab the SHA from it, then do this:
git checkout -b new_innocuous_branch_name <em><SHA_from_reflog></em>

Killer #3 = > Smittied the Repo
This is a bad one. You’re working on a feature branch. You’re regularly merging your master branch into it to avoid merge conflicts down the road. You did this:
git checkout feature_branch<br> <em>make a commit to feature_branch</em><br> git merge origin/master

Then you mean to do this:
git push origin HEAD:feature_branch

But you did this instead:
git push origin HEAD:<strong>master</strong>

What just happened is that you pushed a bunch of commits from your feature branch into master instead of the opposite. The weird merge direction will screw up your git timeline.

Weakness: Rebuild the branch
Your local feature branch is still what you want to push to your remote feature branch, so your first step is to do what you originally intended:
git push origin HEAD:feature_branch

Next, you need to un-hork your remote master branch. To do this, you’ll actually delete and recreate your remote master branch:
git fetch<br> git log origin/master <em># copy the last good SHA from before your merge</em><br> git branch -D master <em># delete your local master branch</em><br> git checkout -b master <em><SHA_from_master_log></em><br> git push origin :master <em># delete the remote master branch</em><br> git push origin HEAD:master <em># create a new master branch from HEAD</em><br>

One important note on that last solution is that anyone who rebased against or pulled from master after the merge commit was pushed will need to delete and recreate their local master branch like so:
git fetch<br> git branch -D master<br> git checkout -b master origin/master

Git’s an awesome tool, but using it can be a lot like using a chainsaw: there are ways you can hack off your own foot if you’re not careful. Hopefully this post will help you recover from some of the common and seemingly destructive mistakes.

Request a Call

Looking for support?

Send Us Your Feedback

Provide us with some information about yourself and we'll be in touch soon. * Required Field