Contents

A cheat sheet for Git CLI

This is a cheat sheet for Git.

Setup

Configuring user information used across all local repositories

1
2
3
git config --global user.name "[firstname lastname]"
git config --global user.email "[valid-email]"
git config --global color.ui auto

Setup & Init

Configuring user information, initializing and cloning repositories

Initialize an existing directory as a Git repository

1
git init

Retrieve an entire repository from a hosted location via URL

1
git clone [url]

Stage & Snapshot

Working with snapshots and the Git staging area

Show modified files in working directory, staged for your next commit

1
git status

Add a file as it looks now to your next commit (stage)

1
git add [file]

Add all files as it looks now to your next commit (stage)

1
git add -A

Unstage a file while retaining the changes in working directory

1
git reset [file]

Diff of what is changed but not staged

1
git diff

Diff of what is staged but not yet commited

1
git diff --staged

Commit your staged content as a new commit snapshot

1
git commit -m "[descriptive message]"

Branch & Merge

Isolating work in branches, changing context, and integrating changes

List your branches. a * will appear next to the currently active branch

1
git branch

Create a new branch at the current commit

1
git branch [branch-name]

Switch to another branch and check it out into your working directory

1
git checkout

Merge the specified branch’s history into the current one

1
git merge [branch]

Show all commits in the current branch’s history

1
git log

Inspect & Compare

Examining logs, diffs and object information

Show the commit history for the currently active branch

1
git log

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

1
git log branchB..branchA

Show the commits that changed file, even across renames

1
git log --follow [file]

Show the diff of what is in branchA that is not in branchB

1
git diff branchB...branchA

Show any object in Git in human-readable format

1
git show [SHA]

Share & Update

Retrieving updates from another repository and updating local repos

Add a git URL as an alias

1
git remote add [alias] [url]

Fetch down all the branches from that Git remote

1
git fetch [alias]

Merge a remote branch into your current branch to bring it up to date

1
git merge alias/branch

Transmit local branch commits to the remote repository branch

1
git push [alias] [branch]

Fetch and merge any commits from the tracking remote branch

1
git pull

Tracking path changes

Versioning file removes and path changes

Delete the file from project and stage the removal for commit

1
git rm [file]

Change an existing file path and stage the move

1
git mv [existing-path] [new-path]

Show all commit logs with indication of any paths that moved

1
git log --stat -M

Rewrite history

Rewriting branches, updating commits and clearing history

Apply any commits of current branch ahead of specified one

1
git rebase [branch]

Clear staging area, rewrite working tree from specified commit

1
git reset --hard [commit]

Discard changes in working directory if not staged yet

1
git restore [file]

Temporary commits

Temporarily store modified, tracked files in order to change branches

Save modified and staged changes

1
git stash

List stack-order of stashed file changes

1
git stash list

Write working from top of stash stack

1
git stash pop

Discard the changes from top of stash stack

1
git stash drop

Ignoring Patterns

Preventing unintentional staging or commiting of files

Save a file with desired paterns as .gitignore with either direct string matches or wildcard globs.

1
2
3
logs/
*.notes
pattern*/

System wide ignore patern for all local repositories

1
git config --global core.excludesfile [file]

**Source : ** https://education.github.com/git-cheat-sheet-education.pdf