Git Commands Cheatsheet 2026
Quick reference guide for essential Git commands, branching workflows, remote repositories, stashing, and rollbacks.
Interactive Skill Mastery
Mark commands as learned to build your customized reference tracker. Retained locally in this browser.
Basics
When to Use
When starting a brand-new coding project on your local machine and you want to track changes with version control from scratch.
Common Mistakes
Running git init inside an already-existing Git repository, which creates nested .git configurations and causes tracking confusion.
Shortcut / Pro-Tip
Ctrl + ` (Open Terminal in VS Code) -> type git initExample
mkdir my-new-project
cd my-new-project
git initOutput Example
Initialized empty Git repository in /Users/dev/my-new-project/.git/When to Use
When you want to download a complete copy of an existing remote repository (from GitHub, GitLab, etc.) to your local machine.
Common Mistakes
Forgetting to change directory (cd) into the newly cloned directory before trying to run other git commands.
Shortcut / Pro-Tip
None direct. Clipboard paste: Shift+Insert (Linux) or Cmd+V (macOS)Example
git clone https://github.com/facebook/react.gitOutput Example
Cloning into 'react'...
remote: Enumerating objects: 15423, done.
remote: Counting objects: 100% (231/231), done.
Receiving objects: 100% (15423/15423), done.When to Use
When you want to see which files have been modified, which are staged for the next commit, and which are untracked.
Common Mistakes
Assuming changes are staged automatically just because they appear in green or red lists.
Shortcut / Pro-Tip
Configure alias: git config --global alias.st status -> then run 'git st'Example
git statusOutput Example
On branch main
Your branch is up to date.
Changes not staged for commit:
(use "git add <file>..." to update)
modified: src/App.tsxWhen to Use
When you want to stage changes in a specific file to prepare them for your next commit.
Common Mistakes
Adding a file with unsaved editor changes, causing the committed version to miss recent updates.
Shortcut / Pro-Tip
Tab autocompletion: Type 'git add src/c' and press Tab to complete pathsExample
git add src/components/Header.tsxOutput Example
(No output means success. Run 'git status' to verify the staged files)When to Use
When you want to stage all modified, deleted, and newly created files in your working directory.
Common Mistakes
Accidentally staging temporary build artifacts, log files, or API secrets (make sure you configure .gitignore first!).
Shortcut / Pro-Tip
Alias: git config --global alias.a "add ." -> then run 'git a'Example
git add .Output Example
(Stages all changes in current tree recursively. No output returned)When to Use
When you want to save your currently staged changes to your local commit history with a descriptive message.
Common Mistakes
Writing vague commit messages like 'fixed bug' or 'updates' which make historical auditing useless.
Shortcut / Pro-Tip
Command history: Up Arrow in terminal to recall recent commit structuresExample
git commit -m "feat: implement search filter on cheatsheets hub"Output Example
[main a8f9c2d] feat: implement search filter on cheatsheets hub
2 files changed, 45 insertions(+), 12 deletions(-)When to Use
Use when you want to execute operations related to 'log' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git log --onelineOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)Branching
When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git branchOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git branch <name>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git checkout <branch>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
Use when you want to execute operations related to 'checkout' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git checkout -b <name>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git merge <branch>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git branch -d <branch>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git branch -D <branch>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)Remote
When to Use
Use when you want to execute operations related to 'remote' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git remote add <name> <url>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are working on a new feature, hotfix, or chore, and want to isolate your edits safely away from the main production branch.
Common Mistakes
Creating branch names that are confusing or violating team conventions (e.g., use 'feature/login' rather than just 'login-work').
Shortcut / Pro-Tip
Use 'git checkout -' to quickly switch back to your previously active branch.Example
git push -u origin <branch>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When synchronizing local branch commits with your remote repository hosted on GitHub, GitLab, or Bitbucket.
Common Mistakes
Pushing raw, uncompiled code that breaks unit tests, or overwriting work with a force push (--force).
Shortcut / Pro-Tip
Verify your local target branch matches remote branch before pushing to avoid tracking errors.Example
git pullOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When synchronizing local branch commits with your remote repository hosted on GitHub, GitLab, or Bitbucket.
Common Mistakes
Pushing raw, uncompiled code that breaks unit tests, or overwriting work with a force push (--force).
Shortcut / Pro-Tip
Verify your local target branch matches remote branch before pushing to avoid tracking errors.Example
git fetchOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
Use when you want to execute operations related to 'remote' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git remote -vOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)Undo
When to Use
When you made a mistake in your code, committed unwanted files, or need to step back to a fully functional historical state.
Common Mistakes
Using 'git reset --hard' when you have unstashed work, as it will wipe out all uncommitted local modifications irrevocably.
Shortcut / Pro-Tip
Use 'git reflog' to find lost commits even after a hard reset or accidental branch deletion.Example
git reset --hard HEAD~1Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you made a mistake in your code, committed unwanted files, or need to step back to a fully functional historical state.
Common Mistakes
Using 'git reset --hard' when you have unstashed work, as it will wipe out all uncommitted local modifications irrevocably.
Shortcut / Pro-Tip
Use 'git reflog' to find lost commits even after a hard reset or accidental branch deletion.Example
git reset --soft HEAD~1Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
Use when you want to execute operations related to 'checkout' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git checkout -- <file>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you made a mistake in your code, committed unwanted files, or need to step back to a fully functional historical state.
Common Mistakes
Using 'git reset --hard' when you have unstashed work, as it will wipe out all uncommitted local modifications irrevocably.
Shortcut / Pro-Tip
Use 'git reflog' to find lost commits even after a hard reset or accidental branch deletion.Example
git revert <commit>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
Use when you want to execute operations related to 'commit' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git commit --amendOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)Stash
When to Use
When you are mid-way through a feature, but need to pull hotfixes immediately and want a clean slate without losing current modifications.
Common Mistakes
Leaving work in stashes for weeks and forgetting what they contain. Run 'git stash list' frequently.
Shortcut / Pro-Tip
Use 'git stash save "descriptive message"' to label stashed changes for easy retrieval.Example
git stashOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are mid-way through a feature, but need to pull hotfixes immediately and want a clean slate without losing current modifications.
Common Mistakes
Leaving work in stashes for weeks and forgetting what they contain. Run 'git stash list' frequently.
Shortcut / Pro-Tip
Use 'git stash save "descriptive message"' to label stashed changes for easy retrieval.Example
git stash listOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are mid-way through a feature, but need to pull hotfixes immediately and want a clean slate without losing current modifications.
Common Mistakes
Leaving work in stashes for weeks and forgetting what they contain. Run 'git stash list' frequently.
Shortcut / Pro-Tip
Use 'git stash save "descriptive message"' to label stashed changes for easy retrieval.Example
git stash popOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are mid-way through a feature, but need to pull hotfixes immediately and want a clean slate without losing current modifications.
Common Mistakes
Leaving work in stashes for weeks and forgetting what they contain. Run 'git stash list' frequently.
Shortcut / Pro-Tip
Use 'git stash save "descriptive message"' to label stashed changes for easy retrieval.Example
git stash applyOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When you are mid-way through a feature, but need to pull hotfixes immediately and want a clean slate without losing current modifications.
Common Mistakes
Leaving work in stashes for weeks and forgetting what they contain. Run 'git stash list' frequently.
Shortcut / Pro-Tip
Use 'git stash save "descriptive message"' to label stashed changes for easy retrieval.Example
git stash clearOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)Tags
When to Use
Use when you want to execute operations related to 'tag' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git tag <tagname>Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
Use when you want to execute operations related to 'tag' on your local repository.
Common Mistakes
Forgetting to stage files first, or performing this on the wrong active branch.
Shortcut / Pro-Tip
Setup alias in git config --global alias.<shortname> <command>Example
git tag -a <tagname> -m "<message>"Output Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)When to Use
When synchronizing local branch commits with your remote repository hosted on GitHub, GitLab, or Bitbucket.
Common Mistakes
Pushing raw, uncompiled code that breaks unit tests, or overwriting work with a force push (--force).
Shortcut / Pro-Tip
Verify your local target branch matches remote branch before pushing to avoid tracking errors.Example
git push origin --tagsOutput Example
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)Git Best Practices
1Commit Early, Commit Often
Keep your commits small, single-purpose, and focused. This makes rollback and merging significantly simpler.
2Write Imperative Commit Messages
Use present tense and start with an active verb (e.g., 'feat: implement search' instead of 'implemented search').
3Always Use Feature Branches
Never commit directly to 'main' or 'production'. Create feature/bugfix branches and merge via pull requests.
4Configure .gitignore Properly
Ensure build folders (dist, node_modules), system files (.DS_Store), and secret variables (.env) are never tracked.
5Keep Your Repository Clean
Delete local and remote branches after they have been successfully merged into main.
Common Git Errors & Solutions
Fatal: Refusing to merge unrelated histories
Run: git pull origin main --allow-unrelated-histories
Error: Push rejected (non-fast-forward)
Your local branch is out of sync. Pull latest remote updates: git pull, resolve conflicts, and push again.
Committed to the wrong branch
Run: git reset --soft HEAD~1 to undo commit, git stash to save changes, git checkout correct-branch, and git stash pop.
Forgot to include a file in the last commit
Stage the forgotten file: git add <file>, then run: git commit --amend --no-edit to update last commit.
Merge Conflicts
Open conflict files, review conflict markers (<<<<<<<, =======, >>>>>>>), keep the correct code, remove markers, run git add and git commit.
Common Git Interview Questions
Q1What is the difference between git reset --hard and git reset --soft?
git reset --soft moves HEAD back but preserves staged changes in the index and working directory. git reset --hard destroys both the commit, index state, and all uncommitted working directory changes.
Q2What is the difference between git fetch and git pull?
git fetch downloads objects, metadata, and commits from the remote repository to your local tracker, but does not merge them. git pull runs git fetch first and then immediately runs git merge to update your active branch.
Q3How do you undo a commit that has already been pushed to a public remote?
Use git revert <commit-hash>. This generates a brand new commit that applies the exact opposite changes, preserving collaborative history safely without rewriting past commits.
Q4What is Git Rebase and when should you use it?
Rebase rewrites commit history by moving or combining a sequence of commits to a new base commit. It creates a linear history. It should only be used on private branches before merging, and never on shared public branches.
Q5What is the git stash command used for?
git stash temporarily shelves (saves) all local uncommitted changes (both staged and unstaged) in a clean working buffer, letting you pull hotfixes or switch branches, and retrieve them later with git stash pop.
Related Resources
GitHub API Playground
Interactive sandbox to test and explore real-time GitHub REST API endpoints.
HTTP Status Codes
A Senior Developer's guide and decision tree for returning optimal HTTP responses.
REST API Tester
Send HTTP requests (GET, POST, PUT, DELETE) and analyze live API responses.
JSON Formatter
Beautify, format, and validate JSON payloads with visual syntax highlighting.
Git Article: Best Practices
A complete engineer's guide to professional Git workflows, linear histories, and credential security.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com