git-essentials
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Essentials
Git 核心指南
Essential Git commands for version control and collaboration.
用于版本控制与协作的必备Git命令。
Initial Setup
初始设置
bash
undefinedbash
undefinedConfigure 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
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git custom-name
undefinedgit clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git custom-name
undefinedBasic Workflow
基础工作流
Staging and committing
暂存与提交
bash
undefinedbash
undefinedCheck 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
undefinedgit commit --amend -m "New message"
git commit --amend --no-edit # Keep message
undefinedViewing changes
查看变更
bash
undefinedbash
undefinedShow 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
undefinedgit diff commit1 commit2
undefinedBranching & Merging
分支与合并
Branch management
分支管理
bash
undefinedbash
undefinedList 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
undefinedgit branch -m old-name new-name
undefinedMerging
合并操作
bash
undefinedbash
undefinedMerge 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
undefinedgit diff --name-only --diff-filter=U
undefinedRemote Operations
远程仓库操作
Managing remotes
远程仓库管理
bash
undefinedbash
undefinedList 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
undefinedgit remote remove origin
undefinedSyncing with remote
与远程仓库同步
bash
undefinedbash
undefinedFetch 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
undefinedgit push --force-with-lease
undefinedHistory & Logs
历史记录与日志
Viewing history
查看历史记录
bash
undefinedbash
undefinedShow 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
undefinedgit log -- file.txt
undefinedSearching history
搜索历史记录
bash
undefinedbash
undefinedSearch 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
undefinedgit bisect start
git bisect bad
git bisect good commit-hash
undefinedUndoing Changes
撤销变更
Working directory
工作区
bash
undefinedbash
undefinedDiscard 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 .
undefinedgit restore .
undefinedStaging area
暂存区
bash
undefinedbash
undefinedUnstage 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
undefinedgit reset
undefinedCommits
提交记录
bash
undefinedbash
undefinedUndo 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
undefinedgit reset --hard commit-hash
undefinedStashing
暂存变更(Stash)
bash
undefinedbash
undefinedStash 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
undefinedgit stash clear
undefinedRebasing
变基(Rebase)
bash
undefinedbash
undefinedRebase 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
undefinedgit rebase --abort
undefinedTags
标签(Tags)
bash
undefinedbash
undefinedList 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
undefinedgit tag -d v1.0.0
git push origin --delete v1.0.0
undefinedAdvanced Operations
高级操作
Cherry-pick
拣选提交(Cherry-pick)
bash
undefinedbash
undefinedApply 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
undefinedgit cherry-pick -n commit-hash
undefinedSubmodules
子模块(Submodules)
bash
undefinedbash
undefinedAdd 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
undefinedgit clone --recursive https://github.com/user/repo.git
undefinedClean
清理文件(Clean)
bash
undefinedbash
undefinedPreview 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
undefinedgit clean -fdx
undefinedCommon Workflows
常用工作流
Feature branch workflow:
bash
git checkout -b feature/new-feature功能分支工作流:
bash
git checkout -b feature/new-featureMake 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-buggit checkout main
git pull
git branch -d feature/new-feature
**热修复工作流:**
```bash
git checkout main
git pull
git checkout -b hotfix/critical-bugFix 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 maingit 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 mainUseful Aliases
实用别名
Add to :
~/.gitconfigini
[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添加到 :
~/.gitconfigini
[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-editTips
小贴士
- Commit often, perfect later (interactive rebase)
- Write meaningful commit messages
- Use for files to exclude
.gitignore - Never force push to shared branches
- Pull before starting work
- Use feature branches, not main
- Rebase feature branches before merging
- Use instead of
--force-with-lease--force
- 频繁提交,后续优化(使用交互式变基)
- 撰写有意义的提交信息
- 使用 排除无需追踪的文件
.gitignore - 切勿对共享分支执行强制推送
- 开始工作前先拉取最新代码
- 使用功能分支,而非直接在main分支开发
- 合并前对功能分支执行变基
- 使用 替代
--force-with-lease--force
Common Issues
常见问题
Undo accidental commit:
bash
git reset --soft HEAD~1Recover 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
undefinedEdit files to resolve conflicts
Edit files to resolve conflicts
git add resolved-files
git commit # Or git merge --continue
undefinedgit add resolved-files
git commit # Or git merge --continue
undefinedDocumentation
参考文档
Official docs: https://git-scm.com/doc
Pro Git book: https://git-scm.com/book
Visual Git guide: https://marklodato.github.io/visual-git-guide/
官方文档:https://git-scm.com/doc
Pro Git 书籍:https://git-scm.com/book
可视化Git指南:https://marklodato.github.io/visual-git-guide/