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.
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:
Pushed that branch already? Then we just need to delete the old branch from the remote and push up the new one:
Mistake in the last commit message
The command below will open your configured editor where you can change the last commit message:
You can also set a new message directly without the editor:
Forget to add a file? Feel free to add it and then change the last commit
Mistake in another commit message
Interactive rebasing comes in handy when we want to change the commit message prior to the last commit.
If you didn't specify any tracking information for this branch then you will have to add upstream and remote branch information:
Running git rebase will open your configured editor and present you with the following menu:
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
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:
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:
Run this command when you need to reset commits and discard all the changes. Be careful though, all your changes will be lost:
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:
Run this command if you need to revert the second to the last commit
You can also revert a whole range of commits
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
-n for short):
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 firstname.lastname@example.org or call us at 619-574-1100 to learn more.