Skip to main content

Snippets

Useful Git Commands and Configuration Snippets

Efficiently managing version control requires a solid grasp of Git beyond basic commits. This collection provides essential snippets for refining .gitignore rules, enforcing consistent line endings across platforms, and safely undoing local changes.

Update .gitignore

After you have updated your .gitignore file you need to reset git to track the correct files.

git rm -r --cached .
git add .
git commit -m "Update files after change in .gitignore"

Consistent line-endings

To keep the line-endings in text files consistent, add an .gitattributes with the following content to the root of your repo. This will make sure all text files keep their unix line endings except for the ones specified.

* text=auto eol=lf
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf

If this is done to an existing repo, make sure to reset all files with:

git rm --cached -r .
git reset --hard

Undo the last commit

Did you accidentally commit your changes to the wrong branch? If you still haven't pushed, it is no problem just undo it (the commit will be undone but your changes will remain).

git reset --soft HEAD~1