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.
| Command | Action |
|---|---|
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 main | Set 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 true | Rebase local commits by default when pulling |
git config --list --show-origin | List 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.
# 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.gitHow 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.
| Command | Action |
|---|---|
git status | Show active working tree and staging status |
git add filename.ts | Stage a specific file for commit |
git add . | Stage all modified and new files in directory |
git add -p | Stage changes interactively chunk-by-chunk |
git commit -m "feat: description" | Record staged changes with message |
git commit --amend --no-edit | Add staged changes to last commit without changing message |
git commit --amend | Edit 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.
# 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-pageHow 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.
# 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-authHow 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).
| Command | Action |
|---|---|
git restore file.ts | Discard local unstaged modifications in file |
git restore --staged file.ts | Unstage a file while preserving local edits |
git reset --soft HEAD~1 | Undo last commit, keeping changes staged |
git reset --mixed HEAD~1 | Undo last commit, keeping changes unstaged |
git reset --hard HEAD~1 | Destructive: 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.
# 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-leaseFrequently 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>.
What to Read Next
- Linux Bash Cheat Sheet: Commands & Scripting — Essential shell commands for developer workflows.
- Vim Cheat Sheet: Modes, Navigation & Editing — Master terminal text editing.
- iScreen Suite: Open-Source Browser Productivity — Build Bento-grid launchpads and custom browser widgets.



