25 Git Commands Every Developer Should Know (With Examples)
The Git commands you will use every week, grouped by task, with real examples and the safe way to undo mistakes before they reach your team.
· 7 min read
Contents
- Setting up
- 1. git config — tell Git who you are
- 2. git init and git clone
- The everyday loop
- 3. git status
- 4. git add
- 5. git commit
- 6. git diff
- 7. git log
- Branches
- 8. git switch — move between branches
- 9. git branch
- 10. git merge
- 11. git rebase
- Working with remotes
- 12. git remote
- 13. git fetch
- 14. git pull
- 15. git push
- Saving work in progress
- 16. git stash
- Undoing things
- 17. git restore
- 18. git reset
- 19. git revert
- 20. git reflog — your safety net
- Investigating history
- 21. git blame
- 22. git show
- 23. git bisect — find the commit that broke something
- Cleaning up
- 24. git clean
- 25. git cherry-pick
- Aliases worth adding
- FAQ
- What is the difference between git fetch and git pull?
- Should I use merge or rebase?
- How do I undo git add?
- Further reading
Most developers use a dozen Git commands every day and look up the rest when something goes wrong. This guide covers both: the everyday commands, grouped by the task you are trying to do, plus the handful you need when you have to undo something.
Every command here works on Git 2.23 and newer (released in 2019), which is what nearly every system ships today. Check yours with git --version.
Setting up
1. git config — tell Git who you are
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
These settings live in ~/.gitconfig. Use --local instead of --global inside a repository to override them for one project, which is handy for separating work and personal emails.
2. git init and git clone
git init # start tracking the current folder
git clone https://github.com/user/repo.git # copy an existing repository
git clone --depth 1 <url> # only the latest commit, much faster for big repos
The everyday loop
3. git status
Shows which files are modified, staged or untracked. Run it constantly; git status -sb gives a compact version with the branch name.
4. git add
git add file.py # stage one file
git add . # stage everything in the current folder
git add -p # review and stage changes hunk by hunk
git add -p is the single most underrated Git command. It shows each change and lets you stage only the parts that belong in this commit, which keeps commits small and reviewable.
5. git commit
git commit -m "Fix rounding error in invoice totals"
git commit --amend # change the last commit (message or content)
Write messages in the imperative ("Fix…", "Add…"), the same style Git uses itself. Only amend commits you have not pushed yet.
6. git diff
git diff # unstaged changes
git diff --staged # what you are about to commit
git diff main...feature # what the feature branch adds compared to main
7. git log
git log --oneline --graph --decorate -20
git log -p path/to/file.py # history of one file, with diffs
git log --author="Ana" --since="2 weeks ago"
Branches
8. git switch — move between branches
git switch main
git switch -c feature/login # create and switch
git switch was added in Git 2.23 to replace the branch-related half of git checkout, which did too many unrelated things. The old commands still work.
9. git branch
git branch # list local branches
git branch -a # include remote branches
git branch -d old-feature # delete a merged branch
git branch -D old-feature # force delete, even if unmerged
10. git merge
git switch main
git merge feature/login
If both branches changed the same lines, Git stops and marks the conflict in the file. Edit the file, git add it, then git commit to finish.
11. git rebase
git switch feature/login
git rebase main
Rebasing replays your branch's commits on top of the latest main, producing a straight history. The golden rule: never rebase commits that other people have already pulled.
Working with remotes
12. git remote
git remote -v
git remote add origin git@github.com:user/repo.git
13. git fetch
Downloads new commits from the remote without changing your files. Safe to run at any time.
14. git pull
git pull # fetch + merge
git pull --rebase # fetch + rebase, avoids noisy merge commits
Set git config --global pull.rebase true if you prefer rebasing by default.
15. git push
git push -u origin feature/login # first push of a new branch
git push --force-with-lease # safe force-push after a rebase
Always prefer --force-with-lease over --force: it refuses to overwrite the remote if someone else pushed in the meantime.
Saving work in progress
16. git stash
git stash # shelve uncommitted changes
git stash -u # include untracked files
git stash list
git stash pop # re-apply the latest stash and drop it
Perfect when you need to switch branches to fix something urgent.
Undoing things
This is where most Git anxiety comes from. Here is what to use for each situation.
| Situation | Command |
|---|---|
| Discard changes to a file | git restore file.py |
| Unstage a file (keep changes) | git restore --staged file.py |
| Change the last commit message | git commit --amend |
| Undo last commit, keep changes | git reset --soft HEAD~1 |
| Undo last commit, discard changes | git reset --hard HEAD~1 |
| Undo a pushed commit safely | git revert <commit> |
| Recover "lost" commits | git reflog |
17. git restore
Replaces the file-level half of the old git checkout -- file. It throws away local changes, so be sure first.
18. git reset
Moves the current branch to another commit. --soft keeps your changes staged, --mixed (the default) keeps them unstaged, --hard deletes them. Only reset commits that have not been pushed.
19. git revert
Creates a new commit that undoes an earlier one. This is the right way to undo something on a shared branch because it does not rewrite history.
20. git reflog — your safety net
git reflog
git reset --hard HEAD@{3}
The reflog records every position HEAD has been at, including commits you "deleted" with a hard reset or a bad rebase. Entries are kept for 90 days by default, so almost nothing is truly lost.
Investigating history
21. git blame
git blame -L 40,60 app.py
Shows who last changed each line and in which commit — useful for finding the context behind a strange piece of code.
22. git show
git show a1b2c3d
git show main:config.yaml # a file as it exists on another branch
23. git bisect — find the commit that broke something
git bisect start
git bisect bad # current version is broken
git bisect good v1.4.0 # this version worked
# Git checks out a commit in the middle; test it, then:
git bisect good # or: git bisect bad
git bisect reset # when done
Bisect performs a binary search, so even 1,000 commits take only about 10 steps.
Cleaning up
24. git clean
git clean -n # dry run: list untracked files that would be deleted
git clean -fd # delete untracked files and folders
Always run -n first.
25. git cherry-pick
git cherry-pick a1b2c3d
Copies a single commit onto the current branch. Useful for applying a bug fix to a release branch.
Aliases worth adding
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate"
git config --global alias.undo "reset --soft HEAD~1"
FAQ
What is the difference between git fetch and git pull?
fetch downloads new commits but leaves your files alone. pull runs a fetch and then merges (or rebases) those commits into your current branch.
Should I use merge or rebase?
Use rebase to keep your own feature branch up to date before opening a pull request, and merge (or your platform's squash-merge) to bring finished work into main. Avoid rebasing shared branches.
How do I undo git add?
Run git restore --staged <file>. Your changes stay in the working directory.
Further reading
- Pro Git book, free and official
- Tool: .gitignore generator for new repositories
- Git reference documentation
- Related: Cron expressions explained
Keep reading
How to Undo Almost Anything in Git (Without Losing Work)
Undo the last commit, unstage files, discard changes, fix a bad merge or recover deleted commits. Every Git undo scenario with the exact command to use.
SSH Keys Explained: Generate, Add and Use Them Safely
Create an SSH key with ssh-keygen, add it to a server and to GitHub, use ssh-agent and a config file, and fix the most common SSH permission errors.
Regex Cheat Sheet: Syntax and Real Examples You Can Copy
A practical regular expression cheat sheet: characters, quantifiers, groups, lookarounds and flags, plus tested patterns for emails, dates, logs and more.