GIT

Git Cheatsheet

The git commands developers reach for daily - from basic commits to untangling a messy branch.

Basics

git init

Create a new repository in the current folder

git clone <url>

Clone a remote repository

git status

Show changed/staged files

git add <file>

Stage a file for commit

git add .

Stage all changed files

git commit -m "msg"

Commit staged changes with a message

Branching

git branch

List local branches

git branch <name>

Create a new branch

git checkout <name>

Switch to a branch

git checkout -b <name>

Create and switch to a new branch

git merge <name>

Merge a branch into the current one

git branch -d <name>

Delete a local branch

Remote & sync

git push origin <branch>

Push a branch to the remote

git pull

Fetch and merge from the remote

git fetch

Download remote changes without merging

git remote -v

List configured remotes

Undoing things

git restore <file>

Discard unstaged changes to a file

git restore --staged <file>

Unstage a file (keep changes)

git reset --soft HEAD~1

Undo last commit, keep changes staged

git reset --hard HEAD~1

Undo last commit and discard changes

git revert <commit>

Create a new commit that undoes a previous one

git stash

Temporarily shelve uncommitted changes