Git Command Builder
Find the right git command for any situation. Interactive wizard with 30+ common scenarios, copy-paste commands, and safety warnings.
How to Use
- Type what you want to do in the search box (e.g., "undo commit" or "delete branch").
- Browse the filtered list of git scenarios.
- Click a scenario to expand it and see the exact commands with explanations.
- Click Copy to copy any command to your clipboard.
- Look for warning badges on destructive operations before running them.
Understanding the Git Workflow
Git tracks changes across three main areas: the working directory (your actual files), the staging area (changes you have marked to include in the next commit), and the repository (the committed history). Understanding how commands move changes between these areas is the key to mastering Git. For example, git add moves changes from the working directory to the staging area, while git commit moves them from the staging area into the repository history.
Most Git mistakes happen when developers confuse which area they are operating on. The git status command is your best friend — run it frequently to see exactly where your changes are. Files can be in one of four states: untracked, modified, staged, or committed.
Safe vs. Destructive Commands
Git commands fall into two broad categories: safe commands that preserve history and can be undone, and destructive commands that permanently alter or remove data. Commands like git revert, git stash, and git branch are safe — they either create new commits or store data that can be retrieved. Commands like git reset --hard, git clean -fd, and git push --force are destructive — they permanently remove commits, files, or overwrite remote history.
A good rule of thumb: if a command rewrites history or deletes untracked files, always do a dry run first. For git clean, add the -n flag to preview what would be deleted. For git reset, consider using --soft instead of --hard so your changes remain in the staging area.
Branching Strategies for Teams
Most teams use one of three branching models. Git Flow uses long-lived develop and main branches with feature, release, and hotfix branches — ideal for versioned software with scheduled releases. GitHub Flow is simpler: branch off main, make changes, open a pull request, merge back to main — best for continuous deployment. Trunk-based development keeps all work on main with short-lived feature branches lasting hours or days — favored by teams practicing continuous integration.
Regardless of strategy, keep branches short-lived. Long-running branches accumulate merge conflicts and diverge from the main codebase, making integration painful. Aim to merge feature branches within a few days. Use git rebase to keep your feature branch up to date with main before merging.
Undoing Mistakes: A Decision Tree
The right "undo" command depends on where your mistake lives. If you haven't committed yet, use git restore to discard working directory changes or git restore --staged to unstage files. If you committed locally but haven't pushed, use git reset --soft HEAD~1 to undo the commit while keeping changes staged. If you already pushed, use git revert to create a new commit that undoes the changes — this is the only safe option for shared branches because it preserves history for collaborators.
If you accidentally ran git reset --hard and lost commits, there is still hope. The git reflog command shows a log of all HEAD movements, including "deleted" commits. Find the commit hash in the reflog and run git reset --hard <hash> to recover it. Reflog entries are kept for 90 days by default.
Tips for Git Beginners
- Commit often, commit small. Each commit should represent one logical change. This makes it easier to review, revert, and understand the history.
- Write meaningful commit messages. Start with a short summary (50 characters or fewer), then add a blank line followed by a more detailed explanation if needed.
- Use
.gitignorefrom the start. Prevent build artifacts, dependencies (node_modules), environment files (.env), and IDE settings from cluttering your repository. - Pull before you push. Always run
git pull(orgit fetchfollowed bygit merge) before pushing to avoid conflicts on the remote. - Learn
git log --oneline --graph. Visualizing branch history helps you understand merges, rebases, and where branches diverged. - Set up SSH keys. Using SSH instead of HTTPS for remotes eliminates the need to type your password on every push and pull.
Useful Git Configuration
A few global settings can improve your daily Git experience. Set git config --global pull.rebase true to automatically rebase on pull instead of creating merge commits. Enable git config --global rerere.enabled true to let Git remember how you resolved conflicts, automatically applying the same resolution if it sees the same conflict again. And git config --global alias.lg "log --oneline --graph --all" creates a handy shortcut so you can type git lg for a visual branch graph.
Related Tools
Compare text changes with the Diff Checker. Test patterns for Git hooks with the Regex Tester. Format JSON configuration files with the JSON Formatter. Encode credentials for Git config with the Base64 Encoder. Generate UUIDs for commit references with the UUID Generator.
Frequently Asked Questions
- What is the difference between git reset and git revert?
- git reset moves the branch pointer backward, rewriting history — best for local, unpushed commits. git revert creates a new commit that undoes a previous one without rewriting history, making it safe for shared branches.
- How do I undo the last commit without losing my changes?
- Run "git reset --soft HEAD~1". This moves HEAD back one commit while keeping all your changes staged. You can then edit and recommit.
- What does git stash do?
- git stash temporarily saves your uncommitted changes (both staged and unstaged) to a stack, reverting your working directory to the last commit. Use "git stash pop" to restore them later.
- When should I use rebase instead of merge?
- Use rebase to maintain a linear commit history on feature branches before merging into main. Use merge when you want to preserve the full branch history. Never rebase commits that have been pushed to a shared remote branch.
- Is my data sent to a server when using this tool?
- No. This tool runs entirely in your browser. No data is transmitted to any server. It is a static reference of git commands.
AI agent tools available.
The CodeTidy MCP Server gives Claude, Cursor, and other AI agents
access to 47 developer tools. One command: npx @codetidy/mcp