Skip to main content

Git Cheatsheet

·2 mins

A quick reference for the Git commands used most often in day-to-day development.

Cloning and branches #

  • git clone <URL> — clone a repository to your local workspace
  • git checkout <branch> — switch to an existing branch
  • git checkout -b <branch> — create a new branch and switch to it
  • git branch — list local branches
  • git branch -vv — list local branches with their last commit and the upstream (remote-tracking) branch they’re tracking, e.g. [origin/main: ahead 2]

Staging and unstaging #

  • git add . / <file> — stage changes in a file (or all changes with .)
  • git reset — unstage all changes, keeping the changes in your working directory intact (equivalent to git reset --mixed HEAD)
  • git restore <file> — restore a file in the working tree from the index (staging area), discarding unstaged edits. To also discard staged changes and fully revert to the last commit, use git restore --staged <file> first (or git restore --source=HEAD --staged --worktree <file> to do both in one step)

Inspecting changes #

  • git diff — show unstaged changes
  • git diff --staged — show staged changes (equivalent to git diff --cached)
  • git status — show both staged and unstaged changes

Committing and remotes #

  • git commit -m "comment" — save staged changes to the current branch
  • git remote -v — list remote repositories and their fetch/push URLs
  • git remote add origin <URL> — register a remote repository under the name “origin”
  • git remote set-url origin <URL> — change the URL associated with the “origin” remote
  • git push -u origin <local branch>:<remote branch> — push a branch to the remote and set it as the upstream for future git push/git pull

Stash #

  • git stash — save local changes and remove them from the working tree
  • git stash apply — reapply the most recently stashed changes
  • git stash clear — remove all stashed changes

Merge #

  • git merge <branch> — merge the given branch into the current branch
  • git merge --abort — cancel an in-progress merge and return to the pre-merge state
Ryo Ueda
Author
Ryo Ueda
Cloud Network Engineer from Japan. Currently working in Toronto, Canada. Sharing my journey and knowledge in Cloud & Networking.