Git Commands Cheat Sheet: Essential Commands for Every Developer
The ultimate Git commands reference with clear examples. Covers basic operations, branching, merging, rebasing, stashing, and advanced Git workflows.
Getting Started: Init, Clone, and Config
Every Git journey starts with 'git init' to create a new repository or 'git clone URL' to copy an existing one. Configure your identity with 'git config --global user.name "Your Name"' and 'git config --global user.email your@email.com'. Useful global settings include 'git config --global init.defaultBranch main' to set the default branch name, 'git config --global pull.rebase true' for cleaner pull behavior, and 'git config --global core.editor "code --wait"' to use VS Code for commit messages. View all settings with 'git config --list'. These one-time configurations save headaches on every future repository.
Daily Workflow: Add, Commit, Push, Pull
The core Git cycle is: make changes, stage them, commit, and push. 'git status' shows what has changed. 'git add file.txt' stages a specific file; 'git add .' stages everything. 'git commit -m "descriptive message"' creates a snapshot. 'git push origin main' sends commits to the remote. 'git pull origin main' fetches and merges remote changes. Write commit messages in the imperative mood: 'Add user authentication' not 'Added user authentication.' Use 'git diff' to review unstaged changes and 'git diff --staged' for staged changes. A git command generator can help construct complex commands when you are unsure of the exact syntax.
Branching and Merging Strategies
Create a branch with 'git checkout -b feature-name' or the newer 'git switch -c feature-name'. List branches with 'git branch -a' (including remotes). Merge a feature branch into main with 'git checkout main' then 'git merge feature-name'. For a cleaner history, use 'git rebase main' while on the feature branch to replay your commits on top of main. Delete merged branches with 'git branch -d feature-name' locally and 'git push origin --delete feature-name' remotely. Resolve merge conflicts by editing the conflicted files, removing the conflict markers, then staging and committing the resolution.
Recommended Resources
AI pair programmer that helps you write code faster.
Cloud infrastructure for developers. Get $200 free credit.
Sponsored · We may earn a commission at no cost to you
Undoing Changes: Reset, Revert, and Stash
Discard unstaged changes to a file with 'git checkout -- file.txt' or 'git restore file.txt'. Unstage a file with 'git reset HEAD file.txt' or 'git restore --staged file.txt'. Undo the last commit but keep changes with 'git reset --soft HEAD~1'. Undo the last commit and discard changes with 'git reset --hard HEAD~1' (dangerous — data loss). Safely undo a pushed commit with 'git revert commit-hash', which creates a new commit that reverses the changes. Temporarily save work in progress with 'git stash', then restore it later with 'git stash pop'. View stashed items with 'git stash list'.
Advanced Commands for Power Users
Interactive rebase with 'git rebase -i HEAD~5' lets you squash, reorder, edit, or drop the last 5 commits. Cherry-pick specific commits with 'git cherry-pick commit-hash'. Find which commit introduced a bug with 'git bisect start', 'git bisect bad', and 'git bisect good commit-hash' for automated binary search. View file history with 'git log --follow -p file.txt'. Find who changed each line with 'git blame file.txt'. Clean up untracked files with 'git clean -fd'. Create lightweight tags with 'git tag v1.0' or annotated tags with 'git tag -a v1.0 -m "Release 1.0"'. Push tags with 'git push --tags'.
Related Free Tools
Related Articles
Frequently Asked Questions
How do I undo my last git commit?
If the commit has not been pushed, use 'git reset --soft HEAD~1' to undo the commit but keep your changes staged, or 'git reset --mixed HEAD~1' to undo the commit and unstage the changes. If the commit has already been pushed, use 'git revert HEAD' to create a new commit that undoes the changes without rewriting history. Avoid 'git reset --hard' unless you are certain you want to permanently discard the changes, as they cannot be easily recovered after a hard reset.
What is the difference between git merge and git rebase?
Both integrate changes from one branch into another, but they do it differently. 'git merge' creates a new merge commit that combines the histories of both branches, preserving the complete branching timeline. 'git rebase' replays your commits on top of the target branch, creating a linear history. Use merge for shared branches and public history. Use rebase for local feature branches before merging to main. Never rebase commits that have been pushed and shared with others, as it rewrites history and causes conflicts for collaborators.
How do I resolve a git merge conflict?
When Git cannot automatically merge, it marks conflicted sections in the files with '<<<<<<< HEAD', '=======', and '>>>>>>> branch-name' markers. Open each conflicted file, decide which changes to keep (or combine both), remove the conflict markers, then run 'git add' on the resolved files and 'git commit' to complete the merge. Use 'git status' to see which files have conflicts. Tools like VS Code highlight conflicts and offer one-click resolution options. To abort a merge entirely, use 'git merge --abort'.