DevPlainly
Developer Productivity

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
  1. Setting up
  2. 1. git config — tell Git who you are
  3. 2. git init and git clone
  4. The everyday loop
  5. 3. git status
  6. 4. git add
  7. 5. git commit
  8. 6. git diff
  9. 7. git log
  10. Branches
  11. 8. git switch — move between branches
  12. 9. git branch
  13. 10. git merge
  14. 11. git rebase
  15. Working with remotes
  16. 12. git remote
  17. 13. git fetch
  18. 14. git pull
  19. 15. git push
  20. Saving work in progress
  21. 16. git stash
  22. Undoing things
  23. 17. git restore
  24. 18. git reset
  25. 19. git revert
  26. 20. git reflog — your safety net
  27. Investigating history
  28. 21. git blame
  29. 22. git show
  30. 23. git bisect — find the commit that broke something
  31. Cleaning up
  32. 24. git clean
  33. 25. git cherry-pick
  34. Aliases worth adding
  35. FAQ
  36. What is the difference between git fetch and git pull?
  37. Should I use merge or rebase?
  38. How do I undo git add?
  39. 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.

SituationCommand
Discard changes to a filegit restore file.py
Unstage a file (keep changes)git restore --staged file.py
Change the last commit messagegit commit --amend
Undo last commit, keep changesgit reset --soft HEAD~1
Undo last commit, discard changesgit reset --hard HEAD~1
Undo a pushed commit safelygit revert <commit>
Recover "lost" commitsgit 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

Keep reading