Git Commands Cheat Sheet

git config

git config –global user.name “[name]”
git config –global user.email “[email address]”

This command sets the your name and email address respectively to your git project.

git init

git init [repository name]

Start a new repository.

git clone

git clone [url]
git clone --recurse-submodules link-to-repo
git clone --recurse-submodules -b branch_name linktorepo

This clones a code from git project.

git add

git add [file]

Adds a file or folders and prepares them to be committed.

git add *

This command adds all files and folders.

git commit

git commit -m “your commit message here”

Prepares new files, folders or both to be pushed to git project.

git commit -a

Commits files you’ve added with the git add command and also commits any files you’ve changed since then.

git diff

git diff

Shows the file differences which are not yet committed.

git diff –staged

Shows the differences between the files which are not yet committed and the latest version present.

git diff first-branch second-branch

Shows the differences between the two branches mentioned.

git reset

git reset file

This command unstages the file, but it preserves the file contents.

git reset <commit>

This command undoes all the commits after the specified commit and preserves the changes locally.

git reset –hard <commit> 

Discards all history and goes back to the specified commit.

git status

git status

Lists all the files that have to be committed.

git rm

git rm [file]

Deletes the file from your working directory and stages the deletion.

git log

git log

This command is used to list the version history for the current branch.

git log –follow<file>

Lists version history for a file, including the renaming of files also.

git show

git show <commit>

Shows the metadata and content changes of the specified commit.

git tag

git tag <commitID>

This command is used to give tags to the specified commit.

git branch

git branch

Lists all the local branches.

git branch <branch name>

Creates a new branch.

git branch -d <branch name>

Deletes the feature branch.

git checkout

git checkout <branch name>

This command is used to switch from one branch to another.

git checkout -b <branch name>

Creates a new branch and also switches to it.

git merge

git merge <branch name>

Merges the specified branch’s into the current branch.

git remote

git remote add <variable name> <Remote Server Link>

This command is used to connect your local repository to the remote server.

git push

git push <variable name> master

Sends the committed changes of master branch to your remote repository.

git push <variable name> <branch>

Sends the branch commits to your remote repository.

git push –all <variable name>

Pushes all branches to your remote repository/git project.

git push <variable name> :<branch name>

Deletes a branch on your remote repository.

git pull

git pull <Repository Link>

or

git pull

This command fetches and merges changes on the remote server to your working directory.

git stash

git stash save

Temporarily stores all the modified files.

git stash pop

Restores the most recently stashed files.

git stash list

Lists all stashed change-sets.

git stash drop

Discards the most recently stashed change-set.