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
配置用户信息
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
初始化仓库
git init
git init
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
查看状态
git status
git status
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 # 包括删除在内的所有更改
Commit changes
提交更改
git commit -m "Commit message"
git commit -m "Commit message"
Add and commit in one step
一步完成添加与提交
git commit -am "Message"
git commit -am "Message"
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 # 保留原提交信息
undefinedViewing changes
查看更改
bash
undefinedbash
undefinedShow unstaged changes
显示未暂存的更改
git diff
git diff
Show staged changes
显示已暂存的更改
git diff --staged
git diff --staged
Show changes in specific file
显示特定文件的更改
git diff file.txt
git diff file.txt
Show changes between commits
显示两次提交之间的更改
git diff commit1 commit2
undefinedgit diff commit1 commit2
undefinedBranching & Merging
分支与合并
Branch management
分支管理
bash
undefinedbash
undefinedList branches
列出分支
git branch
git branch -a # Include remote branches
git branch
git branch -a # 包括远程分支
Create branch
创建分支
git branch feature-name
git branch feature-name
Switch branch
切换分支
git checkout feature-name
git switch feature-name # Modern alternative
git checkout feature-name
git switch feature-name # 现代替代命令
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
删除分支
git branch -d branch-name
git branch -D branch-name # Force delete
git branch -d branch-name
git branch -D branch-name # 强制删除
Rename branch
重命名分支
git branch -m old-name new-name
undefinedgit branch -m old-name new-name
undefinedMerging
合并
bash
undefinedbash
undefinedMerge branch into current
将指定分支合并到当前分支
git merge feature-name
git merge feature-name
Merge with no fast-forward
禁用快进合并
git merge --no-ff feature-name
git merge --no-ff feature-name
Abort merge
终止合并
git merge --abort
git merge --abort
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
列出远程仓库
git remote -v
git remote -v
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
修改远程仓库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
删除远程仓库
git remote remove origin
undefinedgit remote remove origin
undefinedSyncing with remote
与远程仓库同步
bash
undefinedbash
undefinedFetch from remote
从远程仓库拉取更新(不合并)
git fetch origin
git fetch origin
Pull changes (fetch + merge)
拉取并合并更改
git pull
git pull
Pull with rebase
拉取并变基
git pull --rebase
git pull --rebase
Push changes
推送更改到远程仓库
git push
git push
Push new branch
推送新分支到远程仓库
git push -u origin branch-name
git push -u origin branch-name
Force push (careful!)
强制推送(谨慎操作!)
git push --force-with-lease
undefinedgit push --force-with-lease
undefinedHistory & Logs
历史记录与日志
Viewing history
查看历史记录
bash
undefinedbash
undefinedShow commit history
显示提交历史
git log
git log
One line per commit
每条提交显示为一行
git log --oneline
git log --oneline
With graph
显示分支图
git log --graph --oneline --all
git log --graph --oneline --all
Last N commits
显示最近N条提交
git log -5
git log -5
Commits by author
显示指定作者的提交
git log --author="Name"
git log --author="Name"
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
显示特定文件的提交历史
git log -- file.txt
undefinedgit log -- file.txt
undefinedSearching history
搜索历史记录
bash
undefinedbash
undefinedSearch commit messages
搜索提交信息
git log --grep="bug fix"
git log --grep="bug fix"
Search code changes
搜索代码更改
git log -S "function_name"
git log -S "function_name"
Show who changed each line
显示每行代码的修改者
git blame file.txt
git blame file.txt
Find commit that introduced bug
定位引入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
丢弃文件中的更改
git restore file.txt
git checkout -- file.txt # Old way
git restore file.txt
git checkout -- file.txt # 旧命令
Discard all changes
丢弃所有更改
git restore .
undefinedgit restore .
undefinedStaging area
暂存区
bash
undefinedbash
undefinedUnstage file
将文件移出暂存区
git restore --staged file.txt
git reset HEAD file.txt # Old way
git restore --staged file.txt
git reset HEAD file.txt # 旧命令
Unstage all
清空暂存区
git reset
undefinedgit reset
undefinedCommits
提交记录
bash
undefinedbash
undefinedUndo last commit (keep changes)
撤销最后一次提交(保留更改)
git reset --soft HEAD~1
git reset --soft HEAD~1
Undo last commit (discard changes)
撤销最后一次提交(丢弃更改)
git reset --hard HEAD~1
git reset --hard HEAD~1
Revert commit (create new commit)
回滚指定提交(创建新提交)
git revert commit-hash
git revert commit-hash
Reset to specific commit
重置到指定提交
git reset --hard commit-hash
undefinedgit reset --hard commit-hash
undefinedStashing
暂存更改(Stash)
bash
undefinedbash
undefinedStash changes
暂存当前更改
git stash
git stash
Stash with message
添加消息暂存更改
git stash save "Work in progress"
git stash save "Work in progress"
List stashes
列出所有暂存
git stash list
git stash list
Apply latest stash
应用最近一次暂存
git stash apply
git stash apply
Apply and remove stash
应用并删除最近一次暂存
git stash pop
git stash pop
Apply specific stash
应用指定暂存
git stash apply stash@{2}
git stash apply stash@{2}
Delete stash
删除指定暂存
git stash drop stash@{0}
git stash drop stash@{0}
Clear all stashes
清空所有暂存
git stash clear
undefinedgit stash clear
undefinedRebasing
变基(Rebase)
bash
undefinedbash
undefinedRebase current branch
对当前分支执行变基
git rebase main
git rebase main
Interactive rebase (last 3 commits)
交互式变基(最近3条提交)
git rebase -i HEAD~3
git rebase -i HEAD~3
Continue after resolving conflicts
解决冲突后继续变基
git rebase --continue
git rebase --continue
Skip current commit
跳过当前提交
git rebase --skip
git rebase --skip
Abort rebase
终止变基
git rebase --abort
undefinedgit rebase --abort
undefinedTags
标签(Tags)
bash
undefinedbash
undefinedList tags
列出所有标签
git tag
git tag
Create lightweight tag
创建轻量标签
git tag v1.0.0
git tag v1.0.0
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
为指定提交打标签
git tag v1.0.0 commit-hash
git tag v1.0.0 commit-hash
Push tag
推送标签到远程仓库
git push origin v1.0.0
git push origin v1.0.0
Push all tags
推送所有标签到远程仓库
git push --tags
git push --tags
Delete tag
删除本地标签
git tag -d v1.0.0
git push origin --delete v1.0.0
undefinedgit tag -d v1.0.0
Advanced Operations
删除远程标签
Cherry-pick
—
bash
undefinedgit push origin --delete v1.0.0
undefinedApply specific commit
高级操作
—
樱桃采摘(Cherry-pick)
git cherry-pick commit-hash
bash
undefinedCherry-pick without committing
应用指定提交的更改
git cherry-pick -n commit-hash
undefinedgit cherry-pick commit-hash
Submodules
应用指定提交的更改但不提交
bash
undefinedgit cherry-pick -n commit-hash
undefinedAdd submodule
子模块(Submodules)
git submodule add https://github.com/user/repo.git path/
bash
undefinedInitialize submodules
添加子模块
git submodule init
git submodule add https://github.com/user/repo.git path/
Update submodules
初始化子模块
git submodule update
git submodule init
Clone with submodules
更新子模块
git clone --recursive https://github.com/user/repo.git
undefinedgit submodule update
Clean
克隆包含子模块的仓库
bash
undefinedgit clone --recursive https://github.com/user/repo.git
undefinedPreview files to be deleted
清理(Clean)
git clean -n
bash
undefinedDelete untracked files
预览将被删除的文件
git clean -f
git clean -n
Delete untracked files and directories
删除未跟踪的文件
git clean -fd
git clean -f
Include ignored files
删除未跟踪的文件和目录
git clean -fdx
undefinedgit clean -fd
Common Workflows
包括删除被忽略的文件
Feature branch workflow:
bash
git checkout -b feature/new-featuregit clean -fdx
undefinedMake changes
常见工作流
git add .
git commit -m "Add new feature"
git push -u origin feature/new-feature
功能分支工作流:
bash
git checkout -b feature/new-featureCreate 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 add .
git commit -m "Add new feature"
git push -u origin feature/new-feature
Fix bug
创建PR,合并后:
git commit -am "Fix critical bug"
git push -u origin 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-bugAfter merge:
修复bug
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 commit -am "Fix critical bug"
git push -u origin hotfix/critical-bug
Useful 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-editgit 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 mainTips
实用别名
- 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
添加到 :
~/.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-editCommon 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- 经常提交,后续优化(使用交互式变基)
- 编写有意义的提交信息
- 使用 排除不需要追踪的文件
.gitignore - 切勿对共享分支执行强制推送
- 开始工作前先拉取最新代码
- 使用功能分支,而非直接在主分支操作
- 合并前对功能分支执行变基
- 使用 替代
--force-with-lease--force
Edit files to resolve conflicts
常见问题
git add resolved-files
git commit # Or git merge --continue
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
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/
git add resolved-files
git commit # 或 git merge --continue
undefined—
文档资源
—
官方文档:https://git-scm.com/doc
Pro Git 书籍:https://git-scm.com/book
可视化Git指南:https://marklodato.github.io/visual-git-guide/