As a part of gap intelligence's Prod/Dev team, I work on many large projects where there are many other programmers involved. While doing so it's wise to use a version control system and Git is one of the ones that I frequently utilize. Due to the complexity of Git, it's VERY easy to make mistakes. I wanted to go over some common mistakes I made while using Git and how I solved them. So, let’s get started.

www.perforce.com

Mistake in the name of the branch

We can rename a branch same way that we rename a file with the mv command: by moving it to a new location with the correct name:

git branch -m name-with-mistake correct-name

Pushed that branch already? Then we just need to delete the old branch from the remote and push up the new one:

git push origin –delete name-with-mistake
git push origin correct-name

Mistake in the last commit message

The command below will open your configured editor where you can change the last commit message:

git commit –amend

You can also set a new message directly without the editor:

git commit –amend -m "New Message"

Forget to add a file? Feel free to add it and then change the last commit

git add forgotten.file
git commit –amend

Mistake in another commit message

Interactive rebasing comes in handy when we want to change the commit message prior to the last commit. 

git rebase –interactive

If you didn't specify any tracking information for this branch then you will have to add upstream and remote branch information:

git rebase –interactive origin branch

Running git rebase will open your configured editor and present you with the following menu:

pick 8a20121 First commit
pick 22dcc45 Second commit with a typo
pick 56d8752 Last commit
# Rebase fcb7d7c.. 56d8752 onto fcb7d7c
#
# Commands: # p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

There you will see a list of local commits on top, followed by an explanation of the available commands. Pick a commit or commits that you want to update, change pick to reword (or r for short), and you will be taken to a new view where you can edit the message.

Interactive rebases offer a lot more than simple commit message change: you can remove commits by deleting them from the list, edit, reorder, and squash them.

Undo local changes

Reverting the file to its original state can be the fastest and easiest solution when you are done playing around with the code. Command below allows you to reverta  specific file or files and also works with directories:

git checkout — first.file second.file lib bin

Undo local commits

Resetting code comes in handy when you want to undo your local commits. Run command below to reset last 2 commits, for example. This will keep the changes that you did within those commits:

git reset HEAD~2

Run this command when you need to reset commits and discard all the changes. Be careful though, all your changes will be lost:

git reset –hard HEAD~2

Undo pushed commits

A faulty commit does occasionaly make it into the remote repository and git also has a way to revert specific commit by the ID: 

git revert c761f5c

Run this command if you need to revert the second to the last commit

git revert HEAD^

You can also revert a whole range of commits

git revert develop~4..develop~2

In case you don’t want to create additional revert commits but only apply the necessary changes to your working tree, you can use the --no-commit(or -n for short):

git revert -n HEAD

I went over some of the ways to deal with gitfails that arise when I work with git. I hope you will not face any of these situations and will not need these techniques. But if something does go wrong, now have the tools you need to do something about it! 

For more than 16 years, gap intelligence has served manufacturers and sellers by providing world-class services monitoring, reporting, and analyzing the 4Ps: prices, promotions, placements, and products. Email us at info@gapintelligence.com or call us at 619-574-1100 to learn more.