Many newbies tend to make mistakes, especially at the beginning. This is completely normal, as Git can get very complex, depending on the project's size. I want to accelerate your learning success with Git in this article by showing you the 7 most common Git errors, more specifically Git problems, and how you can easily solve them.
List of 7 most common Git mistakes and their solutions:
1. Discard changes to local files in git
As a programmer, it happens every day that unexpected errors occur. To solve the errors quickly, we fumble wildly with the code. Unfortunately, these code changes are not always optimal. It is, therefore, helpful to quickly undo the changes you have made. this is the common mistake in git
. if you don't want to do it you can do git commit with a comment.
With the command “git checkout” you can reset the files to their original state:
. if you don't want to do it you can do git commit with a comment.
With the command “git checkout” you can reset the files to their original state:
# Reset directory "myCode"
git checkout - myCode
git checkout - myCode
2. Undo local commits. git commit revert
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):
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):
# Undo the last four commits, keep changes
git reset HEAD ~ 4
# Undo the last four commits, discard changes
git reset --hard HEAD ~ 4
git reset HEAD ~ 4
# Undo the last four commits, discard changes
git reset --hard HEAD ~ 4
3. Remove the file from Git without deleting it completely in git commit
You often 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:
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:
git reset Dateiname
echo Dateiname >> .gitignore
echo Dateiname >> .gitignore
4. Git commit message change
git commit message changes Every programmer makes a typo on a commit. Fortunately, commit messages are very easy to correct using the “git commit — amend” command. you can commit your code with your own git commit with a message. That’s how it’s done:
# Start the standard text editor to edit the commit message
git commit --amend
# Sets the new message directly
git commit --amend -m "My new commit message
git commit --amend
# Sets the new message directly
git commit --amend -m "My new commit message
5. Change local commits before a push in git commit
In the last point, you got to know the option “ — git commit amends message”. 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.
You must enter your remote (usually “origin”) and the branch name here.
git rebase-interactive origin branchName
Running the above command will run your default text editor with the following text:
pick 9g2020f New feature ready
pick 23js34cc My commit
# Rebase 9g2020f..23js34cc onto 9g2020f
#
# Commands:
# p, pick = use commit
# r, reword = Used commit, but changed 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 from the history
# x, exec = execute command (via the 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 to want to update. Change pick to reword (or r for short ), and it will take you to a new view where you can edit the message.
As you can see from the list above, however, interactive rebase offer much more than simply editing commit messages: you can delete commits by deleting them from the list, as well as “can” edit, rearrange and squash.
You must enter your remote (usually “origin”) and the branch name here.
git rebase-interactive origin branchName
Running the above command will run your default text editor with the following text:
pick 9g2020f New feature ready
pick 23js34cc My commit
# Rebase 9g2020f..23js34cc onto 9g2020f
#
# Commands:
# p, pick = use commit
# r, reword = Used commit, but changed 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 from the history
# x, exec = execute command (via the 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 to want to update. Change pick to reword (or r for short ), and it will take you to a new view where you can edit the message.
As you can see from the list above, however, interactive rebase offer much more than simply editing commit messages: you can delete commits by deleting them from the list, as well as “can” edit, rearrange and squash.
6. Undo pushed commits in git commit
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:
If 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):
Still, don’t be desperate, as Git offers a simple solution to rolling back single or multiple commits:
# Commit anhand von Commit-ID rueckgaengig machen
git revert 453fsdu2#Undo the penultimate commit
git revert HEAD ^#Undo multiple commits
git revert feature ~ 4..feature ~ 2
git revert 453fsdu2#Undo the penultimate commit
git revert HEAD ^#Undo multiple commits
git revert feature ~ 4..feature ~ 2
If 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):
# Undo the last commit, but don't create a revert commit
git revert -n HEAD
git revert -n HEAD
7. Find the commit that causes an error after a merge in git commit
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:
Fortunately, Git has a good binary search facility with the “git bisect” command. First, you have to start the set-up:
# Start bisecting session
git bisect start#Mark the current revision as "bad"
git bisect bad#Mark the last known good revision as "good"
git bisect good revision
git 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 between “Good” and “Bad.” Then rerun your code. Mark the commits with “bad” or “good” until you find the appropriate commit causing the error.
Bonus tip: Everything else has failed — this is how you solve the problem
So far, we’ve seen many commands and tips to help solve common mistakes in everyday Git use.
Most problems have a fairly simple solution. But some incidents 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:
git filter-branch — force — index-filter \
‘git rm — cached — ignore-unmatch logindaten.txt’ \
— prune-empty — tag-name-filter cat — — all
‘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. Also, 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. This can lead to high transaction costs and disrupt the entire development team at work in an ongoing project.
Read More:: 5 Common CSS mistakes developers make
Tags
common git error git commit revert
git commit -am not working
git commit message change
git commit squash
git commit with message
whenever we take a Pull from existing project than some confusing bcz each project more than 2 developers so each time discuss each other and than take pull this is the only rules. so some time accidantly by mistake some one take a pull than create some unusual problems
ReplyDelete