Reference
A quick-reference list of common Git commands. All commands are prefixed with git.
Setup
Command |
Description |
|---|---|
|
Set your name (used in commits) |
|
Set your email (used in commits) |
|
Initialize a new repo in the current folder |
|
Clone a remote repository locally |
Inspecting state
Command |
Description |
|---|---|
|
Show changed, staged, and untracked files |
|
Show commit history |
|
Compact commit history, one line per commit |
|
Show unstaged changes |
|
Show staged changes (what will be committed) |
|
Show history of HEAD movements — useful for recovering lost commits |
Staging and committing
Command |
Description |
|---|---|
|
Stage a specific file |
|
Stage all changed files |
|
Commit staged changes with a message |
|
Modify the last commit (only before pushing) |
Branching
Command |
Description |
|---|---|
|
List all local branches |
|
Create a new branch |
|
Delete a branch (safe — refuses if unmerged) |
|
Delete a branch (force) |
|
Switch to a branch |
|
Create a new branch and switch to it |
Pushing and pulling
Command |
Description |
|---|---|
|
Push branch to remote and set upstream |
|
Push to the configured upstream branch |
|
Fetch and merge changes from the remote |
|
Download changes from remote without merging |
Warning
Avoid git push -f (force push). It overwrites remote history and can destroy
your collaborators’ work. Never force push to main. If you need to overwrite
a remote feature branch after a local rebase, use git push --force-with-lease
instead — it will abort if someone else has pushed since you last fetched.
Rebasing
Command |
Description |
|---|---|
|
Replay your commits on top of another branch |
|
Continue after resolving a conflict |
|
Abort the rebase and return to the original state |
|
Interactively edit the last |
In interactive rebase, each commit can be given one of the following actions:
pick— keep the commit as-isreword— keep the commit but edit the messagesquash— fold into the commit above (combines messages)fixup— fold into the commit above (discards this message)drop— remove the commit entirely
Undoing things
Command |
Description |
|---|---|
|
Create a new commit that undoes a previous one (safe, preserves history) |
|
Move HEAD back, keep changes staged |
|
Move HEAD back, keep changes unstaged |
|
Move HEAD back and discard all changes (destructive) |
|
Discard unstaged changes to a file |
Note
Prefer git revert over git reset when undoing changes that have already been
pushed to a shared remote. revert is safe because it adds a new commit rather than
rewriting history.