Git Commands Cheat Sheet

Git Basics

Creating a new repository

mkdir project
cd project
git init
git remote add origin git@github.com:yourlogin/your-repo.git
git add .
git commit -am “new repository”
git push -u origin master

Git Clone

Clone repo located at<repo>onto the local machine. The original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.

git clone <repo.git>

Git Config

Git config user.name

Define the author’s name to be used for all commits in the current repo. Devs commonly use –global flag to set config options for the current user:

git config --global user.name <name>

Git config user.email

The same can be applied by using an email address:

git config --global user.email <email address>

Git Add

Stage all changes in <directory> for the next commit. Replace <directory> with a <file> to change a specific file.

git add <directory>

Git Commit

Commit the staged snapshot, but instead of launching a text editor, use <message> as the commit message.

git commit -m "<message>"

Git Status

List which files are staged, unstaged, and untracked

git status

Git Log

Display the entire commit history using the default format. For customization see additional options

git log

Git Diff

Show unstaged changes between your index and working directory.

git diff

Git diff head

Show the difference between the working directory and last commit:

git diff HEAD

Git diff –cached

Show the difference between staged changes and last commit:

git diff --cached

Git Branches

List all branches

Add a <branch> argument to create a new branch with the name <branch>:

git branch -a

Create new branch

Create and check out a new branch named <branch>:

git checkout -b <branch>
git add .
git commit -am <branch>

Merging branch to master

git checkout master
git merge <branch>
git push

Deleting branch

git branch -d <branch>

Switch branch

git checkout <branch>

Switch to master branch

git checkout master

Remote repositories

List remote repositories:

git remote -v

Add remote Repository

Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands:

git remote add <name> <url>

Git fetch

Fetches a specific <branch>, from the repository. Leave off <branch> to fetch all remote refs:

git fetch <remote> <branch>

Git pull

Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy:

git pull <remote>

Git push

Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.

git push <remote> <branch>

Replacing remote repository

Just in case your remote repository changes or you want to switch from HTTPS->SSH or SSH->HTTPS

git remote remove origin
git remote add origin git@github.com:yourlogin/your-repo.git

Tags

Git tag

To create a new tag execute the following command:

git tag <tagname>

Tag list

To list stored tags in a repo execute the following:

git tag

Push tag

Sharing tags is similar to pushing branches. By default, git push will not push tags. Tags have to be explicitly passed to git push.

git push origin <tag>

Tag checkout

git checkout <tag>

Tag delete

git tag -d <tag>