The Essential Git Cheat Sheet: Commands, Workflow, and Best Practices
Git is the most widely used version control system in the world. Whether you're a solo developer or part of a large team, mastering Git is essential for managing your code history and collaborating effectively.
Use this cheat sheet as a quick reference for common Git commands and workflows.
1. Basic Workflow
The core cycle of making changes and saving them to your repository.
| Command | Description | Example |
|---|---|---|
git init |
Initialize a new local Git repository | git init |
git clone |
Create a copy of a remote repository | git clone [url] |
git add |
Stage changes for the next commit | git add . (add all) |
git commit |
Save staged changes to history | git commit -m "feat: login" |
git status |
Show the state of the working directory | git status |
git push |
Upload local commits to remote | git push origin main |
git pull |
Fetch and merge changes from remote | git pull origin main |
git fetch |
Download changes from remote (no merge) | git fetch origin |
2. Branching and Merging
Branches allow you to work on different features or fixes in isolation.
- Create Branch:
git branch [name] - Switch Branch:
git checkout [name](orgit switch [name]) - Create & Switch:
git checkout -b [name] - Merge Branch:
git merge [name](merges [name] into current branch) - Rebase Branch:
git rebase [name](reapplies commits on top of [name]) - Delete Branch:
git branch -d [name]
3. Undoing Changes
How to fix mistakes or revert to a previous state.
- Revert Commit:
git revert [commit-hash](creates a new commit undoing changes) - Reset (Soft):
git reset --soft HEAD~1(undoes commit, keeps changes staged) - Reset (Hard):
git reset --hard HEAD~1(undoes commit and deletes changes!) - Stash Changes:
git stash(temporarily hides uncommitted changes) - Pop Stash:
git stash pop(brings back stashed changes)
4. Inspecting and Debugging
Tools for viewing history and finding bugs.
| Command | Description |
|---|---|
git log |
Show commit history |
git diff |
Show changes between commits or working tree |
git blame [file] |
Show who changed what line in a file |
git show [commit] |
Show details of a specific commit |
git bisect |
Use binary search to find the commit that introduced a bug |
Common Questions (FAQ)
Q: Merge vs Rebase: Which one should I use?
A: Merge creates a new "merge commit" and preserves the complete history and chronology. Rebase rewrites history by moving your commits to the tip of the target branch, creating a cleaner, linear history. Rule of thumb: Never rebase public branches; use it for local cleanup before pushing.
Q: How do I fix a merge conflict?
A: When a conflict occurs, Git will mark the files. 1. Open the file and look for <<<<<<<, =======, and >>>>>>>. 2. Edit the code to keep what you want. 3. git add [file]. 4. git commit.
Q: What is a "detached HEAD"?
A: It means you've checked out a specific commit or tag instead of a branch. You're not on any branch. Any new commits will be lost if you switch away without creating a new branch. To save changes: git checkout -b new-branch-name.
Q: What are the best practices for .gitignore?
A: Always ignore environment secrets (.env), build artifacts (dist/, build/), dependency folders (node_modules/), and OS-specific files (.DS_Store). Use community templates (like those from GitHub) for your specific language or framework.
Related on Tool3M
- Home Page: Discover more developer tools and references to boost your productivity.