Git can be your best friend as a developer, but it can also be the source of quite a lot of errors in your workflow. Here I will show you how to fix 6 common Git mistakes.
1. You Misspelled the Last Commit Message
You realize you just pushed a commit where you misspelled something like
"Fix lanting page" and you don’t want to sound dumb in front of your colleagues.
git commit --amend
git commit --amend -m "Fix landing page"
This command will let you combine any staged changes with your last commit. If nothing is staged, you can use it to update the commit message like in this case, either via a new CLI prompt, as the first command will show, or directly, as the second command will do.
2. You Forgot a File in Your Last Commit
You can use
git commit --amend on this occasion too, supposing, for example, you forgot a file in your last commit.
Just add it to the staging area and then run the command again.
git add landingpage.js git commit --amend
3. You Pushed a Commit That Has a Problem in It
You realized you pushed a commit with some code you thought was good to go, but it was actually not that acceptable.
You can fix this by running:
git revert <SHA>
This command is going to create an opposite commit of the one you got wrong. Kind of like throwing water on a fire. Anything removed in the old commit will be added in the new reverted one, and similarly, previously added content will be cut.
4. You Want to Cancel Some Uncommitted Code
When you realize you are writing completely useless stuff, you want to get rid of that as soon as possible.
If nothing has been committed yet, you can do that by running:
git checkout --<bad filename>
This command will get files in your working directory to a state previously known to Git. You can provide it with a branch name, folder name, or even a specific
SHA you want to get back to. Be careful however before using it, as this command will completely cancel everything you undo, and you won’t be able to get it back ever again.
5. You Want to Cancel Some Unpushed Commits
You start to write part of a feature and realize everything is garbage again.
You want to get rid of any footprints you left, you can do that by running:
git reset <last good SHA>
git reset --hard <last good SHA>
This command will rewind your history to the specified
SHA. In the first case though, the working directory will be preserved, meaning your commits will be gone but still on disk.
In the second case, even those changes you performed will be removed.
6. You Pushed Some Commits on the Master Branch
So you crazily start to type your code for a new feature, commit some code proudly, only to realize you pushed those changes on the master branch.
Note: Make sure you commit or stash your changes first to save them, otherwise everything will be lost.
You can roll back all those changes to a new branch with the following three commands:
git branch future-brunch git reset HEAD~ --hard git checkout future-brunch
These three commands will create a new branch before rolling back master to where it was before your changes, and then actually moving on the branch you wished you did the changes initially.
Common Git errors like the one you saw can be a big obstacle in your daily peace. Now you should have a little toolkit on how to tackle them easily and fast. As always, thanks for the time spent reading the 2 AM blogs, and don’t forget to use the comments section down below for your opinion!