Git Commands Cheat Sheet: Every Command You Need, Grouped by Task

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.

Advertisement

📖 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

SituationCommand
Discard edits in a filegit restore file
Unstage a filegit restore --staged file
Fix the last commit messagegit commit --amend -m "msg"
Undo last commit, keep changes stagedgit reset --soft HEAD~1
Undo last commit, keep changes unstagedgit reset HEAD~1
Throw away last commit and changesgit reset --hard HEAD~1
Undo a pushed commit safelygit revert <hash>
Remove untracked filesgit clean -n then git clean -fd
Recover lost commitsgit 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

  1. Committing secrets or .env files.
  2. Committing node_modules or test reports instead of using .gitignore.
  3. Working directly on main in a team.
  4. Force-pushing shared branches (use --force-with-lease only on your own).
  5. Rebasing commits others already have.
  6. Huge commits with messages like “update”.
  7. Long-lived branches that drift far from main.
  8. Using reset --hard without checking git status and git diff first.
  9. Resolving conflicts without understanding both sides.
  10. 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

đŸŽŦ Learn Git and GitHub step by step: watch the series on YouTube!

Subscribe to SqaToolsByDY • Practice on sqatools.in

Leave a Comment