My Favorite Git Commands and Settings

My Favorite Git Commands and Settings

Introduction

Version control is important for any software developer and Git is now the most popular one. I will show you some of my favorite git commands and settings.

Setup name and email

Configure name and email for git commits.

Usage: git config --global user.name “[name]”

Usage: git config --global user.email “[email address]”

Handling end of line character

Windows and Unix operating systems have different end of line characters. To prevent users editing code on different systems always changing the end of line characters we add the below settings to the repository so developers don’t need to manually configure it them self’s

  1. git add .gitattributes
  2. git add --renormalize // update cache and normalize end of line if needed
  3. git push

git commit

Create a new file in the root of the repo called .gitattributes.gitattributes

text=auto
*.png binary
*.jpg binary

Undo local changes

Revert any local changes before

Usage: git reset --hard <commit>

Usage: git reset [file]

Prevent local file from being added to repository

Create file called .gitignore then include files you want to prevent from being added to to your repository. Don’t forget to commit and push so everyone cloning the repository knows which files to exclude.

Get latest file changes from remote

When working with other developers you will need to get the latest changes to our repository

git pull -r

What are the changes I’m making?

Knowing what changes you might be committing is is important to keeping your commits clean.

git diff

Usage: git diff <filename>

git status

Undo the latest commit message

The great thing about version control in general you can revert any change this includes changing your commit messages.

Fixing a commit message.

git commit --amend