All the Git commands you need on one page, grouped by task: setup, committing, history, branches, merging and rebasing, remotes, undo, stash, tags, advanced tools and the GitHub CLI. Bookmark it and keep it open while you work.
đ What You’ll Learn: setup and configuration, starting repositories, staging and committing, history, branches, merge and rebase, remotes, an undo table, stash, tags and releases, advanced commands, GitHub CLI essentials and the top 10 mistakes to avoid.
đ How to Use This Cheat Sheet
All the Git commands you use daily, grouped by task, with links to the full lessons in the Git and GitHub Tutorial. To keep a copy, use your browser’s Print, Save as PDF.
âī¸ Setup and Configuration
git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --list --show-origin
ssh-keygen -t ed25519 -C "you@example.com" # SSH key for GitHub
ssh -T git@github.com # test the connection
Lessons: install, config, SSH keys
đ§ą Start a Repository
git init # new repo in the current folder
git clone <url> # copy a remote repo
git clone --depth 1 <url> # shallow clone
git remote add origin <url>
git push -u origin main # first push with upstream
Lessons: first repository, push an existing project
đž Daily Work: Stage and Commit
git status # what changed? (git status -s for short)
git add file.txt # stage one file
git add . # stage everything here
git add -p # stage parts of files
git commit -m "feat(login): add locked-out user test"
git commit -am "fix: update selector" # stage tracked files and commit
git commit --amend --no-edit # add staged changes to the last commit
git diff # unstaged changes
git diff --staged # staged changes
Lessons: staging area, commit messages
đ History
git log --oneline --graph --all
git log -n 5 --stat
git log --author="Asha" --since="2 weeks ago"
git log -S "waitForTimeout" # commits that added/removed text
git log -- tests/login.spec.ts # history of one file
git show HEAD~1 # one commit
git blame -L 10,20 file.ts # who changed these lines
Lesson: log, show and diff
đŋ Branches
git branch # list (-a all, -vv with tracking)
git switch -c feature/x # create and switch
git switch main # switch
git branch -m old new # rename
git branch -d feature/x # delete merged (-D force)
git push -u origin feature/x # publish a branch
git push origin --delete feature/x # delete on GitHub
Lesson: branches
đ Merge and Rebase
git merge feature/x # merge into current branch
git merge --no-ff feature/x
git merge --squash feature/x
git merge --abort
git rebase main # replay current branch on main
git rebase -i HEAD~3 # squash, reword, reorder
git rebase --continue | --skip | --abort
git cherry-pick <hash> # copy one commit here
Lessons: merge, conflicts, rebase, interactive rebase, cherry-pick
đ Remotes: Sync with GitHub
git remote -v
git fetch # download, do not merge (--prune to clean up)
git pull # fetch + merge (--rebase to rebase)
git push
git push --force-with-lease # after rewriting YOUR branch
git remote add upstream <original-url> # forks
git fetch upstream && git merge upstream/main
Lessons: remotes, clone, push, pull, fetch vs pull, forks
âŠī¸ Undo
| Situation | Command |
|---|---|
| Discard edits in a file | git restore file |
| Unstage a file | git restore --staged file |
| Fix the last commit message | git commit --amend -m "msg" |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 |
| Undo last commit, keep changes unstaged | git reset HEAD~1 |
| Throw away last commit and changes | git reset --hard HEAD~1 |
| Undo a pushed commit safely | git revert <hash> |
| Remove untracked files | git clean -n then git clean -fd |
| Recover lost commits | git reflog then git reset --hard <hash> |
Lessons: undo changes, reflog
đĻ Stash
git stash push -m "WIP login" # -u to include untracked files
git stash list
git stash pop # apply and remove
git stash apply stash@{1} # apply and keep
git stash drop stash@{1}
Lesson: stash
đˇī¸ Tags and Releases
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0 # or --tags
git tag -d v1.0.0 && git push origin --delete v1.0.0
gh release create v1.0.0 --generate-notes
Lesson: tags
đ§° Advanced
git bisect start; git bisect bad; git bisect good v1.0.0
git bisect run npm test # automate; git bisect reset when done
git submodule update --init --recursive
git check-ignore -v file # why is it ignored?
git rm --cached .env # stop tracking, keep the file
git cat-file -p HEAD # look inside a commit
Lessons: bisect, submodules, .gitignore, internals
â¨ī¸ GitHub CLI Essentials
gh auth login
gh repo create my-repo --private --clone
gh pr create --fill
gh pr checkout 57
gh pr merge 57 --squash --delete-branch
gh run watch
gh run view <id> --log-failed
Lesson: GitHub CLI
đĢ Top 10 Mistakes to Avoid
- Committing secrets or
.envfiles. - Committing
node_modulesor test reports instead of using .gitignore. - Working directly on
mainin a team. - Force-pushing shared branches (use
--force-with-leaseonly on your own). - Rebasing commits others already have.
- Huge commits with messages like “update”.
- Long-lived branches that drift far from main.
- Using
reset --hardwithout checkinggit statusandgit difffirst. - Resolving conflicts without understanding both sides.
- Ignoring failing CI on pull requests.
â FAQ
đ¤ What are the most important Git commands for beginners?
git init, git clone, git status, git add, git commit, git log, git branch, git switch, git merge, git pull and git push.
đ¤ Is there a Git cheat sheet PDF?
Use your browser’s Print, Save as PDF on this page to keep an offline copy.
đ¤ How do I undo the last commit in Git?
git reset --soft HEAD~1 to keep the changes staged, or git revert HEAD if the commit is already pushed.
đ¤ How do I see all branches?
git branch -a lists local and remote-tracking branches.
đ¯ Next Steps
- đŧ Git Interview Questions for Experienced
- đ Module-wise Practice Assignments
- đ Git and GitHub Tutorial: full course
đŦ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in