Mastering Git: 5 Common Mistakes and How to Avoid Them

Totally inspired by: https://dangitgit.com/

Omar Valdez
8 min readMar 17

--

Photo by Roman Synkevych 🇺🇦 on Unsplash

In case you’ve already worked with Git but still struggle with it, then this article is for you. To be honest, I would have loved to come across something like this after learning the basics of Git.

Most likely you’ve heard of Git if you’re involved with programming, but in case you have no idea what Git is for, let me explain.

Back in the old days, when you wrote a project for school, you might have saved multiple versions of the same file with different names, such as “finalessay_v1.doc”, “finalessay_v2.doc”, and so on. This was a way to keep track of changes you made to the document over time.

Photo by John Schnobrich on Unsplash

But there were a few problems with this approach. First, it was easy to lose track of which version was the most recent or which changes were made in which version. Second, if you wanted to experiment with different ideas, you would need to create a new version of the file, which could quickly become messy and confusing.

This is where version control systems like Git come in.

Git allows you to keep track of changes to your code or documents in a more organized way. With Git, you can create a repository for your project, and every time you make a change, you can save it as a “commit” with a description of what was changed.

With Git, you’re able to keep track of changes to a document with multiple users working on a project.

Photo by Roman Synkevych 🇺🇦 on Unsplash

Enough with explanations. As you already noticed with the title of this article, using Git can be difficult.

First and foremost, I cannot recommend enough this freeCodeCamp.org tutorial for learning the basics:

Thanks freeCodeCamp.org !!

However, even with this tutorial, I know that at some point, you may mess something up, and that’s okay.

Photo by Ian Taylor on Unsplash

How do you stop messing up so much?

Fortunately, after stumbling upon with this article, I was able to identify my five common mistakes and how to avoid them.

The important thing is to learn from those mistakes and take steps to prevent them in the future.

With practice and persistence, you’ll become a Git expert in no time!

#1 Dangit, I did something terribly wrong, please tell me Git has a magical time machine!?!

Fortunately, there’s a way to do it. You can use git reflog to fix your disaster.

You can recover things you accidentally deleted, you can recover things after breaking something, you can recover things after a bad “merge,” you can travel back in time to a specific moment when things were working.

git reflog is a lifesaver, definitely.

# Create directory and initialize Git repository
mkdir dangit_one
cd dangit_one
git init

# Create new file and commit it
echo "First commit" > file1.txt
git add file1.txt
git commit -m "First commit"

# Modify the file and commit changes
echo "Second commit" >> file1.txt
git add file1.txt
git commit -m "Second commit"

# Use git reflog to see commit history
git reflog

# Use git reset to go back to previous commit
git reset HEAD@{1}

Now your working directory is reset to the state before the mistaken commit.

Keep in mind that if you have uncommitted changes in your working directory, you may need to stash them first before performing the git reset command to avoid conflicts.

By the way, if you are wondering why the git reset didn’t modify the file by changing the “Second commit” comment, you can use git reset --hard

Please consider that using git reset --hard is a destructive operation and you will lose commit changes. With the next mistake, you’ll understand what I’m talking about.

#2 Dangit, I need to undo a commit from like 5 commits ago!

Suddenly you realize that everything you’ve worked on is wrong, but you know there’s a point in the timeline of your history where you realize that you can revert back to where you want.

Use git revert

# Create directory and initialize Git repository
mkdir dangit_two
cd dangit_two
git init

# Create new file and commit it
echo "First commit" > file2.txt
git add file2.txt
git commit -m "First commit"

# Modify the file and commit changes
echo "Second commit" >> file2.txt
git add file2.txt
git commit -m "Second commit"

# Modify the file again and commit the changes
echo "Third commit" >> file2.txt
git add file2.txt
git commit -m "Third commit"

# Modify the file one more time and commit the changes
echo "Fourth commit" >> file2.txt
git add file2.txt
git commit -m "Fourth commit"

# Check the commit history
git log --oneline

# Output
# (HEAD -> main) (hash4) Fourth commit
# (hash3) Third commit
# (hash2) Second commit
# (hash1) First commit

# If you want to undo changes by the "Third commit"
# This will open an editor with a default commit message
git revert hash3

# Check changes in the working directory
cat file2.txt

# Output
# First commit
# Second commit
# Fourth commit

NOTE: In case you get a merge conflict between the changes introduced in the “Third commit”, you’ll get a prompt message saying Git was unable to automatically resolve the conflict while reverting the commit.

For the sake of this example, do the changes between the conflict markers and remove “Third commit”

Once you’ve done that, do the following:

# Stage the resolved file
git add file2.txt

# Continue with the revert process
git revert --continue

Do you wonder why the #1 Dangit and this one are similar? Remember I told you git reset hard can be destructive?

Explaining the differences will make you understand:

git reset

  • This command moves the HEAD and the current branch pointer to a specified commit
  • It can be used to remove one or more commits from the branch history
  • It does not create new commits

git revert

  • This command creates a new commit that undoes the changes
  • It doesn’t modify the existing commit history, but adds a new commit that reverses the specified commit’s changes
  • It’s useful when collaborating with others or when working in a public repository because it doesn’t rewrite history

So in summary, if you want to undo changes and maintain the commit history (especially when working with others), use git revert. If you want to remove commits from history and don’t mind rewriting history, use git reset.

#3 Dangit, I committed and immediately realized I needed to make one small change!

Yes, this often happens. You think you’re done, but suddenly you realize there’s a small thing you missed and want to fix it. How to do it?

With git commit --amend

Quoting the article:

Warning: You should never fix commits that have been pushed to public/shared branches. Only fix commits that exist in your local copy, or you’ll be in trouble.

# Create directory and initialize Git repository
mkdir dangit_three
cd dangit_three
git init

# Create new file and commit it
echo "First commit" > file3.txt
git add file3.txt
git commit -m "First commit"

# Modify the file and commit changes
echo "Second commit" >> file3.txt
git add file3.txt
git commit -m "Second commit"

# If you need to make a small change and want to include it in the last commit
echo "Small change" >> file3.txt
git add file3.txt

# Amend the last commit
git commit --amend

Now you can see the last commit “Second commit” now includes the “Small change” you made.

#4 Dangit, I accidentally committed something to master that should have been on a brand new branch!

Does this situation sound familiar to you? Did you think you were working on a new branch but guess what? The commit was already pointing to master! Once again, quoting the article:

Note: this does not work if you have already pushed to a public/shared branch, and if you have tried something else before, you may need to do git reset HEAD@{number-of-commits-back} instead of HEAD~. Endless sadness. Also, many people suggested an incredible way to make this shorter that I didn’t know. Thanks, everyone.

# Create directory and initialize Git repository
mkdir dangit_four
cd dangit_four
git init

# Create new file and commit it
echo "First commit" > file4.txt
git add file4.txt
git commit -m "First commit"

# Make a change and commit in main branch
echo "Second commit" >> file4.txt
git add file4.txt
git commit -m "Second commit"

# Create new branch
git branch the-best-branch

# Remove the last commit from 'main' branch
git reset HEAD~ --hard

# Check out the new branch
git switch the-best-branch

Take into consideration that this method must only be used if you haven’t pushed the commit to a public or shared branch.

#5 Forget this noise, I give up

Have you tried everything you can think of in Git? It’s not the best option, but it works.

# This is exactly as it says in dangit.com
cd ..
sudo rm -r stupid-git-repo-dir
git clone https://some.github.url/stupid-git-repo-dir.git
cd stupid-git-repo-dir

Really, it’s not the best option, and it’s not a good practice, but in case you’ve exhausted all of your possible options, go ahead and do this.

Photo by david Griffiths on Unsplash

Git can be complicated because making mistakes is quite easy. The warning here is that anyone who knows Git has their methods of dealing with it. You can find more than one solution to solve any particular problem you’re dealing with, and that’s totally fine.

Explaining Git in a simple way is, and always will be, the best way to explain it to someone who is just starting to use it.

In reality, using apples and pears as a metaphor is a well-established method for explaining almost anything in life. It has remained the top choice for simplifying complex concepts and making them easy to understand.

And remember, if you have problems with Git, feel free to come back to this article as many times as you want.

--

--

Omar Valdez

Data Enthusiast | YouTube @valdezdata | #mex #programming #analytics #engineering