Now Up To  80% OFF  GitKraken Pro
Upgrade to Save
Search
Close this search box.

Git Blog

Releasing the Power of Git

7 git common mistakes

7 (Deadly) Common Git Mistakes and How to Fix Them

Git is a version control system that is used by a vast majority of developers around the world. Developed by the Linux mastermind Linus Torvalds, Git has been available to the public since 2005 and has made developers’ lives much easier. 

With Git, working in teams and collaborating over files is much easier, and it helps enable faster development of software products. It’s no longer mandatory for each programmer on the team to work on different files; they can work on the same files at the same time and, in the end, Git merge their changes without affecting others. Git helps developers to track versions of their codebase and collaborate with different team members effectively for an error-free delivery of software products. 

Of course, collaborating in these situations, Git merge conflicts can occur. Using robust Git tools, like GitKraken Client, can take your team’s Git workflow to the next level. Features like predictive merge conflict alerts notify you when two team members are working on the same file at the same time so you can preemptively avoid conflicts before they happen. 

While Git is quite common, it has many things under the hood–it’s not all just about staging, committing, and pushing changes to remote Git repositories. Git is actually quite powerful, but many developers don’t know how to take advantage of all the features it has on offer. 

Many people fear making mistakes in Git because they don’t understand it well. Everyday, developers make a large number of changes to the codebase, so it can become hard to track down each change and Git commit them effectively. 

It can also be easy to make mistakes when working on multiple tasks at once. If you’re a developer who makes common mistakes in Git, this article will teach you how to fix them without panic. On the other hand, if you’re an engineering manager looking to get remote developers for hire, this is a great resource to share with your team so they understand these mistakes and solutions. 

1. Accidentally Deleting Files

Cartoon depicting woman deleting files from a computer tablet

Accidentally deleting a file in Git is quite a common mistake. When working on feature upgrades, for example, developers often think that some files aren’t needed, and in this anticipation, they delete a file before its needs expire. 

If you’ve accidentally deleted a file in Git, you might think that you’ve made an irreparable mistake. But don’t worry; Git is quite generous with files. It always keeps track of the files that were added or deleted in the repository. This means you can get your file back with just one Git command. Or, you can use the magical Undo button available in GitKraken Client….

“@GitKraken just saved me after a discard all.” – @QiMata

For a better understanding, let’s say, for example, that you’ve deleted the 404.html file from your Git repository. To get the file back, you can type the below command into your terminal of choice. 

git checkout HEAD 404.html

Once you’ve run the above Git checkout command, you will get your repository’s latest committed version of the 404.html file. If you check the repository’s status with the Git status command, you’ll see that the 404.html is no longer under deleted files. It’s now available for modifications in your code editor.

GitKraken Client offers users both a CLI and GUI, so developers can choose the best tools for their workflow. No more need for checking your repo’s status manually; GitKraken Client’s graph automatically updates as actions are completed so you get immediate verification that things are running as expected.  

2. Pushing Unfinished Code

Cartoon depicting woman managing multiple files of different types from a laptop computer

As a developer, you often end up working on important features that need extremely fast delivery, or production bugs that are haunting users and need to be solved quickly. In such cases of high pressure, you may forget to switch your working Git branches and end up working and committing files to the main branch. 

Pushing unfinished code to the remote main branch can cause havoc with the CI/CD and deployment sections of your team’s workflow. 

If you’ve recently pushed your code to a remote main branch, you can revert the changes if you have the previous version of the remote branch synced with your local branch. You can get the older version of the remote branch by using the below command. 

git reset - -hard <remote_branch>/<branch>@{1}

The Git reset command used in this context will reset and update your remote branch with your local history’s last working version of the main branch. If you want to protect this branch from such direct commits, you can also use the below setting in your remote Git repository and sync it with local. 

git config --system receive.denyNonFastForwards true

3. Poor Git Commit Messages

Cartoon depicts woman writing code on a laptop

Writing good commit messages is essential to having a better codebase that everyone can understand. Commit messages should be self-explanatory to anyone who looks at the code. Messages like “fixed,” “edited,” etc., without any additional context don’t serve any purpose. 

Many developers make the mistake of adding poor commit messages while working rapidly, and in the end, they wish they would have written a better commit message, not just for their team members, but also for themselves. 

The good news is that Git allows you to edit commit messages whenever you want. To edit a commit message, use the following Git commit amend command: 

git commit --amend

After typing the above command into your terminal, press enter. This will open a text editor where you can easily edit the last commit message.

4. Accidentally Deleting an Entire Git Branch

Cartoon depicts person working on multiple coding projects on a laptop computer

Working on a branch for a long time and, in the end, accidentally deleting it without merging it can sound like the biggest horror for developers. But it is not as big of a deal as people think. You can recover that branch with just a few commands from the terminal. 

To perfectly recover the branch, you first need to find the commit from where you switched to that deleted branch. To do that, you can use the Git reflog command

to get all the journal entries of commits and details. Once you’ve found the commit, you can use the below command to get your deleted branch back so you can merge and push it to remote branches. 

git checkout -b <branch-name> <commit-id>

In the above command, branch-name is the branch name that you have deleted, and commit-id is the commit that you’ve determined from your Git reflog results. 

5. Bad Git Commits

Image of person coding on a laptop computer with large monitor

Making a commit with errors is a common Git mistake, but it can also be pretty devastating. Your commit could introduce problems into your codebase, for example, or conflict with another change. 

These mistakes can cause unmeasurable damage to production builds if the errors make it past the deployment stages. But if you identify the mistake early, you can take steps to remedy it.

Fix bad commits by reverting to old commits using the following Git revert commit command:

git revert <commit-id>

In the above command, the commit-id is the commit id for the old commit which has the better and more stable version of code. 

6. Too Many Git Commits

Cartoon depicts laptop computer screen with lines of code

Git is used to keeping track of changes in the codebase incrementally, but sometimes people end up making too many commits. This typically happens when you’re experimenting with something new, and as things start to work, you commit incrementally throughout the process.

In the end, when the experiment is successful, and you’re ready to commit to the main branch, you may be left with numerous commits with not-so-explanatory commit messages. These commits will make it hard for other developers to understand your code.

In such cases, you can resort to squashing commits from your terminal and then pushing your changes to the remote. Below is the process to Git squash multiple commits down to one. 

  1. First, Git checkout to the branch you want to merge using the following command: 

git checkout <branch-name> 

  1. Next, use the following command: 

git merge - -squash <branch-name-to-be-merged>

  1. Once you’ve performed the above two commands, you’ll have some merge conflicts that need to be solved. 

7. Forgot To Add the Files

Cartoon depicts person managing numerous files from a laptop computer

Forgetting to add a file is another common Git mistake that can cause trouble. Sometimes you may have different directories, and if you forget to add an essential file to your commit, you can end up with a bad codebase. 

To fix this mistake quickly, you can use the following Git add command: 

git add <missed-file>

After you perform the above command, use: 

git commit - -amend

This will add the file to the last commit. 

Make Fewer Common Git Mistakes

Git is a powerful tool, but you can only leverage all of its benefits by learning more about Git. There’s nothing to fear. Git is complex, but once you’ve learned Git, you won’t panic about mistakes and will feel more confident in your workflow. 

Don’t worry about making these common Git mistakes–you can fix them with a few commands. 

GitKraken Client helps developers of all skill levels better understand Git actions, enabling a more confident workflow with less mistakes. Level up today with the most popular Git client in the world – with a GUI and terminal!

Like this post? Share it!

Read More Articles

Make Git Easier, Safer &
More Powerful

with GitKraken
Visual Studio Code is required to install GitLens.

Don’t have Visual Studio Code? Get it now.

Team Collaboration Services

Secure cloud-backed services that span across all products in the DevEx platform to keep your workflows connected across projects, repos, and team members
Launchpad – All your PRs, issues, & tasks in one spot to kick off a focused, unblocked day. Code Suggest – Real code suggestions anywhere in your project, as simple as in Google Docs. Cloud Patches – Speed up PR reviews by enabling early collaboration on work-in-progress. Workspaces – Group & sync repos to simplify multi-repo actions, & get new devs coding faster. DORA Insights – Data-driven code insights to track & improve development velocity. Security & Admin – Easily set up SSO, manage access, & streamline IdP integrations.
winget install gitkraken.cli