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 workspacegit checkout <branch>— switch to an existing branchgit checkout -b <branch>— create a new branch and switch to itgit branch— list local branchesgit 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 togit 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, usegit restore --staged <file>first (orgit restore --source=HEAD --staged --worktree <file>to do both in one step)
Inspecting changes #
git diff— show unstaged changesgit diff --staged— show staged changes (equivalent togit diff --cached)git status— show both staged and unstaged changes
Committing and remotes #
git commit -m "comment"— save staged changes to the current branchgit remote -v— list remote repositories and their fetch/push URLsgit 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” remotegit push -u origin <local branch>:<remote branch>— push a branch to the remote and set it as the upstream for futuregit push/git pull
Stash #
git stash— save local changes and remove them from the working treegit stash apply— reapply the most recently stashed changesgit stash clear— remove all stashed changes
Merge #
git merge <branch>— merge the given branch into the current branchgit merge --abort— cancel an in-progress merge and return to the pre-merge state