Git
GIT CMD Cheatsheet
Just a short list of GIT commands but sorted by usage/need instead. Hopefully it’s useful to you as much as it was to me.
Everyday commands
git status - see what’s changed
git add . - stage all changes
git add <file> - stage one file
git commit -m "message" - create a commit
git push - upload commits to Git
git pull - download + merge changes from Git
Usual workflow
git pull
git status
git add .
git commit -m "message what changed"
git push
New Project (Computer) to GitHub
cd "path/to/project"
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/USER/REPO.git
git push -u origin main
GitHub to Computer
Clone a repo:
git clone https://github.com/USER/REPO.git
Enter it:
cd REPO
Update existing repo:
git pull
Operations on branches
See branches:
git branch
Create + switch:
git switch -c feature-name
Switch:
git switch main
Delete local branch:
git branch -d feature-name
Push new branch:
git push -u origin feature-name
Delete GitHub branch:
git push origin --delete feature-name
Merge
Switch to the branch receiving the changes:
git switch main
Update it:
git pull
Merge:
git merge feature-name
Push:
git push
Cancel a merge
git merge --abort
Merge Conflict
When Git says CONFLICT:
git status
-
Open the conflicted file
-
Choose/fix the correct content
-
Remove
<<<<<<<,=======,>>>>>>>
Then:
git add .
git commit -m "Resolve merge conflict"
git push
Want to cancel everything?
git merge --abort
See What Changed
Current state:
git status
Unstaged changes:
git diff
Staged changes:
git diff --staged
Commit history:
git log --oneline
Pretty history:
git log --oneline --graph --decorate --all
Show a commit:
git show <commit>
Undo / Fix Mistakes
Unstage a file
git restore --staged <file>
Discard changes to a file
git restore <file>
⚠️ Deletes your uncommitted changes
Discard ALL uncommitted changes
git restore .
⚠️ Dangerous
Undo last commit, keep changes
git reset --soft HEAD~1
Undo last commit, keep files but unstage them
git reset HEAD~1
Completely undo last commit + changes
git reset --hard HEAD~1
⚠️ Dangerous
Safely reverse an existing commit
git revert <commit>
Stash
Temporarily hide unfinished changes:
git stash
See stashes:
git stash list
Bring changes back:
git stash pop
Apply without deleting stash:
git stash apply
Delete stash:
git stash drop
Useful when working on A -> need to switch to B -> don’t want to commit A
GitHub / Remote
See GitHub connection:
git remote -v
Add GitHub:
git remote add origin <URL>
Change GitHub URL:
git remote set-url origin <URL>
Download remote information:
git fetch
Download + integrate:
git pull
Upload:
git push
Remove Files
Remove file through Git:
git rm <file>
Remove directory:
git rm -r <directory>
See untracked files that would be deleted:
git clean -n
Delete untracked files:
git clean -f
⚠️ git clean can permanently delete files
.gitignore
Common .gitignore entries:
.env
node_modules/
*.log
dist/
build/
.vscode/
.DS_Store
After creating .gitignore:
git add .
git commit -m "Add gitignore"
Useful Checks
Git version:
git --version
Git user:
git config --global user.name
Git email:
git config --global user.email
Current branch:
git branch --show-current
Remote:
git remote -v
Common Errors
Error message: “src refspec main does not match any”
Usually you’re on master:
git branch
git branch -M main
git push -u origin main
Error message: “Your branch is ahead of origin/main”
You have commits that aren’t on GitHub:
git push
Error message: “Your branch is behind origin/main”
GitHub has commits you don’t have:
git pull
Error message: “nothing to commit, working tree clean”
Everything is already committed. Nothing to do.
Error message: “fatal: not a git repository”
You’re in the wrong folder:
cd path/to/project
Check:
git status
CONFLICT
Fix the files:
git status
git add .
git commit
git push
Or cancel:
git merge --abort
General Git Flow
EDIT FILES >> git status >> git add . STAGING >> git commit -m "message" >> LOCAL REPOSITORY >> git push >> GITHUB
To get changes:
GITHUB >> git pull >> LOCAL REPOSITORY
TLDR
| I want to… | Command |
|---|---|
| See what’s happening | git status |
| Get GitHub changes | git pull |
| Stage everything | git add . |
| Save a version | git commit -m "message" |
| Upload to GitHub | git push |
| Create a branch | git switch -c <name> |
| Switch branch | git switch <name> |
| Merge branch | git merge <name> |
| See history | git log --oneline |
| Undo staging | git restore --staged <file> |
| Discard file changes | git restore <file> |
| Temporarily save work | git stash |
| Restore stash | git stash pop |
| Cancel merge | git merge --abort |
| See GitHub connection | git remote -v |
Almost all the time just do
git pull
git status
git add .
git commit -m "what I changed"
git push
If you’re unsure what’s happening: git status