Git cheatsheet

Git is a decentralized version management software. With this cheatsheet, you will have quick access to the most common git commands

Configuration

Set the global config

git config --global user.name "[name]"
git config --global user.email "[email]"
Get started

Create a git repository

git init

Clone an existing git repository

git clone [url]
Commit

Commit all tracked changes

git commit -am "[commit message]"

Add new modifications to the last commit

git commit --amend --no-edit
I've made a mistake

Change last commit message

git commit --amend

Undo most recent commit and keep changes

git reset HEAD~1

Undo the N most recent commit and keep changes

git reset HEAD~N

Undo most recent commit and get rid of changes

git reset HEAD~1 --hard

Discard all uncommitted changes

git reset --hard

Reset branch to remote state

git fetch origin
git reset --hard origin/[branch-name]
Miscellaneous

Renaming the local master branch to main

git branch -m master main
Staging

Add file to staging area

git add [file]

Add all changes to staging area

git add .

Remove file from staging area

git reset HEAD [file]

View staged changes

git diff --staged
Branches

List all branches

git branch

Create a new branch

git branch [branch-name]

Switch to a branch

git checkout [branch-name]

Create and switch to a new branch

git checkout -b [branch-name]

Delete a branch

git branch -d [branch-name]

Force delete a branch

git branch -D [branch-name]

Merge a branch into current branch

git merge [branch-name]
Remote

List remote repositories

git remote -v

Add a remote repository

git remote add [name] [url]

Fetch changes from remote

git fetch [remote]

Pull changes from remote

git pull [remote] [branch]

Push changes to remote

git push [remote] [branch]

Push and set upstream

git push -u origin [branch]
History

View commit history

git log

View commit history (one line per commit)

git log --oneline

View commit history with graph

git log --graph --oneline --all

View changes in a specific commit

git show [commit-hash]

View file history

git log -p [file]
Stashing

Stash current changes

git stash

Stash with a message

git stash save "[message]"

List all stashes

git stash list

Apply most recent stash

git stash apply

Apply and remove most recent stash

git stash pop

Delete most recent stash

git stash drop
Status & Inspection

View status of working directory

git status

View unstaged changes

git diff

View staged changes

git diff --staged

View changes between branches

git diff [branch1]..[branch2]
Note: Git is a decentralized version management software. This cheatsheet provides quick access to the most common git commands for everyday development workflows.
Free Git Cheatsheet - Quick Reference for Common Git Commands