DEV Community 👩‍💻👨‍💻

Cover image for 4 Most Common Git Mistakes 😍 🔥
simc dev CSM®
simc dev CSM®

Posted on • Updated on • Originally published at cmsinstallation.blogspot.com

4 Most Common Git Mistakes 😍 🔥

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:

  • Reset directory "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):

  • 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 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 ”. 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

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

Top comments (17)

Collapse
 
carehart profile image
Charlie Arehart

See the original post, which has far better text (and command) formatting, as well as screenshots, and discusses 7 rather than 4 "Git mistakes" .

Collapse
 
devsimc profile image
simc dev CSM®

agreed, that original post has better view and explanation....
the original post is also written by me....
I was trying to make series of it...

Collapse
 
chrisjohdev profile image
Chris Johannesson

I wrote a cheat sheet for Git and Github where I used the book that is referenced in the pdf. It's a free book and well worth downloading. You'll find my cheat sheet at: github.com/ChrisJohDev/pub-GitAndG...

Enjoy it's free. ;)

Collapse
 
devsimc profile image
simc dev CSM®

Nice.!

Collapse
 
crayoncode profile image
crayoncode

Point 1 can also be achieved through git reset --hard HEAD.
git checkout is most handy if you only wish to undo changes in one directory or a specific file by checking out that directory/file again.

Collapse
 
devsimc profile image
simc dev CSM®

🔥

Collapse
 
lazarovbonifacio profile image
Lázaro V Bonifácio

I think that git config --global <something> is also a mistake. Because, I guess, in most cases we have the company repo and personal repos cloned in the computer. And the configuration of git are usually different in both (user.name and user.email, for example).

Collapse
 
yimiprod profile image
Yimi • Edited on

Instead of git checkout - myFile we can use the command git restore myFile which is less ambiguous in and less error prone since it's dedicated to that task.

Collapse
 
devsimc profile image
simc dev CSM®

🔥

Collapse
 
gnsp profile image
Ganesh Prasad

Point 3 can be done with git rm --cached filename

Collapse
 
devsimc profile image
simc dev CSM®

🔥

Collapse
 
thec0def0x profile image
CodeFox

Handy tips. Thank you. I’ve become too reliant on Visual Studio Codes source control for unstaging stuff.

Collapse
 
devsimc profile image
simc dev CSM®

Thanks

Collapse
 
tsbrun profile image
Anya Brun

These are very helpful 👍🏽 Thanks for the article!

Collapse
 
devsimc profile image
simc dev CSM®

Thanks

Collapse
 
magaliechetrit profile image
Magalie Chetrit

A little related, I also use this: gitexplorer.com hope it helps someone

Collapse
 
devsimc profile image
simc dev CSM®

🔥

19 Valuable Github Repositories for Beginners

19 Valuable GitHub Repositories for beginner devs looking to take the first step into the web development career.