Welcome to Git! This is a quick, copy-paste friendly guide to get you productive fast. If you need deeper explanations, check the official Git docs or the excellent free book Pro Git.
git switch main git pull # update main git merge feature/cool-thing # If conflicts: git status # see conflicted files # edit files to resolve, then: git add <resolved-file> git commit # completes the merge
Sync with remotes
1 2 3 4 5 6 7 8 9
# add remote once git remote add origin https://github.com/owner/repo.git
# push current branch (create upstream) git push -u origin HEAD
# later: pull & push git pull git push
Inspect history
1 2 3 4
git log --oneline --graph --decorate --all git show HEAD~1 # show a specific commit git diff # unstaged changes git diff --staged # staged changes
Undo safely
1 2 3 4 5 6 7
git restore path/to/file # discard unstaged changes in file git restore --staged path/to/file # unstage git commit --amend # edit last commit msg / staged content git revert <commit> # new commit that undoes <commit>
# CAREFUL: rewrites history (use only on your own branches) git reset --hard HEAD~1 # move branch pointer back 1 commit
Stash work-in-progress
1 2 3 4 5 6
git stash push -m "wip: refactor parser" git stash list git stash show -p stash@{0} git stash apply stash@{0} # keep stash # or git stash pop # apply + drop
Tags (releases)
1 2 3
git tag -a v1.0.0 -m "v1.0.0" git push origin v1.0.0 git tag # list tags