Git Cheat Sheet

Setup & Config

Set User Name

Set the name you want attached to your commit transactions.

Terminal
git config --global user.name "Your Name"

Set User Email

Set the email you want attached to your commit transactions.

Terminal
git config --global user.email "email@example.com"

Initialize Repository

Create a new local repository.

Terminal
git init

Clone Repository

Download a project and its entire version history.

Terminal
git clone <url>

Basic Snapshotting

Check Status

List which files are staged, unstaged, and untracked.

Terminal
git status

Add Files

Stage changes for the next commit.

Terminal
git add <file>
# or stage all changes
git add .

Commit Changes

Commit your staged content as a new snapshot.

Terminal
git commit -m "Commit message"

View History

Show the commit history for the currently active branch.

Terminal
git log

Branching & Merging

List Branches

List all local branches in the current repository.

Terminal
git branch

Create Branch

Create a new branch.

Terminal
git branch <branch-name>

Switch Branch

Switch to the specified branch and update the working directory.

Terminal
git checkout <branch-name>
# or
git switch <branch-name>

Create & Switch

Create a new branch and switch to it.

Terminal
git checkout -b <branch-name>

Merge Branch

Merge the specified branch's history into the current one.

Terminal
git merge <branch-name>

Sharing & Updating

Remote Add

Add a new remote repository.

Terminal
git remote add <name> <url>

Fetch

Download all changes from the remote repository.

Terminal
git fetch <remote>

Pull

Fetch and merge changes on the remote branch to your local branch.

Terminal
git pull <remote> <branch>

Push

Upload local branch commits to the remote repository.

Terminal
git push <remote> <branch>

Inspection & Comparison

Diff

Show changes between commits, commit and working tree, etc.

Terminal
git diff

Diff Staged

Show changes between the staging area and the last commit.

Terminal
git diff --staged

Undoing Changes

Discard Changes

Discard changes in the working directory.

Terminal
git checkout -- <file>
# or
git restore <file>

Unstage File

Remove a file from the staging area.

Terminal
git reset HEAD <file>
# or
git restore --staged <file>

Amend Commit

Replace the last commit with the staged changes and last commit combined.

Terminal
git commit --amend