Skip to content

Git Quick Reference Manual

Creating Projects

git init

# Initialize local repository
git init

git clone

# Clone remote repository
git clone [url]

Basic Commands

git status

View the status of files in the current repository to help you understand the differences between your working directory, staging area, and repository.

git status

git add

# Add a file
git add file.md 
# Add folders and files
git add sub_folder 
# Add all changed files
git add .

git diff

git diff - Show changes between commits, commit and working tree, etc.

# Show all changes not added to the index
git diff

# Differences from commit records
git diff f2a9c2cad88cfe2206dc7e6c271d673365723add

# Display all changes that have been added to the index but not yet committed
git diff --cached

# Compare the differences with the last version
git diff HEAD^

git commit

# Commit all tracked changes
git commit -m 'add file.'  

# Add and commit a tracked file change, invalid for newly created files
git commit -am 'update a tracked file.' 

# Modify the content of the last commit message
git commit --amend

# Last commit adds changes, without modifying commit message
git commit --amend --no-edit

git reset

Resets the current branch's HEAD to the specified state. This command can be used to undo commits, restore files in the working directory, and more. The behavior of git reset depends on the mode you choose: --soft, --mixed, --hard, --merge, or --keep. Let's take a closer look at these options and their uses.

Command

git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

Option

  • --soft: Just move HEAD to the specified commit, without changing the staging area or working directory.
  • --mixed: By default, HEAD is moved and the staging area is reset to match the target commit, but the working directory is left unchanged. This means your changes remain in your working directory, but are no longer staged.
  • --hard: Move HEAD and reset the staging area and working directory to the target commit. All uncommitted changes will be lost.
  • --merge: If you have unmerged changes in your current working directory, using --merge will attempt to merge those changes into the target commit. If there are conflicts, the reset process will be interrupted.
  • --keep:Try to keep changes in the working directory unchanged, only move HEAD and update the index (staging area).
# Undo the most recent commit and keep the changes
git reset HEAD~1

# Undo the N most recent commits and keep changes
git reset HEAD~N

# Undo the most recent commit and remove changes
git reset --hard HEAD~1 

git rm

Command Delete Workspace Notify Git Auto-Stage RequiresAdd Risk
rm file ✅ Need Low
git rm file ❌ No Need Medium
git rm -r folder ❌ No Need ⚠️ High
git rm file.md
git rm -r folder

# On branch main
# Changes to be committed:
#  (use "git restore --staged <file>..." to unstage)
#   deleted:    start.md
#   deleted:    sub_folder/sub_file.md

git gc

The full name is git garbage collect, which cleans up unnecessary files in the repository, compresses data, and optimizes repository performance.

  • Merge small pack files.
  • Delete unreachable objects (garbage).
  • Compress repository size.
  • Improve Git operation speed.
git gc

git fsck

Check repository integrity.

  • Detect corrupted objects.
  • Find orphaned objects.
  • Verify connectivity.
  • Report potential issues.

Option

  • --full: By default, git fsck may skip some optimization checks; use this option to do a full check.
  • --strict: Being more rigorous about the inspection process could result in more potential, but not necessarily fatal, problems being reported.
  • --lost-found: Copies orphaned or problematic objects to the .git/lost-found directory for further analysis or recovery.
# git fsck [options]
git fsck --full

Branching and Merging

git branch

git branch
git branch --list
git branch -r
git branch -a 
# Create branch git branch [branchName]
git branch dev
# Create and switch branches
git checkout -b new-branch

# Commit the branch that has been merged into the current branch
git branch --merged
# Commit branches that are not fully merged into the current branch
git branch --no-merged

# Local branch name: git branch [-m|-M] <oldName> <newName>
git branch -m test test_new

git checkout

# git checkout [-q] [-f|-m] dev 
# -q Quiet Mode(quiet) 
# -f Force, Discard modifications
# -m Smart Merge(migrate,or --merge)
git checkout dev

# git checkout tag
git checkout v2.0  

# git checkout remote branch and create local branch
# git checkout remote branch auto create local branch
git checkout --track origin/dev
git checkout -b dev origin/dev  
# Create and switch branches
git checkout -b new-branch

# Delete local branch
git branch -d test_new

git merge

git merge - joins two or more development histories together, merging the target branch into the current branch.

# Basic merge
# The target branch name to be merged into the current branch: git merge <branch>
git checkout main
git merge dev

# Disable fast-forward merge
git merge --no-ff feature

# Compression and merging
# Squash all changes from the feature branch into one commit
git merge --squash feature
git commit -m "Squashed and merged feature branch"

git rebase

git rebase is a powerful command in Git that is used to reapply changes from one branch on top of another. Compared to git merge, it provides a linear way to integrate changes, making the commit history clearer and smoother.

# git rebase [<upstream>]
git rebase main

# 远程仓库的基础上工作,并希望拉取最新更改而不创建合并提交
# git pull --rebase 等同于先 git fetch 然后 git rebase origin/main。
git pull --rebase origin main

# 变基过程中对提交进行修改(如压缩、编辑或重排),可以使用交互式变基
# n 是你想回溯的提交数量。Git 会打开一个文本编辑器,允许你指定对每个提交的操作。
git rebase -i HEAD~n

git log

Command

 git log [<options>] [<revision-range>] [[--] <path>...]

Example

git log
git log -n
# 更多信息: 变更行数
git log --stat
# 更多信息: 变更内容 + 提交记录
git log -p -m
# git log <tag name>
git log v0.0.1

git stash

# 暂存当前修改
git stash
# 查看所有暂存列表
git stash list     
# 查看最暂存详情
git stash show -p stash@{0}                            
# 应用暂存
git stash pop [--index] [-q | --quiet] [<stash>]
git stash apply stash@{0}

# 暂存转新分支 git stash branch <branchName> [<stash>]
git stash branch dev_new stash@{0}
# 删除暂存
# git stash drop [-q | --quiet] [<stash>]
git stash drop stash@{0}

git tag

git tag
git tag -a v0.0.1 -m 'add a tag.'
# 删除 git tag -d <tagName>
git tag -d v0.0.2

# git tag -v <tagName>
git tag -v v0.0.1
# git show <tag name>
git show v0.0.1
# git log <tag name>
git log v0.0.1

Sharing and Updating Projects

git push

# -u --set-upstream 本地分支和远程分支之间建立了一个追踪关系(tracking relationship)
git push -u origin "main"

git remote

# 列表
git remote
# 丰富信息列表
git remote -v
git remote -v show <origin>
# 添加远程仓库名称和地址
git remote add origin git@github.com:xxx/git-demo.git
# 重命名远程仓库名称 git remote rename --no-progress <old> <new>
git remote rename origin origin_new
# 移除远程仓库:git remove <name>
git remove origin
# 获取远程仓库地址:git remote get-url [--push] [--all] <name>
git remote get-url origin
# 修改远程仓库地址
git remote set-url origin <new_url> <old_url>
git remote set-url origin --add https://github.com/xxx/git-demo.git
git remote set-url --delete origin https://github.com/xxx/git-demo.git
# Fetching: git remote [-v] update <name>  
git remote [-v] update origin 
# 删除与<name>关联的过时引用。默认情况下,删除<name>下的过时远程跟踪分支,但根据全局配置和远程配置,我们甚至可能会修剪尚未推送到那里的本地标签
# git remote prune [-n | --dry-run] <name>…​

Inspection and Comparison

git show

# git log

# git show hash
git show d4d30167d01d10d7de5e7aca699863dc27735678
git show d4d30
# git show <tag name>
git show v0.0.1

# git show HEAD
# 当前提交
git show HEAD 
git show HEAD~0
# 第一父链, 父提交
git show HEAD^ # git show HEAD~1
git show HEAD^~0
git show HEAD^1
git show HEAD^1~0
# 第一父链, 父提交的父提交,第二父提交
git show HEAD^^    
git show HEAD^~1

# 第二父链:HEAD 是一个合并提交, 有2个以上父提交
git show HEAD^2
git show HEAD^2~0

git show HEAD@{2}
git show main@{yesterday} 

Note

  • ^(脱字符):表示“父提交”
  • HEAD^ = HEAD^1:表示 HEAD 的第一个父提交,当前父链,第一父链。
    • HEAD = HEAD~0:当前提交
    • HEAD^ = HEAD^1~0 = HEAD~1:表示 HEAD 的第一个父提交(也就是上一个提交)。
    • HEAD^^ = HEAD^1~1 = HEAD~2:表示 HEAD 的父提交的父提交,即“祖父提交”.
    • HEAD^^^ = HEAD^1~2 = HEAD~3: 当前父链第三提交.
  • HEAD^2为当前HEAD的第二父提交,HEAD 是一个合并提交, 有2个以上父提交。

Setup And Config

Global Config

Set the global config.

git config --global user.name "your name"
git config --global user.email "your email"

Project Config

git config user.name "name.project.config"
git config user.email "mail@project.config"