Version Control
Updated for 2026

Git Commands Cheatsheet 2026

Quick reference guide for essential Git commands, branching workflows, remote repositories, stashing, and rollbacks.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 32 Mastered (0%)

Basics

git init
BeginnerBasics
Initialize a new local Git repository in the current folder.

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 init

Example

mkdir my-new-project
cd my-new-project
git init

Output Example

Console / Terminal
Initialized empty Git repository in /Users/dev/my-new-project/.git/
git clone <url>
IntermediateTeam Workflow
Clone a remote repository into a new local directory.

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.git

Output Example

Console / Terminal
Cloning into 'react'...
remote: Enumerating objects: 15423, done.
remote: Counting objects: 100% (231/231), done.
Receiving objects: 100% (15423/15423), done.
git status
IntermediateDebugging
Display the state of the working directory and staging area.

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 status

Output Example

Console / Terminal
On branch main
Your branch is up to date.

Changes not staged for commit:
  (use "git add <file>..." to update)
	modified:   src/App.tsx
git add <file>
BeginnerBasics
Add specified file changes to the staging area (index) for the next commit.

When 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 paths

Example

git add src/components/Header.tsx

Output Example

Console / Terminal
(No output means success. Run 'git status' to verify the staged files)
git add .
BeginnerBasics
Add all current file changes (tracked and untracked) to the staging area.

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

Console / Terminal
(Stages all changes in current tree recursively. No output returned)
git commit -m "<message>"
BeginnerBasics
Record the staged changes with a descriptive, inline message.

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 structures

Example

git commit -m "feat: implement search filter on cheatsheets hub"

Output Example

Console / Terminal
[main a8f9c2d] feat: implement search filter on cheatsheets hub
 2 files changed, 45 insertions(+), 12 deletions(-)
git log --oneline
IntermediateDebugging
Display a simplified, single-line record of commit history.

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 --oneline

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)

Branching

git branch
IntermediateAdvanced
List all local branches in the current repository.

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

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git branch <name>
IntermediateAdvanced
Create a new branch with the specified name based on the current HEAD.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git checkout <branch>
IntermediateAdvanced
Switch from the current branch to another existing branch.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git checkout -b <name>
IntermediateAdvanced
Create a new branch with the specified name and switch to it immediately.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git merge <branch>
IntermediateTeam Workflow
Merge the specified branch's history into your current active branch.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git branch -d <branch>
IntermediateAdvanced
Delete the specified branch safely (requires it to be merged).

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git branch -D <branch>
IntermediateAdvanced
Force delete the specified branch, even if its changes are unmerged.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)

Remote

git remote add <name> <url>
IntermediateTeam Workflow
Connect your local repository to a new remote server/repository.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git push -u origin <branch>
IntermediateTeam Workflow
Upload local branch commits to the remote origin and configure upstream tracking.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git pull
IntermediateTeam Workflow
Fetch changes from the remote server and merge them directly into the active branch.

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 pull

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git fetch
IntermediateTeam Workflow
Retrieve latest metadata and objects from remote without merging them.

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 fetch

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git remote -v
IntermediateTeam Workflow
List all configured remote repositories with their fetch and push URLs.

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 -v

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)

Undo

git reset --hard HEAD~1
AdvancedRecovery
Discard the latest commit, index updates, and all working tree file changes.

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~1

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git reset --soft HEAD~1
AdvancedRecovery
Undo the last commit but preserve your staged file updates.

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~1

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git checkout -- <file>
AdvancedRecovery
Discard uncommitted local changes in a specific file and restore it from HEAD.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git revert <commit>
AdvancedRecovery
Generate a new commit that safely reverses the exact changes of a past commit.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git commit --amend
AdvancedRecovery
Combine staged changes with the last commit and edit the commit message.

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 --amend

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)

Stash

git stash
IntermediateAdvanced
Temporarily shelter all local uncommitted changes to get a clean working 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

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git stash list
IntermediateAdvanced
Display a list of all suspended stashed changes.

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 list

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git stash pop
IntermediateAdvanced
Apply the latest stashed changes and remove them from your stash buffer.

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 pop

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git stash apply
IntermediateAdvanced
Apply the latest stashed changes but keep them stored in the stash buffer.

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 apply

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git stash clear
AdvancedRecovery
Permanently delete all stashed workspaces in the current repository.

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 clear

Output Example

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)

Tags

git tag <tagname>
BeginnerBasics
Create a lightweight local release tag on the current HEAD.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git tag -a <tagname> -m "<message>"
BeginnerBasics
Create an annotated release tag with custom author details and message.

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

Console / Terminal
Success. (Use 'git status' or 'git log' to inspect the updated revision tree)
git push origin --tags
IntermediateTeam Workflow
Upload all local release tags to the remote server repository.

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 --tags

Output Example

Console / Terminal
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

Error

Fatal: Refusing to merge unrelated histories

Solution

Run: git pull origin main --allow-unrelated-histories

Error

Error: Push rejected (non-fast-forward)

Solution

Your local branch is out of sync. Pull latest remote updates: git pull, resolve conflicts, and push again.

Error

Committed to the wrong branch

Solution

Run: git reset --soft HEAD~1 to undo commit, git stash to save changes, git checkout correct-branch, and git stash pop.

Error

Forgot to include a file in the last commit

Solution

Stage the forgotten file: git add <file>, then run: git commit --amend --no-edit to update last commit.

Error

Merge Conflicts

Solution

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.