This document is a reference note for commonly used Git commands.
Common Commands
| Category | Command | Parameter Description | Effect |
|---|
| Repository | git clone <repository> | <repository>: Repository URL or path | Clone a remote Repository to the local machine |
| Status | git status | None | Check the current Repository status |
| Diff | git diff | None | View changes that have not been added to the Stage |
| Staging | git add <file> | <file>: Specify a file | Add the specified file to the Staging Area |
| Staging | git add . | .: Current directory | Add changes in the current directory to the Staging Area |
| Commit | git commit -m "<message>" | -m: Commit message; <message>: Message content | Create a new Commit |
| History | git log --oneline | --oneline: Display each Commit on a single line | View the Commit history in a concise format |
| Branch | git branch | None | View local Branches |
| Branch | git switch <branch> | <branch>: Branch name | Switch to the specified Branch |
| Branch | git switch -c <branch> | -c: create; <branch>: New Branch name | Create and switch to a new Branch |
| Merge | git merge <branch> | <branch>: Source Branch | Merge the specified Branch into the current Branch |
| Remote Repository | git remote -v | -v: verbose | View Remote URLs |
| Update | git pull | None | Fetch remote updates and integrate them into the current Branch |
| Push | git push | None | Push local Commits to the configured remote Branch |
| Push | git push -u <remote> <branch> | -u: Set Upstream; <remote>: Remote name; <branch>: Branch name | Push and establish an Upstream relationship for the Branch |
| Stashing | git stash | None | Temporarily save uncommitted changes |
| Restore Stash | git stash pop | None | Apply the most recent Stash and remove it |
| Restore | git restore <file> | <file>: Specify a file | Discard changes to the specified file in the working directory |
| Unstage | git restore --staged <file> | --staged: Operate on the Staging Area | Remove the file from the Stage while keeping the changes in the working directory |
Initialization and Configuration
| Command | Parameter Description | Effect |
|---|
git --version | None | Check the current Git version |
git help <command> | <command>: Git command | View the documentation for the specified command |
git <command> --help | --help: Display help | View the documentation for the specified Git command |
git config --list | --list: List configurations | View the current Git configuration |
git config --global user.name "<name>" | --global: Apply to the current user; user.name: Author name | Set the Git Commit author name |
git config --global user.email "<email>" | --global: Apply to the current user; user.email: Author Email | Set the Git Commit author Email |
git config <key> | <key>: Configuration key | View the specified Git configuration |
git config --global <key> <value> | --global: User-level configuration; <key>: Configuration key; <value>: Configuration value | Set a user-level Git configuration |
git config --local <key> <value> | --local: Repository-level configuration | Set the Git configuration for the current Repository |
Repository
| Command | Parameter Description | Effect |
|---|
git init | None | Initialize the current directory as a Git Repository |
git init <directory> | <directory>: Directory name | Create the specified directory and initialize it as a Git Repository |
git clone <repository> | <repository>: Repository URL or path | Clone a remote Repository to the local machine |
git clone <repository> <directory> | <directory>: Local directory name | Clone the Repository into the specified directory |
git clone --branch <branch> <repository> | --branch: Specify a Branch; <branch>: Branch name | Clone the specified Branch |
git clone --depth <number> <repository> | --depth: Commit depth; <number>: Number of historical levels to download | Create a Shallow Clone with a limited Commit history |
Status / Diff
| Command | Parameter Description | Effect |
|---|
git status | None | Check the current Repository status |
git status -s | -s: short | Display the status in a short format |
git status -b | -b: branch | Display the current Branch information |
git status -sb | -s + -b | Display file and Branch status in a short format |
git diff | None | View unstaged changes in the Working Directory |
git diff <file> | <file>: File | View unstaged changes to the specified file |
git diff --cached | --cached: Compare the Staging Area | View changes that have been staged but not yet committed |
git diff --staged | --staged: Same as --cached | View staged changes |
git diff HEAD | HEAD: Current Commit | Compare the current Working Directory with the current Commit |
git diff <commit> | <commit>: Commit | Compare the current Working Directory with the specified Commit |
git diff <commit1> <commit2> | <commit1>, <commit2>: Two Commits | Compare the differences between two Commits |
git diff <branch1> <branch2> | <branch1>, <branch2>: Two Branches | Compare the differences between two Branches |
git diff --stat | --stat: Statistics | Display statistics of modified files and lines |
git diff --name-only | --name-only | Display only the names of modified files |
Stage / Commit
| Command | Parameter Description | Effect |
|---|
git add <file> | <file>: File | Add the specified file to the Staging Area |
git add <directory> | <directory>: Directory | Add changes in the specified directory to the Staging Area |
git add . | .: Current directory | Add changes in the current directory to the Staging Area |
git add -A | -A: All changes | Add all additions, modifications, and deletions to the Staging Area |
git add -u | -u: tracked files | Stage modifications and deletions of tracked files only |
git add -p | -p: patch | Interactively select parts of changes to Stage |
git add -n <file> | -n: dry run; <file>: File | Preview which files would be added without actually adding them |
git add -f <file> | -f: force; <file>: File | Force-add a file that is normally ignored by .gitignore |
git commit | None | Open an editor to enter a Commit Message and create a Commit |
git commit -m "<message>" | -m: message; <message>: Message | Create a Commit with the specified message |
git commit -a -m "<message>" | -a: all | Commit changes to tracked files directly; does not include untracked files |
git commit --amend | --amend: Modify the previous Commit | Modify the most recent Commit |
git commit --amend --no-edit | --no-edit: Keep the original message | Modify the most recent Commit while keeping the original Commit Message |
Commit History
| Command | Parameter Description | Effect |
|---|
git log | None | View the Commit history |
git log --oneline | --oneline | Display each Commit on a single line |
git log --graph | --graph | Display the Branch structure using an ASCII graph |
git log --all | --all | Display the history of all Branches |
git log --oneline --graph | --oneline + --graph | View the Commit history and Branch structure in a concise graphical format |
git log -n <number> | -n: Number of entries; <number>: Quantity | Display only the specified number of Commits |
git log --author="<name>" | --author: Author filter; <name>: Author name | View Commits by the specified author |
git log -- <file> | --: Separator between Revision and path; <file>: File | View the Commit history of the specified file |
git show <commit> | <commit>: Commit ID | View detailed information and changes of the specified Commit |
git show <commit>:<file> | <commit>: Commit; <file>: File | View the contents of a file from the specified Commit |
git show --stat <commit> | --stat: Statistics; <commit>: Commit | View the change statistics of the specified Commit |
Branch
| Command | Parameter Description | Effect |
|---|
git branch | None | View local Branches |
git branch -a | -a: all | View local and Remote Branches |
git branch -r | -r: remote | View Remote Branches only |
git branch -v | -v: verbose | Display the latest Commit of each Branch |
git branch -vv | -vv: very verbose | Display the Upstream relationship and tracking information of each Branch |
git branch --show-current | --show-current | Display the current Branch |
git branch <branch> | <branch>: New Branch name | Create a Branch without switching to it |
git switch <branch> | <branch>: Branch name | Switch to the specified Branch |
git switch -c <branch> | -c: create; <branch>: New Branch | Create and switch to a new Branch |
git checkout <branch> | <branch>: Branch name | Switch Branch; commonly used in older or compatibility workflows |
git checkout -b <branch> | -b: branch; <branch>: New Branch | Create and switch to a Branch |
git branch -m <new-name> | -m: move; <new-name>: New name | Rename the current Branch |
git branch -M <new-name> | -M: force move; <new-name>: New name | Force-rename a Branch |
git branch -d <branch> | -d: delete; <branch>: Branch | Delete a merged Branch |
git branch -D <branch> | -D: force delete; <branch>: Branch | Force-delete a Branch |
git branch -u <remote>/<branch> | -u: upstream | Set the Upstream of the current Branch |
Merge and Conflicts
| Command | Parameter Description | Effect |
|---|
git merge <branch> | <branch>: Source Branch | Merge the specified Branch into the current Branch |
git merge --no-ff <branch> | --no-ff: no fast-forward | Create a Merge Commit even when Fast-forward is possible |
git merge --ff-only <branch> | --ff-only: Fast-forward only | Merge only when Fast-forward is possible |
git merge --abort | --abort: Abort Merge | Abort an unfinished Merge when a Merge Conflict occurs |
Restore / Reset / Revert
| Command | Parameter Description | Effect |
|---|
git restore <file> | <file>: File | Discard changes to the specified file in the Working Directory |
git restore . | .: Current directory | Discard unstaged changes in the current directory |
git restore --staged <file> | --staged: Operate on the Stage | Remove the file from the Staging Area while keeping the Working Directory changes |
git restore --source=<commit> <file> | --source: Source Commit; <file>: File | Restore a file from the specified Commit |
git reset <file> | <file>: File | Unstage the file; similar to git restore --staged |
git reset --soft <commit> | --soft: Keep the Index and Working Directory; <commit>: Target Commit | Move HEAD to the specified Commit while keeping the staged and Working Directory changes |
git reset --mixed <commit> | --mixed: Default mode | Move HEAD to the specified Commit, unstage changes, and keep the Working Directory changes |
git reset --hard <commit> | --hard: Reset HEAD, Index, and Working Directory | Reset to the specified Commit and discard staged and Working Directory changes |
git revert <commit> | <commit>: Commit | Create a new Commit that reverses the changes introduced by the specified Commit |
git revert --no-edit <commit> | --no-edit: Use the default message | Revert the specified Commit while keeping the default Commit Message |
git revert <commit1>..<commit2> | Commit range | Revert the Commits within the specified range |
Tip
reset, restore, and revert have different purposes: restore mainly handles file contents; reset can move the Branch/HEAD and change history; revert creates a new Commit to reverse the changes introduced by an existing Commit.
Stash
| Command | Parameter Description | Effect |
|---|
git stash | None | Stash the current uncommitted work |
git stash push -m "<message>" | -m: Message; <message>: Description | Stash changes with a description |
git stash list | None | View all Stashes |
git stash show | None | View a summary of the most recent Stash |
git stash show -p | -p: patch | View the detailed diff of a Stash |
git stash apply | None | Apply the most recent Stash while keeping it |
git stash apply <stash> | <stash>: Stash ID | Apply the specified Stash while keeping it |
git stash pop | None | Apply the most recent Stash and remove it |
git stash drop | None | Delete the most recent Stash |
git stash clear | None | Delete all Stashes |
git stash branch <branch> | <branch>: New Branch | Create and switch to a new Branch from the specified Stash |
Remote
| Command | Parameter Description | Effect |
|---|
git remote | None | View Remote names |
git remote -v | -v: verbose | View Remote URLs |
git remote show <remote> | <remote>: Remote name | View detailed information about the Remote |
git remote add <name> <url> | <name>: Remote name; <url>: Repository URL | Add a Remote |
git remote set-url <name> <url> | <name>: Remote name; <url>: New URL | Change the Remote URL |
git remote rename <old> <new> | <old>: Old name; <new>: New name | Rename a Remote |
git remote remove <name> | <name>: Remote name | Remove a Remote |
Fetch / Pull / Push
| Command | Parameter Description | Effect |
|---|
git fetch | None | Fetch Remote updates without directly modifying the current Branch |
git fetch <remote> | <remote>: Remote name | Fetch updates from the specified Remote |
git fetch --all | --all: All Remotes | Fetch updates from all Remotes |
git fetch --prune | --prune: Prune | Remove Remote-tracking Branches that no longer exist on the Remote while fetching updates |
git pull | None | Fetch Remote updates and integrate them into the current Branch |
git pull <remote> <branch> | <remote>: Remote; <branch>: Branch | Fetch and integrate updates from the specified Remote Branch |
git pull --rebase | --rebase: Use Rebase | Fetch and integrate local Commits using Rebase |
git pull --no-rebase | --no-rebase: Use Merge | Fetch and integrate local Commits using Merge |
git pull --ff-only | --ff-only: Fast-forward only | Integrate updates only when Fast-forward is possible |
git push | None | Push the current Branch to the configured Upstream |
git push <remote> <branch> | <remote>: Remote; <branch>: Branch | Push the local Branch to the specified Remote |
git push -u <remote> <branch> | -u: Set Upstream | Push and establish an Upstream relationship |
git push --force | --force: Force | Force-update the Remote Branch, which may overwrite remote history |
git push --force-with-lease | --force-with-lease | Force-update the Remote Branch under safer conditions |
git push <remote> --delete <branch> | --delete: Delete; <branch>: Remote Branch | Delete a Remote Branch |
git push --tags | --tags: All Tags | Push all local Tags to the Remote |
Rebase
| Command | Parameter Description | Effect |
|---|
git rebase <branch> | <branch>: Base Branch | Reapply the current Branch’s Commits on top of the specified Branch |
git rebase -i <commit> | -i: interactive; <commit>: Starting Commit | Interactively edit and organize Commit history |
git rebase --continue | --continue | Continue the Rebase after resolving a Conflict |
git rebase --abort | --abort | Abort the current Rebase and restore the original state |
git rebase --skip | --skip | Skip the Commit currently being applied |
Tag
| Command | Parameter Description | Effect |
|---|
git tag | None | List Tags |
git tag <tag> | <tag>: Tag name | Create a Lightweight Tag |
git tag -a <tag> -m "<message>" | -a: annotated; -m: message | Create an Annotated Tag |
git show <tag> | <tag>: Tag name | View detailed information about the Tag |
git tag -d <tag> | -d: delete | Delete a local Tag |
git push <remote> <tag> | <remote>: Remote; <tag>: Tag | Push the specified Tag |
git push <remote> --tags | --tags: All Tags | Push all Tags |
git push <remote> --delete <tag> | --delete: Delete; <tag>: Tag | Delete a Remote Tag |
Worktree
| Command | Parameter Description | Effect |
|---|
git worktree list | None | List the Worktrees of the current Repository |
git worktree add <path> <branch> | <path>: Worktree path; <branch>: Branch | Create an additional Worktree and Checkout the specified Branch |
git worktree remove <path> | <path>: Worktree path | Remove the specified Worktree |
git worktree prune | None | Clean up administrative information for Worktrees that no longer exist |
Submodule
| Command | Parameter Description | Effect |
|---|
git submodule add <url> | <url>: Another Repository URL | Add another Repository to the current project as a Submodule |
git submodule init | None | Initialize the configuration for existing Submodules |
git submodule update | None | Update Submodules to the Commits specified by the parent Repository |
git submodule update --init --recursive | --init: Initialize; --recursive: Process recursively | Initialize and update all Submodules, including nested Submodules |
git submodule update --remote | --remote: Track the Remote Branch | Update the Submodule to the latest version of its Remote Branch |