git pull 拉取更改
git pull 命令从远程仓库获取更新并自动合并到当前分支。
基本用法
Bash
# 拉取当前分支的更新
git pull
# 拉取指定远程和分支
git pull <remote> <branch>
# 拉取 origin 的 main 分支
git pull origin main
pull = fetch + merge
git pull 实际上是两个操作的组合:
Bash
git pull = git fetch + git merge
| 步骤 | 操作 | 说明 |
|---|---|---|
| 1 | fetch | 获取远程更新 |
| 2 | merge | 合并到当前分支 |
拉取示例
Bash
# 拉取当前分支更新
git pull
# 输出
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 1), reused 3 (delta 1)
Unpacking objects: 100% (3/3), done.
From https://github.com/user/repo
a1b2c3..b2c3d4 main -> origin/main
Updating a1b2c3..b2c3d4
Fast-forward
README.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
常用选项
| 选项 | 说明 |
|---|---|
| 无选项 | fetch + merge |
| --rebase | fetch + rebase |
| --ff-only | 只允许快进合并 |
| --no-rebase | 禁止 rebase |
pull --rebase
Bash
# 使用 rebase 代替 merge
git pull --rebase
# 或简写
git pull -r
对比:
| 方式 | 历史 | 适用场景 |
|---|---|---|
| pull(merge) | 创建合并提交 | 团队协作 |
| pull --rebase | 保持线性 | 本地分支更新 |
处理拉取冲突
Bash
# 拉取时产生冲突
$ git pull
CONFLICT (content): Merge conflict in README.md
# 解决步骤
# 1. 编辑冲突文件
# 2. git add <file>
# 3. git commit(merge 模式)
# 或 git rebase --continue(rebase 模式)
推荐的拉取流程
Bash
# 查看本地状态
git status
# 暂存本地变更(如果有)
git stash
# 拉取更新
git pull --rebase
# 恢复暂存变更
git stash pop
配置默认 pull 行为
text
# 设置分支默认使用 rebase
git config branch.main.rebase true
# 全局设置 pull 使用 rebase
git config --global pull.rebase true
定期 pull 保持本地与远程同步,避免推送时冲突。
要点总结
- git pull = fetch + merge
--rebase用 rebase 代替 merge- 拉取冲突需手动解决
- 有本地变更时建议先 stash
- 定期 pull 保持同步
📝 发现内容有误?点击此处直接编辑