Many newbies tend to make mistakes, especially at the beginning. This is completely normal, as Git can get very complex depending on the size of the project. I want to accelerate your learning success with Git in this article by showing you the 7 most common Git mistakes, more specifically Git problems, and how you can easily solve them.
If you haven’t started with Git yet, you should read through our Git tutorial first .
It starts with the 7 most common Git errors and their solutions:
1. Discard changes to local files
As a programmer, it happens every day that unexpected errors occur. In order to solve the errors quickly, we fiddle around with the code. Unfortunately, these code changes are not always optimal. It is therefore helpful to quickly undo the changes you have made.
With the command ” git checkout ” you can reset the files to their original state:
12# Reset directory “myCode”git checkout – myCode
2. Undo local commits
You have already created four new commits and only now realize that one of these commits contains a major error. Oops!
No panic. If you want to undo one or more commits, you can use the “git reset” command. The command knows three different modes (soft, hard, mixed):
1234th5# Undo the last four commits, keep changesgit reset HEAD ~ 4# Undo the last four commits, discard changesgit reset –hard HEAD ~ 4
Caution: The “hard” mode should be used with a bit of caution. As soon as you execute “git reset –hard”, the working tree and the index are reset. All changes are then lost forever!
3. Remove the file from Git without deleting it completely
It often happens that you add a file to the staging area ( git add ) that doesn’t belong there. You can use the command ” git rm ” here . However, this also removes the file from your file system.
However, if you want to keep the file in the filesystem, you can better remove it from the staging area with ” git reset <filename> “. Then add the file to the .gitignore so that you do not mistakenly pack it back into the staging index in the future. That’s how it’s done:
12git reset filenameecho filename >> .gitignore
4. Subsequently edit the commit message
Every programmer makes a typo on a commit. Fortunately, commit messages are very easy to correct using the “git commit –amend” command. That’s how it’s done:
1234th5# Start the standard text editor to edit the commit messagegit commit –amend # Sets the new message directlygit commit –amend -m “My new commit message”
5. Change local commits before a push
In the last point you got to know the option “–amend”. This is useful if you want to change the last commit. But what if the commit to be corrected wasn’t the last? You can use “git rebase –interactive” for this.
It is important that you enter your remote (usually “origin”) and the branch name here
1git rebase –interactive origin branchName
Running the above command will run your default text editor with the following text:
1234th56th7th8th9101112th13th14th15th1617th18th19thpick 9g2020f New feature ready pick 23js34cc My commit # Rebase 9g2020f..23js34cc onto 9g2020f # # Commands: # p, pick = use commit # r, reword = Used commit, but change commit message # e, edit = Use commit, but stop “amend” # s, squash = use commit, but merge with last commit # f, fixup = like “squash”, but removes this commit message of history # x, exec = execute command (via command line) ## These lines can be re-ordered; they are executed from top to bottom.# # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
Above you will see a list of local commits followed by an explanation of the commands available. Just select the commit or commits you want to update. Change pick to to reword (or r for short ) and you will be taken to a new view where you can edit the message.
As you can see from the above list, however, interactive rebases offer much more than simply editing commit messages: you can remove commits completely by deleting them from the list, as well as editing, rearranging and “being able” to squash.
6. Undo pushed commits
Yes, unfortunately bad commits occasionally make it into the remote repository.
Still, don’t be desperate, as Git offers a simple solution to rolling back single or multiple commits:
1234th56th7th8th# Undo commit using commit IDgit revert 453fsdu2# Undo the penultimate commitgit revert HEAD ^ # Undo multiple commitsgit revert feature ~ 4..feature ~ 2
In the event that you don’t want to create any additional revert commits, but only want to make the necessary changes to the working tree, you can use the –no-commit option (–n for short):
12# Undo the last commit, but don’t create a revert commitgit revert -n HEAD
7. Find the commit that causes an error after a merge
Tracking down the exact commit that causes a failure after a large merge can be quite time consuming.
Fortunately, Git has a good binary search facility with the “git bisect” command. First you have to start the set-up:
1234th56th7th8th# Start bisecting sessiongit bisect start # Mark the current revision as “bad”git bisect bad# Mark the last known good revision as “good”git bisect good revision
After that, Git will automatically check out the revision that is between “Good” and “Bad”. Then run your code again. Mark the commits with “bad” or “good” until you find the appropriate commit that is causing the error.
Bonus tip: Everything else has failed – this is how you solve the problem
So far we have learned many commands and tips that will help solve common mistakes in everyday use of Git.
Most problems have a fairly simple solution. But there are also incidents that require the big “hammer”. The only thing that helps here is to rewrite the history of the respective branch. In practice, this often happens with sensitive data that needs to be removed. This can sometimes be customer log-in data that was accidentally transferred to a publicly visible repository. Action must be taken here quickly and safely. That’s how it’s done:
123git filter-branch –force –index-filter \ ‘git rm –cached –ignore-unmatch logindaten.txt’ \ –prune-empty –tag-name-filter cat – –all
The above command removes the logindaten.txt file from every branch and every day. In addition, all commits are removed that would be empty by the above operation.
Please be particularly careful with the filter-branch command, as the entire history of the repository will be rewritten. In an ongoing project, this can lead to high transaction costs and disrupt the entire development team at work.
Conclusion on the Git problems
That was a detailed overview of the 7 most common Git problems and errors and their solutions. You got to know the following commands here:
- git checkout
- git reset
- .gitignore
- git commit –amend
- git rebase –interactive
- git revert
- git bisect
- git filter-branch
I hope that you can implement some of the tips mentioned here in your own projects to solve Git errors faster and with fewer headaches in the future.