An Interest In:
Web News this Week
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
Most Common Git Mistakes
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.
1. Discard changes to local files
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.
With the command git checkout you can reset the files to their original state:
# 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):
# Undo the last four commits, keep changes
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
You often add a file to the staging area ( git add ) that doesnt 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 . Then add the file to the .gitignore so that you do not mistakenly pack it back into the staging index in the future. Thats how its done:
git reset Dateiname
echo Dateiname >> .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. Thats how its 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
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 wasnt 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
Original Link: https://dev.to/animeshdhanuk/most-common-git-mistakes-4f8
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To