The Ultimate Git Command Cheat Sheet

Whether you are spinning up a new project or untangling a messy merge conflict, Git is the backbone of modern development. Bookmark this cheat sheet for a quick reference to the most essential Git commands.

🛠️ 1. Setup & Initialization

Configure your Git environment and start tracking projects.

Sets the name you want attached to your commit transactions.

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

Sets the email you want attached to your commit transactions.

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

Initializes a new local repository in the current directory.

git init

Downloads a project and its entire version history from a remote server.

git clone [url]

📸 2. Staging & Committing (Basic Snapshotting)

Track your changes and save snapshots of your work.

Lists all new or modified files to be committed.

git status

Shows file differences not yet staged.

git diff

Stages the file, readying it for a commit. Use git add . to stage all modified files in the directory.

git add [file]

Shows file differences between staging and the last file version.

git diff --staged

Commits your staged content as a new commit snapshot.

git commit -m "[descriptive message]"

🌿 3. Branching & Merging

Isolate your work, experiment safely, and integrate changes.

Lists all local branches in the current repository.

git branch

Creates a new branch.

git branch [branch-name]

Switches to the specified branch and updates the working directory.

git switch [branch-name]

(Alternatively, you can use git checkout [branch-name])

Creates a new branch and switches to it immediately.

git switch -c [branch-name]

(Alternatively, you can use git checkout -b [branch-name])

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

git merge [branch-name]

Deletes the specified branch locally.

git branch -d [branch-name]

🌍 4. Sharing & Updating (Remote Repositories)

Sync your local repository with a remote server like GitHub or GitLab.

Connects your local repository to a remote server.

git remote add origin [url]

Downloads all history from the remote tracking branches.

git fetch

Fetches and merges changes on the remote server to your working directory.

git pull

Uploads all local branch commits to the remote repository.

git push origin [branch-name]

🔍 5. Inspection & Comparison

Review the history and figure out what changed.

Lists version history for the current branch.

git log

Shows the commit history condensed to a single line per commit.

git log --oneline

Outputs metadata and content changes of the specified commit.

git show [commit]

⏪ 6. Undoing Changes & Advanced Recovery

Fix mistakes, stash temporary work, and rewrite history safely.

Temporarily stores all modified tracked files so you can switch branches without committing.

git stash

Restores the most recently stashed files.

git stash pop

Discards changes in the working directory for a specific file.

git restore [file]

(Alternatively, you can use git checkout -- [file])

Creates a new commit that undoes all of the changes made in a specific commit, preserving history.

git revert [commit]

Undoes all commits after [commit], preserving changes locally.

git reset [commit]

Warning: Discards all history and changes back to the specified commit.

git reset --hard [commit]