Command Notes Series

Contents

[Note] Git Command Notes

This document is a reference note for commonly used Git commands.

Common Commands

CategoryCommandParameter DescriptionEffect
Repositorygit clone <repository><repository>: Repository URL or pathClone a remote Repository to the local machine
Statusgit statusNoneCheck the current Repository status
Diffgit diffNoneView changes that have not been added to the Stage
Staginggit add <file><file>: Specify a fileAdd the specified file to the Staging Area
Staginggit add ..: Current directoryAdd changes in the current directory to the Staging Area
Commitgit commit -m "<message>"-m: Commit message; <message>: Message contentCreate a new Commit
Historygit log --oneline--oneline: Display each Commit on a single lineView the Commit history in a concise format
Branchgit branchNoneView local Branches
Branchgit switch <branch><branch>: Branch nameSwitch to the specified Branch
Branchgit switch -c <branch>-c: create; <branch>: New Branch nameCreate and switch to a new Branch
Mergegit merge <branch><branch>: Source BranchMerge the specified Branch into the current Branch
Remote Repositorygit remote -v-v: verboseView Remote URLs
Updategit pullNoneFetch remote updates and integrate them into the current Branch
Pushgit pushNonePush local Commits to the configured remote Branch
Pushgit push -u <remote> <branch>-u: Set Upstream; <remote>: Remote name; <branch>: Branch namePush and establish an Upstream relationship for the Branch
Stashinggit stashNoneTemporarily save uncommitted changes
Restore Stashgit stash popNoneApply the most recent Stash and remove it
Restoregit restore <file><file>: Specify a fileDiscard changes to the specified file in the working directory
Unstagegit restore --staged <file>--staged: Operate on the Staging AreaRemove the file from the Stage while keeping the changes in the working directory

Initialization and Configuration

CommandParameter DescriptionEffect
git --versionNoneCheck the current Git version
git help <command><command>: Git commandView the documentation for the specified command
git <command> --help--help: Display helpView the documentation for the specified Git command
git config --list--list: List configurationsView the current Git configuration
git config --global user.name "<name>"--global: Apply to the current user; user.name: Author nameSet the Git Commit author name
git config --global user.email "<email>"--global: Apply to the current user; user.email: Author EmailSet the Git Commit author Email
git config <key><key>: Configuration keyView the specified Git configuration
git config --global <key> <value>--global: User-level configuration; <key>: Configuration key; <value>: Configuration valueSet a user-level Git configuration
git config --local <key> <value>--local: Repository-level configurationSet the Git configuration for the current Repository

Repository

CommandParameter DescriptionEffect
git initNoneInitialize the current directory as a Git Repository
git init <directory><directory>: Directory nameCreate the specified directory and initialize it as a Git Repository
git clone <repository><repository>: Repository URL or pathClone a remote Repository to the local machine
git clone <repository> <directory><directory>: Local directory nameClone the Repository into the specified directory
git clone --branch <branch> <repository>--branch: Specify a Branch; <branch>: Branch nameClone the specified Branch
git clone --depth <number> <repository>--depth: Commit depth; <number>: Number of historical levels to downloadCreate a Shallow Clone with a limited Commit history

Status / Diff

CommandParameter DescriptionEffect
git statusNoneCheck the current Repository status
git status -s-s: shortDisplay the status in a short format
git status -b-b: branchDisplay the current Branch information
git status -sb-s + -bDisplay file and Branch status in a short format
git diffNoneView unstaged changes in the Working Directory
git diff <file><file>: FileView unstaged changes to the specified file
git diff --cached--cached: Compare the Staging AreaView changes that have been staged but not yet committed
git diff --staged--staged: Same as --cachedView staged changes
git diff HEADHEAD: Current CommitCompare the current Working Directory with the current Commit
git diff <commit><commit>: CommitCompare the current Working Directory with the specified Commit
git diff <commit1> <commit2><commit1>, <commit2>: Two CommitsCompare the differences between two Commits
git diff <branch1> <branch2><branch1>, <branch2>: Two BranchesCompare the differences between two Branches
git diff --stat--stat: StatisticsDisplay statistics of modified files and lines
git diff --name-only--name-onlyDisplay only the names of modified files

Stage / Commit

CommandParameter DescriptionEffect
git add <file><file>: FileAdd the specified file to the Staging Area
git add <directory><directory>: DirectoryAdd changes in the specified directory to the Staging Area
git add ..: Current directoryAdd changes in the current directory to the Staging Area
git add -A-A: All changesAdd all additions, modifications, and deletions to the Staging Area
git add -u-u: tracked filesStage modifications and deletions of tracked files only
git add -p-p: patchInteractively select parts of changes to Stage
git add -n <file>-n: dry run; <file>: FilePreview which files would be added without actually adding them
git add -f <file>-f: force; <file>: FileForce-add a file that is normally ignored by .gitignore
git commitNoneOpen an editor to enter a Commit Message and create a Commit
git commit -m "<message>"-m: message; <message>: MessageCreate a Commit with the specified message
git commit -a -m "<message>"-a: allCommit changes to tracked files directly; does not include untracked files
git commit --amend--amend: Modify the previous CommitModify the most recent Commit
git commit --amend --no-edit--no-edit: Keep the original messageModify the most recent Commit while keeping the original Commit Message

Commit History

CommandParameter DescriptionEffect
git logNoneView the Commit history
git log --oneline--onelineDisplay each Commit on a single line
git log --graph--graphDisplay the Branch structure using an ASCII graph
git log --all--allDisplay the history of all Branches
git log --oneline --graph--oneline + --graphView the Commit history and Branch structure in a concise graphical format
git log -n <number>-n: Number of entries; <number>: QuantityDisplay only the specified number of Commits
git log --author="<name>"--author: Author filter; <name>: Author nameView Commits by the specified author
git log -- <file>--: Separator between Revision and path; <file>: FileView the Commit history of the specified file
git show <commit><commit>: Commit IDView detailed information and changes of the specified Commit
git show <commit>:<file><commit>: Commit; <file>: FileView the contents of a file from the specified Commit
git show --stat <commit>--stat: Statistics; <commit>: CommitView the change statistics of the specified Commit

Branch

CommandParameter DescriptionEffect
git branchNoneView local Branches
git branch -a-a: allView local and Remote Branches
git branch -r-r: remoteView Remote Branches only
git branch -v-v: verboseDisplay the latest Commit of each Branch
git branch -vv-vv: very verboseDisplay the Upstream relationship and tracking information of each Branch
git branch --show-current--show-currentDisplay the current Branch
git branch <branch><branch>: New Branch nameCreate a Branch without switching to it
git switch <branch><branch>: Branch nameSwitch to the specified Branch
git switch -c <branch>-c: create; <branch>: New BranchCreate and switch to a new Branch
git checkout <branch><branch>: Branch nameSwitch Branch; commonly used in older or compatibility workflows
git checkout -b <branch>-b: branch; <branch>: New BranchCreate and switch to a Branch
git branch -m <new-name>-m: move; <new-name>: New nameRename the current Branch
git branch -M <new-name>-M: force move; <new-name>: New nameForce-rename a Branch
git branch -d <branch>-d: delete; <branch>: BranchDelete a merged Branch
git branch -D <branch>-D: force delete; <branch>: BranchForce-delete a Branch
git branch -u <remote>/<branch>-u: upstreamSet the Upstream of the current Branch

Merge and Conflicts

CommandParameter DescriptionEffect
git merge <branch><branch>: Source BranchMerge the specified Branch into the current Branch
git merge --no-ff <branch>--no-ff: no fast-forwardCreate a Merge Commit even when Fast-forward is possible
git merge --ff-only <branch>--ff-only: Fast-forward onlyMerge only when Fast-forward is possible
git merge --abort--abort: Abort MergeAbort an unfinished Merge when a Merge Conflict occurs

Restore / Reset / Revert

CommandParameter DescriptionEffect
git restore <file><file>: FileDiscard changes to the specified file in the Working Directory
git restore ..: Current directoryDiscard unstaged changes in the current directory
git restore --staged <file>--staged: Operate on the StageRemove the file from the Staging Area while keeping the Working Directory changes
git restore --source=<commit> <file>--source: Source Commit; <file>: FileRestore a file from the specified Commit
git reset <file><file>: FileUnstage the file; similar to git restore --staged
git reset --soft <commit>--soft: Keep the Index and Working Directory; <commit>: Target CommitMove HEAD to the specified Commit while keeping the staged and Working Directory changes
git reset --mixed <commit>--mixed: Default modeMove HEAD to the specified Commit, unstage changes, and keep the Working Directory changes
git reset --hard <commit>--hard: Reset HEAD, Index, and Working DirectoryReset to the specified Commit and discard staged and Working Directory changes
git revert <commit><commit>: CommitCreate a new Commit that reverses the changes introduced by the specified Commit
git revert --no-edit <commit>--no-edit: Use the default messageRevert the specified Commit while keeping the default Commit Message
git revert <commit1>..<commit2>Commit rangeRevert 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

CommandParameter DescriptionEffect
git stashNoneStash the current uncommitted work
git stash push -m "<message>"-m: Message; <message>: DescriptionStash changes with a description
git stash listNoneView all Stashes
git stash showNoneView a summary of the most recent Stash
git stash show -p-p: patchView the detailed diff of a Stash
git stash applyNoneApply the most recent Stash while keeping it
git stash apply <stash><stash>: Stash IDApply the specified Stash while keeping it
git stash popNoneApply the most recent Stash and remove it
git stash dropNoneDelete the most recent Stash
git stash clearNoneDelete all Stashes
git stash branch <branch><branch>: New BranchCreate and switch to a new Branch from the specified Stash

Remote

CommandParameter DescriptionEffect
git remoteNoneView Remote names
git remote -v-v: verboseView Remote URLs
git remote show <remote><remote>: Remote nameView detailed information about the Remote
git remote add <name> <url><name>: Remote name; <url>: Repository URLAdd a Remote
git remote set-url <name> <url><name>: Remote name; <url>: New URLChange the Remote URL
git remote rename <old> <new><old>: Old name; <new>: New nameRename a Remote
git remote remove <name><name>: Remote nameRemove a Remote

Fetch / Pull / Push

CommandParameter DescriptionEffect
git fetchNoneFetch Remote updates without directly modifying the current Branch
git fetch <remote><remote>: Remote nameFetch updates from the specified Remote
git fetch --all--all: All RemotesFetch updates from all Remotes
git fetch --prune--prune: PruneRemove Remote-tracking Branches that no longer exist on the Remote while fetching updates
git pullNoneFetch Remote updates and integrate them into the current Branch
git pull <remote> <branch><remote>: Remote; <branch>: BranchFetch and integrate updates from the specified Remote Branch
git pull --rebase--rebase: Use RebaseFetch and integrate local Commits using Rebase
git pull --no-rebase--no-rebase: Use MergeFetch and integrate local Commits using Merge
git pull --ff-only--ff-only: Fast-forward onlyIntegrate updates only when Fast-forward is possible
git pushNonePush the current Branch to the configured Upstream
git push <remote> <branch><remote>: Remote; <branch>: BranchPush the local Branch to the specified Remote
git push -u <remote> <branch>-u: Set UpstreamPush and establish an Upstream relationship
git push --force--force: ForceForce-update the Remote Branch, which may overwrite remote history
git push --force-with-lease--force-with-leaseForce-update the Remote Branch under safer conditions
git push <remote> --delete <branch>--delete: Delete; <branch>: Remote BranchDelete a Remote Branch
git push --tags--tags: All TagsPush all local Tags to the Remote

Rebase

CommandParameter DescriptionEffect
git rebase <branch><branch>: Base BranchReapply the current Branch’s Commits on top of the specified Branch
git rebase -i <commit>-i: interactive; <commit>: Starting CommitInteractively edit and organize Commit history
git rebase --continue--continueContinue the Rebase after resolving a Conflict
git rebase --abort--abortAbort the current Rebase and restore the original state
git rebase --skip--skipSkip the Commit currently being applied

Tag

CommandParameter DescriptionEffect
git tagNoneList Tags
git tag <tag><tag>: Tag nameCreate a Lightweight Tag
git tag -a <tag> -m "<message>"-a: annotated; -m: messageCreate an Annotated Tag
git show <tag><tag>: Tag nameView detailed information about the Tag
git tag -d <tag>-d: deleteDelete a local Tag
git push <remote> <tag><remote>: Remote; <tag>: TagPush the specified Tag
git push <remote> --tags--tags: All TagsPush all Tags
git push <remote> --delete <tag>--delete: Delete; <tag>: TagDelete a Remote Tag

Worktree

CommandParameter DescriptionEffect
git worktree listNoneList the Worktrees of the current Repository
git worktree add <path> <branch><path>: Worktree path; <branch>: BranchCreate an additional Worktree and Checkout the specified Branch
git worktree remove <path><path>: Worktree pathRemove the specified Worktree
git worktree pruneNoneClean up administrative information for Worktrees that no longer exist

Submodule

CommandParameter DescriptionEffect
git submodule add <url><url>: Another Repository URLAdd another Repository to the current project as a Submodule
git submodule initNoneInitialize the configuration for existing Submodules
git submodule updateNoneUpdate Submodules to the Commits specified by the parent Repository
git submodule update --init --recursive--init: Initialize; --recursive: Process recursivelyInitialize and update all Submodules, including nested Submodules
git submodule update --remote--remote: Track the Remote BranchUpdate the Submodule to the latest version of its Remote Branch