Back to Cheat Sheets
Cheat Sheets/Interactive Git Cheat Sheet
version control

Interactive Git Cheat Sheet

All essential git commands with examples

GitCommandsVersion Control
Setup & Config
Set name
git config --global user.name "Your Name"

Configure your git username

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

Configure your git email

Check config
git config --list

View all git configurations

Creating Repos
New repo
git init

Initialize a new git repository

Clone repo
git clone <url>

Clone an existing repository

Clone specific branch
git clone -b <branch> <url>

Clone a specific branch

Staging & Committing
Stage file
git add <file>

Stage a specific file

Stage all
git add .

Stage all changes

Commit
git commit -m "message"

Commit staged changes

Amend commit
git commit --amend

Modify the last commit

Branching
List branches
git branch

List all local branches

Create branch
git branch <name>

Create a new branch

Switch branch
git checkout <branch>

Switch to a branch

Create & switch
git checkout -b <branch>

Create and switch to new branch

Delete branch
git branch -d <branch>

Delete a branch

Remote & Push
Add remote
git remote add origin <url>

Add a remote repository

Push
git push origin <branch>

Push to remote

Force push
git push --force

Force push (use carefully)

Pull
git pull

Fetch and merge remote changes

Undoing Changes
Unstage file
git reset HEAD <file>

Unstage a file

Undo commit
git reset --soft HEAD~1

Undo last commit, keep changes

Discard changes
git checkout -- <file>

Discard working directory changes

Revert commit
git revert <commit>

Create a new commit that undoes a commit