GadgetForge

Blog

GadgetForge

BlogCheatsheets

Git Cheat Sheet - Most Used Commands


Configuration

Set the global config

ย shell
git config --global user.name "[name]"
git config --global user.email "[email]"

Get started

Create a git repository

ย shell
git init

Pull a git repository

ย shell
git pull [url]

Clone an existing git repository

ย shell
git clone [url]

Commit

Commit all tracked changes

ย shell
git commit -am "[commit message]"

Add new modifications to the last commit

ย shell
git commit --amend --no-edit

Branches

Lists all local branches in your repository (use -a for local and remote branches)

ย shell
git branch

Switch to an existing branch

ย shell
git switch [branch name]

Create a new branch

ย shell
git checkout -b [branch name]

Iโ€™ve made a mistake

Change last commit message

ย shell
git commit --amend

Undo most recent commit and keep changes

ย shell
git reset HEAD~1

Undo the N most recent commit and keep changes

ย shell
git reset HEAD~N

Undo most recent commit and get rid of changes

ย shell
git reset HEAD~1 --hard

Reset branch to remote state

ย shell
git fetch origin
git reset --hard origin/[branch-name]

Miscellaneous

Renaming the local master branch to main

ย shell
git branch -m master main

Checking log graph

ย shell
git log --graph

Checking log graph (merges only)

ย shell
git log --graph --merges

Tracking down a bad commit using binary search

ย shell
git bisect start
git bisect good 13c988d4f15e06bcdd0b0af290086a3079cdadb0
git bisect bad ca82a6dff817ec66f44342007202690a93763949

Pulling new changes into current branch from mainline

ย shell
git checkout [branch-name]
git fetch origin [master-branch-name]
git rebase origin/[master-branch-name]