• 12 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 8/21/19

Handle those mishaps on your local repo like a champ

Log in or subscribe for free to enjoy all this course has to offer!

Git knows we all make mistakes!

Do you know anyone that doesn’t make mistakes? I don’t. Since it's natural to make mistakes, Git is designed to allow you to turn back time.  

Sometimes we don't always think things through!
Sometimes we don't always think things through

No matter what mistake you have made, there is someone else that has done it too. Mistakes happen a lot with large teams on production software. Luckily, Git accounts for such mistakes and has commands that can be used to go back and fix those boo-boos.

We're going to go over some common mistakes that can be fixed pretty easily in Git.  It’s important to keep these tools in your tool belt so you can pull yourself out of potential problems as quickly as possible and maybe even help out a few friends! 😉 Learning this will save you a lot of time because most mistakes can be fixed in a few minutes and a couple of keystrokes. So fear not, let’s go through each section and learn common mistakes.

Unstage a file you accidentally added before a commit

We’ve gone over how to stage and then commit files on your repo.  What if you staged the wrong file and would like to remove it? The solution is easier than you think.

To remove a staged file you don't need, use the following command: 

 git rm --cached <filename>

The result will look something like this:

Git rm can remove a staged file you added by mistake!
Git rm can remove a staged file you added by mistake 

Oops! I committed when I shouldn’t have!

Now let's see how to get rid of a file you accidentally committed. You have a few options. The safest is  git revert

Git revert will take you back to the staged files before your commit.  It is safe because it does not remove the accidental commit from revision history, it just takes you back to the pre-commit stage as an added commit.  So now you’ll have two commits: the mistaken and reverted one (which counts as a new commit). It’s not quite as clean, but it's definitely the safest.  Let's see how it works. 

Let's say you committed a file, and now you want to uncommit it:

git revert HEAD --no-edit 

The no-edit   option means you don't want to add a commit message. Now you will have a commit  adding birds.c  and a commit  reverting birds.c:


Added flowers.c, revert flowers.c, flowers.c is now unstaged

The commit for  added flowers.c  is still there; however, the latest commit reverses it:

Commit for add flowers.c, commit for revert flowers.c
Commit for add flowers.c, commit for revert flowers.c

Another command, git reset, can also be used to unstage a file and undo a commit. Type in  git reset <filename>, and the file will be removed from the staging area.  To remove and commit to staged files, use  git reset --soft HEAD^.

The  git reset  command comes with a lot of warnings; therefore, it's better to use alternate methods like git rm and git revert first. 

Don't worry, I’ll go over all the things you can do with the  git reset  command later in this part.  🙂

Yikes! I Need to change the message in my commit

Sometimes you might be lazy and just type in an "asdf" for your commit message, or maybe just spell something wrong.  That doesn't look very professional, does it? Fortunately, it’s pretty easy to change the last commit message using the following command:

git commit --amend.

Oopsies! I committed to the master branch!

We have talked about branching, and how you must commit to your own branch if you are working on an open source project. What if you forgot to create your own branch, or better yet, forgot to checkout to your branch before you typed in  git commit?  That’s a pretty bad mistake, but luckily you are the only on your local repo. No one is affected by this commit yet.


Imagine you’ve staged your files, and then typed git commit.  Then you realize your commit is in the master branch, instead of your branch.  No worries. This command will take your files back to the staging area: 

git reset --soft HEAD^

As shown below, the command moves the file out of the commit, and back to staged.

Undo a commit to the wrong branch!
Undo a commit to the wrong branch

Now that your files are staged, you can move your working directory to the right branch, and do your commit with the following commands:

git checkout newbranch

git commit
Switch to intended branch and commit
Switch to intended branch and commit

You can do this when you accidentally commit to any wrong branch.

Oh no! I messed up my branch and want to start over

What if you just want to start over on your new branch?

Need a new branch
Goodbye commits

To start over on your current branch means you have to go back a few commits. No problem. Let's take a look at the commit history:

Remove these last three commits.
Remove these last three commits

Now remove the three commits ending in 57c2, f037 and 6373. The final result should move the latest commit to c1c2, which was the original commit on the branch.  To do that, just say how many commits to go back:

git reset HEAD~3  - removes the last three commits from your working directory.  It doesn't delete the commits completely; rather, it moves the HEAD to the commit that you want to be your latest.

git reset HEAD~3

What is that HEAD thing? 

The HEAD is the end or the tip of your branch.  It points to the most recent commit (in blue below) on the branch. With the command above, you can move the HEAD to the commit you want. As you can see, it now points to the commit ending in c1c2:

Success!  The HEAD points to the c1c2 commit
Success! The HEAD points to the c1c2 commit

Doing the reset, and removing the files from the staging area to start over, requires a more dangerous command:  git reset --hard HEAD~3 . I don't recommend using this unless it's necessary. We'll look at how to use it a little later. 😇

Okay!  You have learned some fairly basic commands to get you started on local repository fixes.  Let’s take a look at what happens after you have pushed your changes to the remote repository.

Let’s recap!

  • git rm removes staged files. 

  • git revert undoes a commit, but saves the correction as an additional commit over the mistake. 

  • git reset  has more options to delete files and commits, but is risky to use!

  • git commit -amend changes your commit message.

Example of certificate of achievement
Example of certificate of achievement