Git Cheatsheet

A quick reference for common Git commands, categorized for ease of use.

Setup & Configuration

git config --global user.name "[name]"

Set the name you want attached to your commit transactions.

git config --global user.email "[email address]"

Set the email you want attached to your commit transactions.

git config --global color.ui auto

Enable helpful colorization of command line output.

git init [project-name]

Create a new local repository with the specified name. If project-name is omitted, initializes in the current directory.

git clone [url]

Download a project and its entire version history from a remote repository.

Staging & Committing Changes

git status

List all new or modified files to be committed.

git diff

Show file differences not yet staged.

git diff --staged

Show file differences between staging and the last file version.

git add [file]

Add a file to the staging area (snapshot for versioning).

git add .

Add all new and modified files to the staging area.

git reset [file]

Unstage a file, but preserve its contents.

git commit -m "[descriptive message]"

Record file snapshots permanently in version history.

git commit --amend

Change the last commit. Do not amend published commits.

Branching & Merging

git branch

List all local branches. Add -av to see all branches (local and remote-tracking).

git branch [branch-name]

Create a new branch.

git checkout [branch-name]

Switch to the specified branch and update the working directory.

git switch [branch-name]

Switch to the specified branch (newer, safer alternative to checkout).

git switch -c [new-branch-name]

Create and switch to a new branch.

git merge [branch-name]

Combine the specified branch’s history into the current branch.

git rebase [branch-name]

Reapply commits of current branch onto [branch-name].

git branch -d [branch-name]

Delete the specified local branch (if merged).

git branch -D [branch-name]

Force delete the specified local branch (even if not merged).

Working with Remotes

git remote -v

List all currently configured remote repositories.

git remote add [shortname] [url]

Add a new remote Git repository (e.g., origin).

git fetch [remote-name]

Download all history from the remote repository’s branches.

git pull [remote-name] [branch-name]

Fetch and merge changes from the remote server to your working directory.

git push [remote-name] [branch-name]

Upload all local branch commits to the remote repository.

git push [remote-name] --tags

Push all local tags to the remote repository.

git push [remote-name] :[branch-name]

Delete a branch on the remote repository (use with caution).

git push [remote-name] --delete [branch-name]

Delete a branch on the remote repository (newer syntax).

Viewing History & Undoing Changes

git log

List version history for the current branch.

git log --oneline --graph --decorate --all

Show a condensed, graphical log of all branches.

git log [file]

List version history for a file, including renames.

git diff [commit1] [commit2]

Show content differences between two commits.

git show [commit]

Output metadata and content changes of the specified commit.

git reset --hard HEAD

Discard all local changes in your working directory.

git reset --hard [commit]

Discard all history and changes back to the specified commit.

git revert [commit]

Create a new commit that undoes all of the changes made in [commit], then apply it to the current branch.

About Git

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on a project simultaneously without overwriting each other's changes.

This cheatsheet provides a quick reference to some of the most commonly used Git commands. For more detailed information, consult the official Git documentation.