MeshWorld India LogoMeshWorld.
CheatsheetGitVersion ControlTerminalDeveloper ToolsGitHub6 min read

Git Cheat Sheet: Every Command You Actually Need (2026)

Vishnu
By Vishnu
|Updated: Jul 30, 2026
Git Cheat Sheet: Every Command You Actually Need (2026)

Git is the fundamental version control system powering modern software development. Whether you are managing feature branches, resolving merge conflicts, or working on concurrent tasks via Git Worktree, mastering essential Git commands is crucial for maintaining a clean, auditable commit history.

Key Takeaways

  • Use modern Git commands (`git switch` and `git restore`) instead of overloaded legacy `git checkout`.
  • Leverage `git worktree` to work on multiple active branches simultaneously in separate directories.
  • Stage code interactively using `git add -p` to craft atomic, logical commits.
  • Safely undo local mistakes with `git reset --soft`, `git restore`, and `git commit --amend`.

What are the essential Git setup and configuration commands?

Git configuration manages user identity, SSH keys, default branch names, line ending handling, and default text editors across your operating system.

CommandAction
git config --global user.name "Your Name"Set your global commit author name
git config --global user.email "[email protected]"Set your global commit author email
git config --global init.defaultBranch mainSet main as the default branch for new repos
git config --global core.editor "code --wait"Set VS Code as the default editor
git config --global pull.rebase trueRebase local commits by default when pulling
git config --list --show-originList all active config values and source files

How do you initialize, clone, and manage repositories in Git?

To initialize a new Git repository locally, run git init, or use git clone <url> to download an existing remote codebase. Cloning creates a full local copy of the remote repository history, configuration, and default branch.

bash
# Initialize a new Git repository in the current directory
git init

# Clone a remote repository over SSH
git clone [email protected]:username/repository.git

# Clone into a specific local folder name
git clone [email protected]:username/repository.git my-custom-folder

# Perform a shallow clone (fetches only the latest commit to save bandwidth)
git clone --depth 1 [email protected]:username/repository.git

How do you stage, commit, and amend code changes cleanly?

Staging prepares modified files for snapshotting using git add, while committing records staged changes into the repository history with git commit. You can also use git commit --amend to modify the most recent commit without creating additional history noise.

CommandAction
git statusShow active working tree and staging status
git add filename.tsStage a specific file for commit
git add .Stage all modified and new files in directory
git add -pStage changes interactively chunk-by-chunk
git commit -m "feat: description"Record staged changes with message
git commit --amend --no-editAdd staged changes to last commit without changing message
git commit --amendEdit the message or files of the most recent commit

What are modern Git branching and switching commands?

Modern Git replaces legacy git checkout with git switch for branch navigation and git restore for file restoration.

bash
# List local branches (* indicates current active branch)
git branch

# Create and switch to a new feature branch
git switch -c feature/login-page

# Switch back to an existing branch
git switch main

# Switch back to the previously active branch (quick toggle)
git switch -

# Delete a merged local branch
git branch -d feature/login-page

# Force delete an unmerged local branch
git branch -D feature/login-page

How do you use Git Worktree to work on multiple branches simultaneously?

Git Worktree allows you to check out multiple branches of the same repository into separate local directories simultaneously. This eliminates the need to stash uncommitted work or switch branches within a single working folder.

bash
# Add a new worktree for a hotfix branch in a separate folder
git worktree add ../hotfix-auth main

# List all active worktree paths
git worktree list

# Remove a worktree directory after completing the work
git worktree remove ../hotfix-auth

How do you undo mistakes, reset commits, and restore files in Git?

Git provides non-destructive options to unstage files (git restore --staged), revert previous commits (git revert), or reset local commit history (git reset).

CommandAction
git restore file.tsDiscard local unstaged modifications in file
git restore --staged file.tsUnstage a file while preserving local edits
git reset --soft HEAD~1Undo last commit, keeping changes staged
git reset --mixed HEAD~1Undo last commit, keeping changes unstaged
git reset --hard HEAD~1Destructive: Discard last commit and all local changes
git revert <commit-hash>Create a new commit that reverses a previous commit

How do you sync, fetch, pull, and push with remote Git repositories?

Remote synchronization transfers commits between local repositories and central git hosts over SSH/HTTPS.

bash
# View configured remote servers and URLs
git remote -v

# Fetch all remote branches without merging into working tree
git fetch origin

# Pull remote changes and rebase local commits on top
git pull --rebase origin main

# Push a new branch to remote and set tracking
git push -u origin feature/login-page

# Force push safely (verifies remote hasn't changed unexpectedly)
git push --force-with-lease

Frequently Asked Questions

What is the difference between git pull and git fetch?

git fetch downloads remote commits and refs without modifying your local working files. git pull combines git fetch with an immediate git merge (or git rebase) to update your current branch.

How do I recover a deleted commit or branch in Git?

Use git reflog to view the log of all local HEAD pointer movements. Locate the commit hash prior to deletion, then create a new branch pointing to that hash via git branch recovery-branch <commit-hash>.


Share_This Twitter / X
Vishnu
Written By

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Enjoyed this article?

Support MeshWorld and help us create more technical content