The git commands you use every week, each with a one-line explanation, plus an undo section organised by situation: fix the last commit, take back a push, recover a deleted branch or move a commit off main. Search for a command or for what went wrong.
49 entries
Set up
git config --global user.name "Your Name"Name written into every commit you make on this machine.
git config --global user.email "[email protected]"Email for your commits. Use the one linked to your GitHub or GitLab account so commits show up on your profile.
git config --global init.defaultBranch mainNew repositories start on main instead of master.
git initTurns the current folder into a git repository.
git clone <url> [folder]Copies a remote repository, with full history, into a new folder.
git clone [email protected]:owner/repo.git
git clone --depth 1 <url>Shallow clone with only the latest commit. Faster for CI and large repos.
Daily work
git statusWhat changed, what is staged, and which branch you are on. Run it often.
git add <file>Stages a file for the next commit. git add . stages everything in the current folder.
git add -pStages changes hunk by hunk, so one commit holds one idea.
git commit -m "message"Records the staged changes with a message.
git commit -m "fix(cart): round GST to two decimals"
git commit -am "message"Stages every tracked, modified file and commits in one step. New files are not included.
git diffUnstaged changes. git diff --staged shows what will go into the next commit.
git log --oneline --graph --decorateCompact history with branch lines. Add -n 20 to limit it.
git show <commit>The message and changes of one commit.
git blame <file>Who last changed each line, and in which commit.
Branches
git switch -c <branch>Creates a branch and moves to it (newer form of git checkout -b).
git switch -c feature/PROJ-142-upi-refunds
git switch <branch>Moves to an existing branch.
git branchLists local branches. -a includes remote ones, -vv shows what each tracks.
git branch -d <branch>Deletes a merged branch. -D forces delete of an unmerged one.
git branch -m <new-name>Renames the current branch.
git merge <branch>Brings another branch into the current one, with a merge commit if they diverged.
git rebase <branch>Replays your commits on top of another branch for a straight history. Do not rebase commits others have already pulled.
git cherry-pick <commit>Copies one commit onto the current branch, for example a hotfix from main into a release branch.
Remotes
git remote -vLists remotes and their URLs.
git remote add origin <url>Connects a local repository to a remote.
git fetchDownloads new commits and branches without changing your files.
git pullFetch, then merge (or rebase, if pull.rebase is set) into the current branch.
git pull --rebasePuts your local commits on top of what was pulled, avoiding merge commits.
git push -u origin <branch>Pushes a new branch and remembers the remote, so next time git push is enough.
git push --force-with-leaseOverwrites the remote branch after a rebase, but refuses if someone else pushed in the meantime. Safer than --force.
git push origin --delete <branch>Deletes a branch on the remote.
Stash and tags
git stashPuts uncommitted changes aside so you can switch branches. -u also stashes new untracked files.
git stash popBrings the last stash back and removes it from the list. git stash apply keeps it.
git stash listAll stashes, newest first.
git tag -a v1.4.0 -m "Release 1.4.0"Creates an annotated tag for a release.
git push origin --tagsPushes tags, which git push does not send by default.
Undo recipes
Pick the situation you are in. Commands marked "rewrites history" are fine on your own branch, not on one others have pulled.
Discard changes to a fileThrow away unstaged edits to one file and go back to the last commit.
git restore <file>
The edits are gone for good.
Unstage a fileTook git add too far? Keep the edits, just remove them from the next commit.
git restore --staged <file>
Fix the last commit messageTypo in the message of the commit you just made.
git commit --amend -m "new message"
Rewrites history: only before pushing, or push with --force-with-lease.
Add a forgotten file to the last commitStage the file, then amend without changing the message.
git add <file> git commit --amend --no-edit
Rewrites history.
Undo the last commit, keep the changesThe commit goes, your edits stay staged so you can redo it.
git reset --soft HEAD~1
Rewrites history.
Undo the last commit and its changesRemoves the commit and everything in it from your working copy.
git reset --hard HEAD~1
Deletes work. Uncommitted changes are lost too; check git status first.
Undo a commit that is already pushedMakes a new commit that reverses an old one. Safe on shared branches.
git revert <commit>
Get back a commit after a bad resetThe reflog lists every position HEAD has been at, so "lost" commits can usually be found for about 90 days.
git reflog git reset --hard <commit-from-reflog>
Committed to the wrong branchMove the last commit from main to a new branch.
git branch feature/my-work git reset --hard HEAD~1 git switch feature/my-work
Rewrites main locally; do it before pushing.
Restore a deleted branchFind the last commit of the branch in the reflog and recreate it there.
git reflog git branch <branch> <commit>
Stop a merge or rebase in progressConflicts got messy? Go back to where you started.
git merge --abort git rebase --abort
Remove a file from git but keep it on diskFor a file that should have been in .gitignore, such as .env.
git rm --cached .env echo ".env" >> .gitignore
If the file had secrets and was pushed, rotate them: they remain in history.
Delete untracked filesPreview first with -n, then remove. -d includes folders.
git clean -n git clean -fd
Deleted files do not go to the recycle bin.
Setup, daily work, branches, remotes, stash and tags
Thirteen undo recipes written as situations, with the exact commands
Warnings on commands that delete work or rewrite shared history
Modern commands (git switch, git restore) alongside the classic ones
Copy any command in one click
Search by command or by problem, such as "undo commit"
Learn the small set of commands that covers most daily work, without reading the whole manual.
Committed to main, pushed a secret, lost a branch? Find the situation and follow the recipe.
Share one page with the commands and branch habits your team expects.
git reset moves your branch back and drops commits from it; that rewrites history, which is fine on your own branch but breaks things for anyone who already pulled. git revert adds a new commit that undoes an old one, so it is the safe choice for main or any shared branch.
Almost nothing committed is lost straight away. git reflog lists where HEAD has been, including commits a reset removed, and git keeps those entries for about 90 days by default. Uncommitted changes are different: git restore and git reset --hard remove them for good.
Conventional Commit Generator
Commit messages with type, scope, breaking changes and footers, checked like commitlint
Git Branch Name Generator
Clean, valid branch names from a ticket number and title
.gitignore Generator
One merged .gitignore for your languages, frameworks, editors and OS
Diff Checker
Compare text differences
JSON Formatter
Format and validate JSON
JSON Sort Keys
Alphabetize JSON keys at every level
JSON Flatten
Nested JSON to dot-path keys
JSON Unflatten
Dot-path keys back to nested JSON
Built by Edesy
Edesy builds AI products for Indian businesses. Start self-serve, no demo call needed.
AI agents that answer and make phone calls 24/7. Handle appointments, inquiries and support in Indian and global languages.
₹50 free credit. No demo call needed.