Git command reference
Everyday git commands with plain explanations, including how to undo things.
44 commands.
Start
- git init
- Make the current folder a repository.
- git clone <url>
- Copy a remote repository to this device.
Everyday
- git status
- Show what has changed and what is staged.
- git add <file>
- Stage a file for the next commit. Use git add -p to pick parts of it.
- git commit -m "<message>"
- Save the staged changes as a commit.
- git diff
- Show changes that are not staged. Add --staged for those that are.
- git log --oneline --graph
- Show history as one line per commit, with branches drawn.
- git show <commit>
- Show what one commit changed.
- git blame <file>
- Show who last changed each line of a file.
Branches
- git branch
- List local branches. Add -a to include remote ones.
- git switch <branch>
- Move to another branch.
- git switch -c <branch>
- Create a branch and move to it.
- git merge <branch>
- Bring the commits of another branch into this one.
- git rebase <branch>
- Replay your commits on top of another branch, for a straight history.
- git branch -d <branch>
- Delete a branch that has been merged. Use -D to delete it anyway.
- git cherry-pick <commit>
- Copy one commit onto the current branch.
Remotes
- git fetch
- Download new commits without changing your files.
- git pull
- Fetch, then merge into the current branch. Add --rebase to rebase.
- git push
- Send your commits to the remote.
- git push -u origin <branch>
- Push a new branch and remember where it goes.
- git push --force-with-lease
- Overwrite the remote branch, but only if nobody else pushed in the meantime.
- git remote -v
- List the remotes and their addresses.
Set work aside
- git stash
- Put uncommitted changes away and get a clean folder.
- git stash pop
- Bring the last stashed changes back.
- git stash list
- List everything that is stashed.
- git worktree add <path> <branch>
- Check out a second branch in another folder.
Undo
- git restore <file>
- Throw away changes to a file that are not staged.
- git restore --staged <file>
- Unstage a file and keep its changes.
- git commit --amend
- Change the last commit: its message, its files or both.
- git reset --soft HEAD~1
- Undo the last commit and keep its changes staged.
- git reset --hard HEAD~1
- Undo the last commit and throw its changes away. This cannot be undone easily.
- git revert <commit>
- Add a new commit that reverses an old one. Safe for shared branches.
- git reflog
- List where HEAD has been, to find commits that seem lost.
- git clean -fd
- Delete files that are not tracked. Add -n first to see what would go.
Tags
- git tag <name>
- Mark the current commit with a name.
- git tag -a <name> -m "<message>"
- Make a tag with a message and an author.
- git push --tags
- Send your tags to the remote.
Find things
- git bisect start
- Begin a search for the commit that introduced a bug.
- git grep "<text>"
- Search tracked files for some text.
- git log -S "<text>"
- Find the commits that added or removed some text.
- git log -- <file>
- Show the commits that touched one file.
Set up
- git config --global user.name "<name>"
- Set the name on your commits.
- git config --global user.email "<email>"
- Set the email address on your commits.
- git config --global init.defaultBranch main
- Name the first branch of new repositories main.
Missing a feature or found a bug in Git command reference? Tell me on X (@_andrewtran) or by email.
How to use Git command reference
- Type what you want to do, such as undo or stash.
- Read the matching commands and what each does.
- Copy a command, then replace the parts in angle brackets.
Questions and answers
How do I undo my last commit?
git reset --soft HEAD~1 removes the commit and keeps your changes. If the commit is already pushed, use git revert to add a commit that reverses it.
I think I lost a commit. Is it gone?
Probably not. git reflog lists everywhere HEAD has been, and you can check out or cherry-pick a commit from that list.
Related tools
Regex pattern library
Ready-made regular expressions for emails, URLs, dates, IP addresses, UUIDs and more.SQL formatter
Format SQL with clean indentation for PostgreSQL, MySQL, SQLite and SQL Server, or minify it to one line.HTML formatter
Beautify HTML with consistent indentation, or minify it by removing comments and spare whitespace.CSS formatter
Beautify CSS rules and media queries, or minify CSS with strings and comments handled safely.