git-essentials

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Essentials

Git 核心指南

Essential Git commands for version control and collaboration.
用于版本控制与协作的必备Git命令。

Initial Setup

初始设置

bash
undefined
bash
undefined

Configure user

Configure user

git config --global user.name "Your Name" git config --global user.email "your@email.com"
git config --global user.name "Your Name" git config --global user.email "your@email.com"

Initialize repository

Initialize repository

git init
git init

Clone repository

Clone repository

undefined
undefined

Basic Workflow

基础工作流

Staging and committing

暂存与提交

bash
undefined
bash
undefined

Check status

Check status

git status
git status

Add files to staging

Add files to staging

git add file.txt git add . git add -A # All changes including deletions
git add file.txt git add . git add -A # All changes including deletions

Commit changes

Commit changes

git commit -m "Commit message"
git commit -m "Commit message"

Add and commit in one step

Add and commit in one step

git commit -am "Message"
git commit -am "Message"

Amend last commit

Amend last commit

git commit --amend -m "New message" git commit --amend --no-edit # Keep message
undefined
git commit --amend -m "New message" git commit --amend --no-edit # Keep message
undefined

Viewing changes

查看变更

bash
undefined
bash
undefined

Show unstaged changes

Show unstaged changes

git diff
git diff

Show staged changes

Show staged changes

git diff --staged
git diff --staged

Show changes in specific file

Show changes in specific file

git diff file.txt
git diff file.txt

Show changes between commits

Show changes between commits

git diff commit1 commit2
undefined
git diff commit1 commit2
undefined

Branching & Merging

分支与合并

Branch management

分支管理

bash
undefined
bash
undefined

List branches

List branches

git branch git branch -a # Include remote branches
git branch git branch -a # Include remote branches

Create branch

Create branch

git branch feature-name
git branch feature-name

Switch branch

Switch branch

git checkout feature-name git switch feature-name # Modern alternative
git checkout feature-name git switch feature-name # Modern alternative

Create and switch

Create and switch

git checkout -b feature-name git switch -c feature-name
git checkout -b feature-name git switch -c feature-name

Delete branch

Delete branch

git branch -d branch-name git branch -D branch-name # Force delete
git branch -d branch-name git branch -D branch-name # Force delete

Rename branch

Rename branch

git branch -m old-name new-name
undefined
git branch -m old-name new-name
undefined

Merging

合并操作

bash
undefined
bash
undefined

Merge branch into current

Merge branch into current

git merge feature-name
git merge feature-name

Merge with no fast-forward

Merge with no fast-forward

git merge --no-ff feature-name
git merge --no-ff feature-name

Abort merge

Abort merge

git merge --abort
git merge --abort

Show merge conflicts

Show merge conflicts

git diff --name-only --diff-filter=U
undefined
git diff --name-only --diff-filter=U
undefined

Remote Operations

远程仓库操作

Managing remotes

远程仓库管理

bash
undefined
bash
undefined

List remotes

List remotes

git remote -v
git remote -v

Add remote

Add remote

git remote add origin https://github.com/user/repo.git
git remote add origin https://github.com/user/repo.git

Change remote URL

Change remote URL

git remote set-url origin https://github.com/user/new-repo.git
git remote set-url origin https://github.com/user/new-repo.git

Remove remote

Remove remote

git remote remove origin
undefined
git remote remove origin
undefined

Syncing with remote

与远程仓库同步

bash
undefined
bash
undefined

Fetch from remote

Fetch from remote

git fetch origin
git fetch origin

Pull changes (fetch + merge)

Pull changes (fetch + merge)

git pull
git pull

Pull with rebase

Pull with rebase

git pull --rebase
git pull --rebase

Push changes

Push changes

git push
git push

Push new branch

Push new branch

git push -u origin branch-name
git push -u origin branch-name

Force push (careful!)

Force push (careful!)

git push --force-with-lease
undefined
git push --force-with-lease
undefined

History & Logs

历史记录与日志

Viewing history

查看历史记录

bash
undefined
bash
undefined

Show commit history

Show commit history

git log
git log

One line per commit

One line per commit

git log --oneline
git log --oneline

With graph

With graph

git log --graph --oneline --all
git log --graph --oneline --all

Last N commits

Last N commits

git log -5
git log -5

Commits by author

Commits by author

git log --author="Name"
git log --author="Name"

Commits in date range

Commits in date range

git log --since="2 weeks ago" git log --until="2024-01-01"
git log --since="2 weeks ago" git log --until="2024-01-01"

File history

File history

git log -- file.txt
undefined
git log -- file.txt
undefined

Searching history

搜索历史记录

bash
undefined
bash
undefined

Search commit messages

Search commit messages

git log --grep="bug fix"
git log --grep="bug fix"

Search code changes

Search code changes

git log -S "function_name"
git log -S "function_name"

Show who changed each line

Show who changed each line

git blame file.txt
git blame file.txt

Find commit that introduced bug

Find commit that introduced bug

git bisect start git bisect bad git bisect good commit-hash
undefined
git bisect start git bisect bad git bisect good commit-hash
undefined

Undoing Changes

撤销变更

Working directory

工作区

bash
undefined
bash
undefined

Discard changes in file

Discard changes in file

git restore file.txt git checkout -- file.txt # Old way
git restore file.txt git checkout -- file.txt # Old way

Discard all changes

Discard all changes

git restore .
undefined
git restore .
undefined

Staging area

暂存区

bash
undefined
bash
undefined

Unstage file

Unstage file

git restore --staged file.txt git reset HEAD file.txt # Old way
git restore --staged file.txt git reset HEAD file.txt # Old way

Unstage all

Unstage all

git reset
undefined
git reset
undefined

Commits

提交记录

bash
undefined
bash
undefined

Undo last commit (keep changes)

Undo last commit (keep changes)

git reset --soft HEAD~1
git reset --soft HEAD~1

Undo last commit (discard changes)

Undo last commit (discard changes)

git reset --hard HEAD~1
git reset --hard HEAD~1

Revert commit (create new commit)

Revert commit (create new commit)

git revert commit-hash
git revert commit-hash

Reset to specific commit

Reset to specific commit

git reset --hard commit-hash
undefined
git reset --hard commit-hash
undefined

Stashing

暂存变更(Stash)

bash
undefined
bash
undefined

Stash changes

Stash changes

git stash
git stash

Stash with message

Stash with message

git stash save "Work in progress"
git stash save "Work in progress"

List stashes

List stashes

git stash list
git stash list

Apply latest stash

Apply latest stash

git stash apply
git stash apply

Apply and remove stash

Apply and remove stash

git stash pop
git stash pop

Apply specific stash

Apply specific stash

git stash apply stash@{2}
git stash apply stash@{2}

Delete stash

Delete stash

git stash drop stash@{0}
git stash drop stash@{0}

Clear all stashes

Clear all stashes

git stash clear
undefined
git stash clear
undefined

Rebasing

变基(Rebase)

bash
undefined
bash
undefined

Rebase current branch

Rebase current branch

git rebase main
git rebase main

Interactive rebase (last 3 commits)

Interactive rebase (last 3 commits)

git rebase -i HEAD~3
git rebase -i HEAD~3

Continue after resolving conflicts

Continue after resolving conflicts

git rebase --continue
git rebase --continue

Skip current commit

Skip current commit

git rebase --skip
git rebase --skip

Abort rebase

Abort rebase

git rebase --abort
undefined
git rebase --abort
undefined

Tags

标签(Tags)

bash
undefined
bash
undefined

List tags

List tags

git tag
git tag

Create lightweight tag

Create lightweight tag

git tag v1.0.0
git tag v1.0.0

Create annotated tag

Create annotated tag

git tag -a v1.0.0 -m "Version 1.0.0"
git tag -a v1.0.0 -m "Version 1.0.0"

Tag specific commit

Tag specific commit

git tag v1.0.0 commit-hash
git tag v1.0.0 commit-hash

Push tag

Push tag

git push origin v1.0.0
git push origin v1.0.0

Push all tags

Push all tags

git push --tags
git push --tags

Delete tag

Delete tag

git tag -d v1.0.0 git push origin --delete v1.0.0
undefined
git tag -d v1.0.0 git push origin --delete v1.0.0
undefined

Advanced Operations

高级操作

Cherry-pick

拣选提交(Cherry-pick)

bash
undefined
bash
undefined

Apply specific commit

Apply specific commit

git cherry-pick commit-hash
git cherry-pick commit-hash

Cherry-pick without committing

Cherry-pick without committing

git cherry-pick -n commit-hash
undefined
git cherry-pick -n commit-hash
undefined

Submodules

子模块(Submodules)

bash
undefined
bash
undefined

Add submodule

Add submodule

git submodule add https://github.com/user/repo.git path/
git submodule add https://github.com/user/repo.git path/

Initialize submodules

Initialize submodules

git submodule init
git submodule init

Update submodules

Update submodules

git submodule update
git submodule update

Clone with submodules

Clone with submodules

git clone --recursive https://github.com/user/repo.git
undefined
git clone --recursive https://github.com/user/repo.git
undefined

Clean

清理文件(Clean)

bash
undefined
bash
undefined

Preview files to be deleted

Preview files to be deleted

git clean -n
git clean -n

Delete untracked files

Delete untracked files

git clean -f
git clean -f

Delete untracked files and directories

Delete untracked files and directories

git clean -fd
git clean -fd

Include ignored files

Include ignored files

git clean -fdx
undefined
git clean -fdx
undefined

Common Workflows

常用工作流

Feature branch workflow:
bash
git checkout -b feature/new-feature
功能分支工作流:
bash
git checkout -b feature/new-feature

Make changes

Make changes

git add . git commit -m "Add new feature" git push -u origin feature/new-feature
git add . git commit -m "Add new feature" git push -u origin feature/new-feature

Create PR, then after merge:

Create PR, then after merge:

git checkout main git pull git branch -d feature/new-feature

**Hotfix workflow:**
```bash
git checkout main
git pull
git checkout -b hotfix/critical-bug
git checkout main git pull git branch -d feature/new-feature

**热修复工作流:**
```bash
git checkout main
git pull
git checkout -b hotfix/critical-bug

Fix bug

Fix bug

git commit -am "Fix critical bug" git push -u origin hotfix/critical-bug
git commit -am "Fix critical bug" git push -u origin hotfix/critical-bug

After merge:

After merge:

git checkout main && git pull

**Syncing fork:**
```bash
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
git checkout main && git pull

**同步复刻仓库:**
```bash
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Useful Aliases

实用别名

Add to
~/.gitconfig
:
ini
[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = log --graph --oneline --all
    amend = commit --amend --no-edit
添加到
~/.gitconfig
ini
[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = log --graph --oneline --all
    amend = commit --amend --no-edit

Tips

小贴士

  • Commit often, perfect later (interactive rebase)
  • Write meaningful commit messages
  • Use
    .gitignore
    for files to exclude
  • Never force push to shared branches
  • Pull before starting work
  • Use feature branches, not main
  • Rebase feature branches before merging
  • Use
    --force-with-lease
    instead of
    --force
  • 频繁提交,后续优化(使用交互式变基)
  • 撰写有意义的提交信息
  • 使用
    .gitignore
    排除无需追踪的文件
  • 切勿对共享分支执行强制推送
  • 开始工作前先拉取最新代码
  • 使用功能分支,而非直接在main分支开发
  • 合并前对功能分支执行变基
  • 使用
    --force-with-lease
    替代
    --force

Common Issues

常见问题

Undo accidental commit:
bash
git reset --soft HEAD~1
Recover deleted branch:
bash
git reflog
git checkout -b branch-name <commit-hash>
Fix wrong commit message:
bash
git commit --amend -m "Correct message"
Resolve merge conflicts:
bash
undefined
撤销意外提交:
bash
git reset --soft HEAD~1
恢复已删除的分支:
bash
git reflog
git checkout -b branch-name <commit-hash>
修正错误的提交信息:
bash
git commit --amend -m "Correct message"
解决合并冲突:
bash
undefined

Edit files to resolve conflicts

Edit files to resolve conflicts

git add resolved-files git commit # Or git merge --continue
undefined
git add resolved-files git commit # Or git merge --continue
undefined

Documentation

参考文档