全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-11 6 分钟 ✍️ juanwangdev

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
步骤操作说明
1fetch获取远程更新
2merge合并到当前分支

拉取示例

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
--rebasefetch + 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 保持本地与远程同步,避免推送时冲突。

要点总结

  1. git pull = fetch + merge
  2. --rebase 用 rebase 代替 merge
  3. 拉取冲突需手动解决
  4. 有本地变更时建议先 stash
  5. 定期 pull 保持同步

📝 发现内容有误?点击此处直接编辑

← 上一篇 git fetch 获取远程更新
下一篇 → git push -u 设置上游分支
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库