D

Git

Git commands for everyday workflow, branching, history rewriting and recovery.

Updated 2026-09-03

On this page

Setup

git config --global user.name 'Your Name'

Sets the name attached to commits, globally for every repo.

git config --global user.email 'you@example.com'

Sets the email attached to commits.

git init

Creates a new, empty Git repository in the current directory.

git clone git@host:org/repo.git

Clones a remote repository, including its full history, into a new local directory.

Status & Diff

git status

Shows staged, unstaged and untracked changes relative to the last commit.

git diff

Shows unstaged changes line by line.

git diff --staged

Shows changes that are staged but not yet committed.

git log --oneline --graph --all

Shows commit history as a compact, one-line-per-commit graph across every branch.

Branching

git branch

Lists local branches, marking the current one.

git checkout -b feature/my-change

Creates a new branch from the current commit and switches to it.

git switch feature/my-change

Switches to an existing branch. The modern, narrower alternative to checkout for this specific action.

git branch -d feature/my-change

Deletes a local branch, but only if it's already merged. Use -D to force-delete an unmerged branch.

Commits

git add .

Stages every changed file in the current directory and below.

git add -p

Interactively stages changes hunk by hunk, for splitting unrelated edits into separate commits.

git commit -m 'message'

Commits staged changes with a message.

git commit --amend
destructive

Rewrites the most recent commit instead of creating a new one. Never amend a commit that's already been pushed and pulled by someone else — it changes the commit hash.

Rebase

git rebase main

Replays the current branch's commits on top of main's latest commit, producing linear history instead of a merge commit.

git rebase -i HEAD~3

Opens an interactive rebase for the last 3 commits — squash, reorder, reword or drop them before they're shared.

git rebase --continue

Resumes a rebase after resolving a conflict in the current commit.

git rebase --abort

Cancels an in-progress rebase and returns the branch to its state before it started.

Never rebase shared history

Rebasing rewrites commit hashes. If anyone else has already pulled the branch, rebasing and force-pushing rewrites history out from under them — every collaborator's local branch now diverges. Rebase local, unpushed work freely; for shared branches, merge instead.

Stash

git stash

Shelves uncommitted changes and restores a clean working directory.

git stash list

Lists all stashed changesets.

git stash pop

Reapplies the most recent stash and removes it from the stash list.

git stash apply

Reapplies the most recent stash but keeps it in the stash list — useful for applying the same stash to multiple branches.

Remotes

git remote -v

Lists configured remotes and their URLs.

git fetch origin

Downloads new commits and branches from a remote without merging them into your working branch.

git pull

Fetches and merges (or rebases, depending on config) the current branch's upstream in one step.

git push -u origin feature/my-change

Pushes a branch and sets it to track the remote branch, so future push/pull need no arguments.

git push --force-with-lease
destructive

Force-pushes, but fails safely if the remote branch has commits you haven't seen — unlike plain --force, it won't silently overwrite someone else's work.

Undoing Things

git restore FILE_NAME

Discards unstaged changes to a file, reverting it to the last commit.

git restore --staged FILE_NAME

Unstages a file without discarding its changes.

git reset --soft HEAD~1

Undoes the last commit but keeps its changes staged.

git reset --hard HEAD~1
destructive

Undoes the last commit and discards its changes completely — unrecoverable through normal means once the reflog entry expires.

git revert COMMIT_HASH

Creates a new commit that undoes a previous one, without rewriting history. The safe way to undo a change that's already been pushed.

Recovery

git reflog

Shows a log of everywhere HEAD has pointed — including commits a hard reset or rebase seemingly threw away. Almost always the way back from a 'lost' commit.

git cherry-pick COMMIT_HASH

Applies a single commit from anywhere in history onto the current branch.

Nothing is really gone

Git rarely deletes commit objects immediately — a hard reset or an abandoned branch just stops pointing at them. git reflog finds the hash, and git checkout COMMIT_HASH or git cherry-pick gets it back, until garbage collection eventually runs.

Official documentation

Related