Snapshots
git status
Show changed and staged files
Add -sb for a compact one-line-per-file view.
git add <file>
Stage a file
git add .
Stage everything here
Stages new, modified and deleted files below the current
directory.
git commit -m "<msg>"
Commit staged changes
Without -m an editor opens for a longer message.
git diff
Show unstaged changes
Does not show untracked files.
git diff --staged
Show staged changes
git add -p
Stage changes hunk by hunk
y = stage, n = skip, s = split, q = quit.
git restore <file>
Discard changes in a file
Uncommitted work cannot be recovered afterwards.
git restore --staged <file>
Unstage a file
git commit --amend
Rewrite the last commit
Do not amend commits you have already pushed.
git rm <file>
Delete a file and stage it
git mv <old> <new>
Rename a file
Branches
git branch
List local branches
git switch <branch>
Switch branch
git switch -c <branch>
Create and switch
Starts at the current commit; add a start point to base it
elsewhere.
git merge <branch>
Merge branch into current
git merge --abort
Cancel a conflicted merge
Only works while the merge is still in progress.
git branch -d <branch>
Delete a merged branch
git rebase <branch>
Replay commits on top of branch
Fix conflicts, then git rebase --continue.
git rebase -i HEAD~<n>
Edit the last n commits
Change pick to squash, reword or drop in the editor.
git cherry-pick <commit>
Copy one commit here
Creates a new commit, so the hash differs.
git branch -vv
Branches with upstream status
git branch -D <branch>
Force-delete a branch
merge, rebase or cherry-pick?
| Command |
Result |
merge |
keeps both lines, adds a merge commit |
rebase |
replays your commits, linear history |
cherry-pick |
copies single commits |
Remotes
git pull
Fetch and integrate changes
Equals git fetch plus merge (or rebase, if configured).
git fetch
Download without merging
Updates origin/* only; your own branches are untouched.
git push -u origin <branch>
Push and track a new branch
-u lets a plain git push / pull work on this branch later.
git remote -v
List remotes
git remote add origin <url>
Add a remote
git push --force-with-lease
Safer force push
Refuses if someone else pushed in the meantime.
git push origin --delete <branch>
Delete a remote branch