Git Cheatsheet

Complete reference guide for all Git commands and workflows

Setup & Configuration

Set User Name

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

Sets the name that will be attached to your commits.

Set User Email

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

Sets the email that will be attached to your commits.

Set Default Editor

git config --global core.editor "code --wait"

Sets VS Code as the default editor for Git messages.

View Configuration

git config --list

Lists all Git configuration settings.

Set Credential Helper

git config --global credential.helper cache

Caches credentials in memory for a short time (default 15 minutes).

Set Auto CRLF Handling

git config --global core.autocrlf true

Automatically handles line endings (Windows). Use input for Linux/Mac.

Basic Commands

Initialize Repository

git init

Creates a new Git repository in the current directory.

Clone Repository

git clone <repository-url>

Creates a local copy of a remote repository.

Add Files to Staging

git add <file>

Adds a specific file to the staging area. Use . to add all files.

Commit Changes

git commit -m "Commit message"

Records changes to the repository with a descriptive message.

View Status

git status

Shows the state of the working directory and staging area.

Restore File

git restore <file>

Discards changes in working directory (Git 2.23+).

View Commit History

git log --oneline --graph --all

Shows commit history in a compact, graphical format.

View Changes

git diff

Shows unstaged changes since last commit.

Branching

List Branches

git branch

Lists all local branches. Add -a to show all branches including remote.

Create Branch

git branch <branch-name>

Creates a new branch from the current HEAD.

Switch Branch

git checkout <branch-name>

Switches to the specified branch.

Create & Switch Branch

git checkout -b <branch-name>

Creates a new branch and switches to it immediately.

Delete Branch

git branch -d <branch-name>

Deletes the specified branch (safe delete). Use -D for force delete.

Rename Branch

git branch -m <new-branch-name>

Renames the current branch.

Track Remote Branch

git branch -u origin/<branch-name>

Sets up tracking relationship with a remote branch.

Merging & Rebasing

Merge Branch

git merge <branch-name>

Merges the specified branch into the current branch.

Rebase Branch

git rebase <branch-name>

Reapplies commits on top of another base branch.

Abort Rebase

git rebase --abort

Cancels an ongoing rebase operation.

Continue Rebase

git rebase --continue

Continues a rebase after resolving conflicts.

Squash Commits

git rebase -i HEAD~n

Interactive rebase to squash last n commits.

Abort Merge

git merge --abort

Cancels an ongoing merge operation.

Remote Repositories

List Remotes

git remote -v

Lists all remote repositories with their URLs.

Add Remote

git remote add origin <url>

Adds a new remote repository with the name "origin".

Fetch from Remote

git fetch origin

Downloads objects and refs from remote repository without merging.

Pull from Remote

git pull origin <branch>

Fetches and merges changes from a remote branch.

Push to Remote

git push origin <branch>

Uploads local branch commits to the remote repository.

Remove Remote

git remote remove origin

Removes the remote named "origin".

Update Remote URL

git remote set-url origin <new-url>

Changes the URL for the remote named "origin".

Track Remote Branch

git checkout -b <branch> origin/<branch>

Creates a local branch that tracks a remote branch.

Stashing

Stash Changes

git stash

Temporarily stores modified tracked files.

Stash with Message

git stash save "message"

Stashes changes with a descriptive message.

List Stashes

git stash list

Lists all stashed changesets.

Apply Stash

git stash apply

Applies the most recent stash without removing it from stash list.

Pop Stash

git stash pop

Applies and removes the most recent stash from the stash list.

Drop Stash

git stash drop stash@{n}

Deletes the stash at index n.

Clear Stash

git stash clear

Removes all stashed entries.

Inspection & Comparison

View Commit Log

git log --oneline -10

Shows the last 10 commits in a compact format.

View Changes Between Commits

git diff <commit1> <commit2>

Shows changes between two commits.

Show Commit Details

git show <commit>

Shows information about a specific commit.

View Branch History

git log --graph --all --oneline

Shows a graphical representation of branch history.

Search Commit Messages

git log --grep="search term"

Filters commits by message content.

View Commits by Author

git log --author="name"

Shows commits by a specific author.

View File History

git log -p <file>

Shows commit history with changes for a specific file.

View Commits by Date

git log --since="2 weeks ago"

Shows commits from the last 2 weeks.

Advanced Commands

Undo Last Commit

git reset --soft HEAD~1

Undoes the last commit but keeps changes staged.

Hard Reset

git reset --hard HEAD~1

Completely removes the last commit and all changes.

Revert Commit

git revert <commit>

Creates a new commit that undoes the changes of a previous commit.

Find by Hash

git show <commit-hash>

Shows information about a specific commit using its hash.

Create Tag

git tag -a v1.0 -m "Version 1.0"

Creates an annotated tag for a release point.

List Tags

git tag -l

Lists all tags in the repository.

Clean Untracked Files

git clean -fd

Removes untracked files and directories. Use with caution!

Bisect for Debugging

git bisect start
git bisect bad
git bisect good <commit>

Binary search to find the commit that introduced a bug.

Git Cheatsheet © 2023 | Complete reference for developers

Remember: git status is your best friend when you're unsure about the current state!