Git notes

Git commands for reviewing changes, working with branches and recovering work.

Inspect the repository

Status and differences

status shows the branch and changes. diff compares the working tree with the staging area. Use --staged to see what will go into the next commit.

git status -sb
git diff
git diff --staged
git diff HEAD -- README.md
git diff --check

History and file changes

Use show to inspect a commit. log --follow follows a file’s history across renames.

git log --oneline --graph --decorate --all
git log -p -2
git show --stat HEAD
git show --name-only HEAD
git log --follow -- README.md
git blame -- README.md

Stage and commit changes

Stage by file or by hunk

add -p lets you select individual hunks. restore --staged removes a file from the next commit and keeps your local changes.

git add -- README.md
git add -p
git diff --staged
git commit -m "Update documentation"
git restore --staged -- README.md

Amend the latest local commit

Use amend on a commit you have yet to share. It includes staged changes and creates a new commit ID.

git commit --amend
git commit --amend --no-edit

Ignored and tracked files

check-ignore identifies the rule that ignores a path. rm --cached keeps the file on disk and stages its removal from the repository. Add the corresponding rule to .gitignore to keep it from being added again.

git check-ignore -v -- dist/index.html
git rm --cached -- local-settings.json

Branches and integration

Create and switch branches

switch -c creates a branch from the current commit. switch - returns to the previous branch. branch -d protects branches with unmerged changes.

git branch -vv
git switch -c feature/docs
git switch main
git switch -
git branch --merged main
git branch -d feature/docs

Integrate changes and resolve conflicts

From your working branch, you can merge origin/main. If conflicts occur, edit the files, stage them and continue the merge. Use --abort if you decide to cancel it.

git fetch origin
git merge origin/main
git status
git add -- README.md
git merge --continue
git merge --abort

Apply a specific commit

cherry-pick applies a commit’s changes to the current branch. Replace abc1234 with its ID.

git show abc1234
git cherry-pick abc1234

Remote repositories

Clone and sync

Replace the example URL with your repository’s URL. fetch --prune updates remote-tracking refs and removes stale ones. pull --ff-only integrates only when the branch can advance without a merge commit.

git clone https://example.com/team/project.git
git remote -v
git fetch --prune origin
git pull --ff-only

Publish a branch

-u sets the remote branch as the upstream for your local branch. After that, you can use git push from that branch.

git push -u origin feature/docs

Stash and recover

Stash work in progress

push -u also stashes untracked files. apply restores changes and keeps the entry. Drop it once you have checked the result.

git stash push -u -m "Work in progress"
git stash list
git stash show -p 'stash@{0}'
git stash apply 'stash@{0}'
git stash drop 'stash@{0}'

Undo a shared commit

revert creates a new commit that reverses an earlier change. Inspect the commit you intend to revert first.

git show abc1234
git revert abc1234

Recover a local commit

reflog records recent movements of local refs. When you find the commit, create a branch at its ID to keep it.

git reflog --date=relative
git branch rescue abc1234

Search and diagnose

Search files and commits

git grep searches tracked files. log -S finds commits that change the number of occurrences of a string.

git grep -n 'example'
git log -S 'example' --oneline
git log origin/main..HEAD --oneline

Find the commit that introduced a bug

Mark HEAD as bad and abc1234 as a commit known to work. Test each revision and mark it with git bisect good or git bisect bad as appropriate. Reset to the original branch when finished.

git bisect start
git bisect bad HEAD
git bisect good abc1234

Run this command once the search is complete.

git bisect reset

Rebase, tags and worktrees

Update a local branch with rebase

On an unshared branch, rebase reapplies your commits on top of origin/main. If there is a conflict, resolve it and stage the files before continuing. Use --abort to cancel.

git fetch origin
git rebase origin/main
git rebase --continue
git rebase --abort

Tag a release

An annotated tag stores a message and points to a commit. Push the tag by name to publish only that release tag.

git tag -a v1.0.0 -m "Release 1.0.0"
git show v1.0.0
git push origin v1.0.0

Work on two branches at once

worktree add checks out another branch in a neighbouring directory. This lets you switch tasks while keeping your current work open.

git worktree add -b feature/review ../project-review main
git worktree list
git worktree remove ../project-review