Applies to: Current Git command-line workflows Last reviewed: 2026-07-14
A practical reference for repository setup, branches, commits, remotes, inspection, undo operations, rebasing, and recovery.
Warning
Commands such as reset --hard, clean, forced pushes, and branch deletion can permanently remove uncommitted or unreferenced work. Inspect state and create a backup branch before destructive recovery.
git status --short --branch
git branch --show-current
git remote -v
git log --oneline --decorate --graph -20git config --global user.name '<name>'
git config --global user.email '<email>'
git config --global init.defaultBranch main
git config --list --show-originUse repository-local configuration when work and personal identities differ:
git config user.name '<name>'
git config user.email '<email>'git init
git clone <repository-url>
git clone --branch <branch> <repository-url>git status
git diff
git diff --staged
git diff <base>...<head>
git log --oneline --decorate --graph --all
git show <commit>
git blame <file>git add <file>
git add --patch
git restore --staged <file>
git commit -m '<message>'
git commit --amendUse git add --patch to review each hunk and avoid accidentally committing unrelated changes.
git branch
git branch --all
git switch -c <new-branch>
git switch <branch>
git switch -
git branch -d <branch>Force-delete a local branch only after confirming its commits are preserved elsewhere:
git branch -D <branch>git remote -v
git remote add <name> <url>
git remote set-url <name> <url>
git fetch --all --prune
git remote show origingit fetch origin
git merge origin/<branch>
git pull --ff-only
git push -u origin <branch>
git pushPrefer git pull --ff-only when you do not want Git to create an implicit merge commit or rebase.
git switch <target-branch>
git fetch origin
git merge --ff-only origin/<target-branch>
git merge <source-branch>Resolve conflicts, stage the resolved files, and complete the merge:
git status
git add <resolved-file>
git commitAbort an in-progress merge:
git merge --abortgit fetch origin
git rebase origin/main
git rebase --continue
git rebase --skip
git rebase --abortInteractive cleanup:
git rebase -i <base-commit>Caution
Rebase rewrites commit IDs. Avoid rebasing shared branches unless the collaboration workflow explicitly expects it.
git stash push -m '<description>'
git stash push --include-untracked -m '<description>'
git stash list
git stash show --patch stash@{0}
git stash apply stash@{0}
git stash pop
git stash drop stash@{0}Use apply instead of pop when you want to keep the stash until the result is verified.
Restore one file from the index:
git restore <file>Restore both staged and working-tree content from HEAD:
git restore --source=HEAD --staged --worktree <file>Remove untracked files only after a dry run:
git clean -nd
git clean -nfd
git clean -fd[!DANGER]
git clean -fddeletes untracked files and directories. Ignored files require additional flags and can include build artifacts, local configuration, or secrets.
Create a new commit that reverses an earlier commit:
git revert <commit>
git revert <oldest-commit>^..<newest-commit>Revert is generally safer for shared branches because it preserves history.
Inspect first:
git status
git log --oneline --decorate -10
git branch backup/<description>Reset modes:
git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>| Mode | Branch pointer | Index | Working tree |
|---|---|---|---|
--soft |
Moves | Preserved | Preserved |
--mixed |
Moves | Reset | Preserved |
--hard |
Moves | Reset | Reset |
[!DANGER]
git reset --harddiscards tracked working-tree changes. Create a backup branch and verify the target commit before running it.
git cherry-pick <commit>
git cherry-pick --continue
git cherry-pick --abortApply a commit without immediately committing it:
git cherry-pick --no-commit <commit>git tag
git tag -a <tag> -m '<message>'
git show <tag>
git push origin <tag>
git push origin --tagsgit bisect start
git bisect bad
git bisect good <known-good-commit>Test each selected commit and mark it:
git bisect good
git bisect bad
git bisect resetAutomate with a test command:
git bisect run <test-command>git reflog
git show <reflog-commit>
git branch recovery/<description> <reflog-commit>The reflog is local and expires over time. Create a branch as soon as the desired commit is found.
git submodule status
git submodule update --init --recursive
git submodule sync --recursive
git clone --recurse-submodules <repository-url>git worktree list
git worktree add ../<directory> <branch>
git worktree add -b <new-branch> ../<directory> <base>
git worktree remove ../<directory>
git worktree pruneWorktrees are useful when multiple branches must be checked out simultaneously without repeated stashing.
git fetch origin
git push --force-with-lease origin <branch>Caution
--force-with-lease is safer than --force, but it still rewrites remote history. Confirm that the branch is intended to be rewritten and that no collaborator work will be lost.
git config --global alias.st 'status --short --branch'
git config --global alias.lg 'log --oneline --decorate --graph --all'
git config --global alias.unstage 'restore --staged --'- Run
git status --short --branch. - Confirm the current branch and remote.
- Fetch before comparing local and remote history.
- Inspect staged and unstaged diffs separately.
- Use
git reflogbefore assuming a commit is lost. - Create a backup branch before reset, rebase, or force-push recovery.
- Prefer revert for changes already shared with others.