man git

Git Cheatsheet

The git commands I use every day: set up a repo, commit, branch, merge, sync with remotes, undo mistakes and dig through history. Every command has a real example you can copy.

Showing 61 of 61 commands

Setup & Config

8 commands

Identify yourself, tune defaults and start or clone a repository.

git config --global user.name "<name>" beginner Set the author name recorded on every commit you make.

Example

bash
git config --global user.name "Jeferson Jeronimo"

Tip: Drop --global inside a repo to use a different identity just there, e.g. a work name on work repos.

git config --global user.email "<email>" beginner Set the author email; GitHub and GitLab use it to link commits to your account.

Example

bash
git config --global user.email "jeff@example.com"
git config --global init.defaultBranch main beginner Name the first branch of new repositories main instead of master.

Example

bash
git config --global init.defaultBranch main

Tip: Only affects repos created after the change; existing ones keep their branch name.

git config --list --show-origin beginner Show every effective setting and the file it comes from.

Example

bash
git config --list --show-origin | grep user
file:/home/jeff/.gitconfig   user.name=Jeferson Jeronimo
file:/home/jeff/.gitconfig   user.email=jeff@example.com

Tip: When a value surprises you, --show-origin tells you whether it is system, global or repo config.

git config --global alias.<name> "<command>" intermediate Create a shortcut for a command you type often.

Example

bash
git config --global alias.lg "log --oneline --graph --decorate --all"
git lg
git init beginner Turn the current folder into a new Git repository.

Example

bash
mkdir infra && cd infra
git init
Initialized empty Git repository in /home/jeff/infra/.git/
git clone <url> beginner Download a remote repository with its full history into a new folder.

Example

bash
git clone git@github.com:jeffxl/infra.git
Cloning into 'infra'...
remote: Enumerating objects: 1248, done.
Receiving objects: 100% (1248/1248), 2.31 MiB | 8.40 MiB/s, done.
Resolving deltas: 100% (612/612), done.
git clone --depth 1 <url> intermediate Shallow clone: fetch only the latest commit, much faster for big repos.

Example

bash
git clone --depth 1 --branch v1.34.1 https://github.com/kubernetes/kubernetes.git

Tip: Great for CI jobs. Run git fetch --unshallow later if you need the full history.

Staging & Committing

9 commands

Pick the changes that go into a commit and record them in history.

git status beginner Show the current branch plus staged, unstaged and untracked files.

Example

bash
git status -sb
## main...origin/main [ahead 1]
M  deploy.yaml
 M README.md
?? values-prod.yaml

Tip: -sb gives the short view: left column is staged, right column is unstaged, ?? is untracked.

git add <file> beginner Stage a file's current changes for the next commit.

Example

bash
git add deploy.yaml values-prod.yaml
git add .

Tip: git add . stages everything under the current folder, including new files. Check git status first.

git add -p intermediate Review each change hunk and choose what to stage, so one file can feed several commits.

Example

bash
git add -p deploy.yaml
@@ -12,7 +12,7 @@ spec:
-      replicas: 2
+      replicas: 3
(1/2) Stage this hunk [y,n,q,a,d,s,e,?]?
git commit -m "<message>" beginner Save the staged changes as a new commit.

Example

bash
git commit -m "feat: scale web deployment to 3 replicas"
[main 4f9c2ab] feat: scale web deployment to 3 replicas
 1 file changed, 1 insertion(+), 1 deletion(-)
git commit --amend intermediate Replace the last commit: fix its message or add forgotten staged files.

Example

bash
git add values-prod.yaml
git commit --amend --no-edit

Tip: Amending rewrites the commit hash. Only amend commits you have not pushed yet.

git diff beginner Show changes in the working tree that are not staged yet.

Example

bash
git diff --stat
 README.md   | 4 ++--
 deploy.yaml | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
git diff --staged beginner Show exactly what will go into the next commit.

Example

bash
git diff --staged

Tip: Make it a habit to run this before every commit.

git rm <file> intermediate Delete a file and stage the removal.

Example

bash
git rm old-config.yaml
git rm --cached .env

Tip: --cached stops tracking the file but keeps it on disk; add it to .gitignore right after.

git mv <src> <dest> beginner Move or rename a file and stage the change in one step.

Example

bash
git mv deploy.yaml k8s/deploy.yaml

Branching

7 commands

Create, switch, rename and clean up branches.

git branch beginner List local branches; the current one is marked with *.

Example

bash
git branch -vv
  feature/helm-chart  9a1d3e7 [origin/feature/helm-chart] add chart skeleton
* main                4f9c2ab [origin/main: ahead 1] scale web deployment

Tip: -vv also shows the upstream branch and how far ahead or behind you are.

git branch -a beginner List local and remote-tracking branches together.

Example

bash
git branch -a
git switch <branch> beginner Move to an existing branch.

Example

bash
git switch feature/helm-chart
git switch -
Switched to branch 'feature/helm-chart'

Tip: git switch - jumps back to the branch you were on before, like cd -.

git switch -c <branch> beginner Create a new branch from the current commit and switch to it.

Example

bash
git switch -c fix/ingress-tls origin/main
branch 'fix/ingress-tls' set up to track 'origin/main'.
Switched to a new branch 'fix/ingress-tls'
git branch -d <branch> beginner Delete a branch that has already been merged.

Example

bash
git branch -d feature/helm-chart
Deleted branch feature/helm-chart (was 9a1d3e7).

Tip: Git refuses if the branch is not merged. -D forces the delete and the commits become unreachable.

git branch -m <new-name> intermediate Rename the current branch.

Example

bash
git branch -m fix/ingress-tls-cert
git push origin --delete <branch> intermediate Delete a branch on the remote.

Example

bash
git push origin --delete feature/helm-chart
To github.com:jeffxl/infra.git
 - [deleted]         feature/helm-chart

Merging & Rebasing

5 commands

Combine work from different branches and keep history readable.

git merge <branch> intermediate Merge another branch into the current one.

Example

bash
git switch main
git merge --no-ff feature/helm-chart
Merge made by the 'ort' strategy.
 chart/Chart.yaml  |  6 ++++++
 chart/values.yaml | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

Tip: --no-ff always creates a merge commit, so the feature stays visible as one unit in the history.

git merge --abort intermediate Cancel a merge that hit conflicts and go back to the state before it.

Example

bash
git merge --abort
git rebase <branch> advanced Replay your commits on top of another branch for a linear history.

Example

bash
git fetch origin
git rebase origin/main
Successfully rebased and updated refs/heads/fix/ingress-tls.

Tip: Never rebase commits that others already pulled. On conflict: fix the files, git add, then git rebase --continue.

git rebase -i HEAD~<n> advanced Interactively reorder, squash, reword or drop the last n commits.

Example

bash
git rebase -i HEAD~3
pick 1a2b3c4 add ingress
pick 5d6e7f8 fix typo
pick 9a0b1c2 add tls secret
# change "pick" to "squash" or "fixup" to fold a commit into the one above

Tip: Clean up a messy branch before opening a pull request.

git cherry-pick <commit> advanced Copy one specific commit onto the current branch.

Example

bash
git switch release/1.4
git cherry-pick 4f9c2ab
[release/1.4 b7e1d90] fix: scale web deployment to 3 replicas
 1 file changed, 1 insertion(+), 1 deletion(-)

Tip: Handy for backporting a hotfix to a release branch without merging everything else.

Remotes & Syncing

7 commands

Connect to remotes and move commits between your machine and the server.

git remote -v beginner List configured remotes with their fetch and push URLs.

Example

bash
git remote -v
origin  git@github.com:jeffxl/infra.git (fetch)
origin  git@github.com:jeffxl/infra.git (push)
git remote add <name> <url> beginner Register a new remote, e.g. the upstream repo of a fork.

Example

bash
git remote add upstream https://github.com/argoproj/argo-cd.git
git remote set-url <name> <url> intermediate Change a remote's URL, e.g. switch from HTTPS to SSH.

Example

bash
git remote set-url origin git@github.com:jeffxl/infra.git
git fetch <remote> intermediate Download new commits and branches without touching your working tree.

Example

bash
git fetch --all --prune
Fetching origin
From github.com:jeffxl/infra
 - [deleted]         (none)     -> origin/feature/helm-chart
   4f9c2ab..c81d0e5  main       -> origin/main

Tip: --prune removes local references to branches that were deleted on the remote.

git pull beginner Fetch and integrate remote changes into the current branch.

Example

bash
git pull --rebase
Successfully rebased and updated refs/heads/main.

Tip: --rebase avoids noisy merge commits. Make it the default with git config --global pull.rebase true.

git push beginner Upload your local commits to the tracked remote branch.

Example

bash
git push -u origin fix/ingress-tls
To github.com:jeffxl/infra.git
 * [new branch]      fix/ingress-tls -> fix/ingress-tls
branch 'fix/ingress-tls' set up to track 'origin/fix/ingress-tls'.

Tip: -u sets the upstream the first time, so later a plain git push is enough.

git push --force-with-lease advanced Overwrite the remote branch after a rebase, but only if nobody pushed in the meantime.

Example

bash
git push --force-with-lease

Tip: Always prefer this over --force: it refuses instead of silently deleting a teammate's commits.

Undoing Changes

7 commands

Discard, unstage, revert and recover work safely.

git restore <file> beginner Throw away uncommitted changes in a file and go back to the last commit.

Example

bash
git restore deploy.yaml

Tip: This cannot be undone: changes that were never committed or stashed are gone.

git restore --staged <file> beginner Unstage a file but keep its changes in the working tree.

Example

bash
git restore --staged values-prod.yaml

Tip: Same effect as the older git reset <file>.

git reset --soft HEAD~1 intermediate Undo the last commit and keep its changes staged.

Example

bash
git reset --soft HEAD~1

Tip: Perfect when you committed too early or on the wrong branch: undo, switch, commit again.

git reset --hard <commit> advanced Move the branch to a commit and discard all changes after it, staged or not.

Example

bash
git reset --hard origin/main
HEAD is now at c81d0e5 chore: bump chart version

Tip: Destructive. If you regret it, the old commits can usually be found with git reflog.

git revert <commit> intermediate Create a new commit that reverses an earlier one, without rewriting history.

Example

bash
git revert 4f9c2ab --no-edit
[main d3a9f11] Revert "feat: scale web deployment to 3 replicas"
 1 file changed, 1 insertion(+), 1 deletion(-)

Tip: The safe way to undo something that is already pushed to a shared branch.

git clean -fd intermediate Delete untracked files and folders from the working tree.

Example

bash
git clean -nd
git clean -fd
Would remove build/
Would remove values-test.yaml

Tip: Run with -n first for a dry run. Add -x to also delete ignored files.

git reflog advanced Show every position HEAD has been in, the safety net for lost commits.

Example

bash
git reflog -5
c81d0e5 HEAD@{0}: reset: moving to origin/main
4f9c2ab HEAD@{1}: commit: feat: scale web deployment to 3 replicas
9a1d3e7 HEAD@{2}: checkout: moving from feature/helm-chart to main

Tip: Recover with git reset --hard HEAD@{1} or git branch rescue 4f9c2ab.

Stashing

6 commands

Park unfinished work so you can switch context with a clean tree.

git stash intermediate Save uncommitted changes and reset the working tree to the last commit.

Example

bash
git stash push -u -m "wip: ingress tls"
Saved working directory and index state On main: wip: ingress tls

Tip: -u also stashes untracked files; -m gives the stash a name you will recognize later.

git stash list beginner List saved stashes, newest first.

Example

bash
git stash list
stash@{0}: On main: wip: ingress tls
stash@{1}: WIP on fix/probe: 9a1d3e7 add readiness probe
git stash pop intermediate Apply the latest stash and remove it from the list.

Example

bash
git stash pop stash@{1}

Tip: If the pop hits a conflict, the stash is kept, so nothing is lost.

git stash apply intermediate Apply a stash but keep it in the list, e.g. to reuse it on another branch.

Example

bash
git stash apply
git stash show -p intermediate Show the full diff stored in a stash before applying it.

Example

bash
git stash show -p stash@{0}
git stash drop intermediate Delete a specific stash.

Example

bash
git stash drop stash@{1}
Dropped stash@{1} (2c7e4b1f9d0a8e3c6b5a4f1e2d3c4b5a6f7e8d9c)

History & Inspection

7 commands

Read the log, find who changed what and track down regressions.

git log beginner Show the commit history of the current branch.

Example

bash
git log --oneline --graph --decorate -5
* c81d0e5 (HEAD -> main, origin/main) chore: bump chart version
*   7b2e4a9 Merge branch 'feature/helm-chart'
|\
| * 9a1d3e7 add chart skeleton
|/
* 4f9c2ab feat: scale web deployment to 3 replicas
git log -p <file> intermediate Show every change ever made to a file, commit by commit.

Example

bash
git log -p --follow -- k8s/deploy.yaml

Tip: --follow keeps tracking the file across renames.

git log -S "<text>" advanced Find the commits that added or removed a piece of text.

Example

bash
git log -S "replicas: 3" --oneline
4f9c2ab feat: scale web deployment to 3 replicas
git show <commit> beginner Show the author, message and diff of one commit.

Example

bash
git show 4f9c2ab --stat
commit 4f9c2ab
Author: Jeferson Jeronimo <jeff@example.com>
Date:   Tue Oct 28 14:02:11 2026 -0300

    feat: scale web deployment to 3 replicas

 deploy.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git blame <file> intermediate Show who last changed each line, and in which commit.

Example

bash
git blame -L 10,14 deploy.yaml
4f9c2ab (Jeferson 2026-10-28 12) spec:
4f9c2ab (Jeferson 2026-10-28 13)   replicas: 3
9a1d3e7 (Jeferson 2026-10-21 14)   selector:
git diff <a>..<b> intermediate Compare two commits, branches or tags.

Example

bash
git diff main..fix/ingress-tls --stat
git diff v1.3.0..v1.4.0 -- chart/
 k8s/ingress.yaml | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
git bisect start advanced Binary-search the history to find the commit that introduced a bug.

Example

bash
git bisect start
git bisect bad
git bisect good v1.3.0
# test, then mark each step with git bisect good or git bisect bad
git bisect reset
Bisecting: 6 revisions left to test after this (roughly 3 steps)
[9a1d3e7] add chart skeleton

Tip: Automate it with git bisect run ./test.sh: the script's exit code marks each commit good or bad.

Tags & Releases

5 commands

Mark release points and share them with the remote.

git tag beginner List tags.

Example

bash
git tag -l "v1.*" --sort=-v:refname
v1.4.0
v1.3.1
v1.3.0

Tip: --sort=-v:refname sorts by version, newest first, so v1.10 comes after v1.9.

git tag -a <name> -m "<msg>" intermediate Create an annotated tag with author, date and message, the right kind for releases.

Example

bash
git tag -a v1.4.0 -m "Release 1.4.0: ingress TLS and Helm chart"
git push <remote> <tag> intermediate Push a tag to the remote; git push does not send tags by default.

Example

bash
git push origin v1.4.0
To github.com:jeffxl/infra.git
 * [new tag]         v1.4.0 -> v1.4.0

Tip: git push --follow-tags pushes your commits plus any annotated tags that point at them.

git tag -d <name> intermediate Delete a local tag.

Example

bash
git tag -d v1.4.0
git push origin --delete v1.4.0
Deleted tag 'v1.4.0' (was 3e8f2d1)

Tip: The second line removes the tag from the remote too.

git describe --tags intermediate Describe the current commit relative to the nearest tag, handy for build versions.

Example

bash
git describe --tags
v1.4.0-3-gc81d0e5

Tip: Reads as 3 commits after v1.4.0, at commit c81d0e5.

↑ ↓ navigate Enter open Esc close