Git Cheat Sheet

Git is the most important part for the developers. Git is the distributed version of the control system that helps developers to collaborate on projects.

Why use GIT?

Many developers are engaged in the same project in real life and git is one of the version control systems that help developers shared their files with others working in parallel.

Basic Commands of GIT you should know!

Git Configuration

Define the username for the commits and tags.

git config --global user.name <name>

Define the email for the commits and tags.

git config --global user.email <email>

Enable some colorization of git output.

git config --global color.ui auto

Setup and init

Initialize an existing directory as a git repository.

git init

Downloads the projects with all history from remote repository.

git clone

Git day to day commands

Shows the new, staged, modified files for the next commit.

git status

Add the modified files to the staging area.

git add <files>

Shows the difference between what is changed but not staged.

git diff

Show the difference of what is staged but not committed.

git diff --staged

Revert your repository to a previously known working state.

git reset <file>

Create a new commit for the changes added to the staged area. It must include the commit message.

git  commit

Branch and Merge

List all the branches in the repository.

git branch -a

Create a new branch from the current branch.

git branch <branch_name>

Create new branch and checkout to the new branch.

git checkout -b <branch_name>

Switch to another branch from the current branch.

git checkout <branch_name>

Merge the branch into the current branch.

git merge <branch_name>

Remove the selected branch.

git branch -d <branch_name>

Review your work

List commit history of the current branch.

git log

Show the commits on branch A that are not in branch B.

git log  branchB..branchA

Tracking path changes

Remove the file from the working directory and staging area.

git rm <file>

Change an existing file path and stage the move.

git mv <existing_path> <new_path>

Synchronize repository

Fetch changes from the remote

git fetch

Delete remote Refs that were removed from the remote repository.

git fetch --prune [remote]

Fetch changes from the remote and merge.

git pull

Push local changes to the remote repository.

git push

Temporary commits

Put the current changes in your working directory into stash for later use.

git stash

List stack-order of stashed file changes.

git stash list

Apply stored stash content into the working directory and clear stash.

git stash pop

Delete the specific stash for all your previous stashes.

git stash drop