Git Cheat Sheet
Quick reference from FreeCodingCourses.com
Start a repo
git init- turn the current folder into a repo.
git clone <url>- copy a remote repo to your machine.
git status- what changed, what is staged. Run it constantly.
git config --global user.name 'You'- set your name once per machine.
Stage and commit
git add file.txt- stage one file for the next commit.
git add .- stage everything changed in this folder down.
git commit -m "message"- save the staged snapshot with a message.
git commit -am "message"- stage tracked files and commit in one step.
git log --oneline- compact history, one commit per line.
git diff- see unstaged changes; add --staged for staged.
Branches
git branch- list branches; the current one has a *.
git switch -c feature- create and move to a new branch.
git switch main- move to an existing branch.
git merge feature- merge feature into the branch you are on.
git branch -d feature- delete a merged branch.
Sync with a remote
git remote -v- show the remotes you are connected to.
git push- send your commits to the remote.
git push -u origin feature- push a new branch and track it.
git pull- fetch and merge the remote's new commits.
git fetch- download remote changes without merging yet.
Undo (carefully)
git restore file.txt- throw away unstaged changes to a file.
git restore --staged file.txt- unstage, keep the edits.
git commit --amend- fix the last commit's message or contents.
git revert <hash>- make a new commit that undoes an old one. Safe.
git reset --soft HEAD~1- undo the last commit, keep the changes staged.
Stash work in progress
git stash- shelve uncommitted changes and get a clean tree.
git stash pop- bring the stashed changes back.
git stash list- see what you have shelved.
Unlock the full Git cheat sheet
You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.
No spam, unsubscribe anytime. See our privacy policy.