git 获取
Downloading remote 更改使用 git 获取 — safe alternative 到 git 拉取那个做 不合并.
你将学到
- 如何 git 获取 downloads 更改不使用修改你的工作 directory
- 为什么获取是 safer than 拉取
- 获取特定分支 and remotes
- Inspecting 获取更改之前合并
概念讲解
What git fetch Does
git 获取 downloads commits, files, and refs from a remote repository, but does not merge them into your local branches. Your working directory stays unchanged. This makes it the safe way to see what is new on the remote without risking your local work.
git fetch
After fetching, the remote-tracking branches (like 原点/主要) are updated, but your local 主要 is not. You can inspect the changes and decide when (or whether) to merge.
fetch vs pull
This is a crucial distinction:
git 获取— downloads remote changes. Your local branches and working directory are NOT modified. Safe.git 拉取— runsgit 获取THENgit 合并. Your local branch IS modified. Can create merge commits or conflicts.
git pull = git fetch + git merge
git 拉取 is convenient but can surprise you with merge commits or conflicts. git 获取 gives you control — you see what changed first, then decide how to integrate it.
The Safe Update Workflow
# 1. Fetch remote changes (does not touch your working directory)
git fetch origin
# 2. See what changed on the remote
git log --oneline main..origin/main
# shows commits on origin/main that are not in your local main
# 3. See the actual diff
git diff main origin/main
# 4. When you are ready, merge
git merge origin/main
# or rebase:
git rebase origin/main
This workflow lets you review changes before they touch your code.
Fetching Specific Branches
git fetch origin main # fetch only the main branch
git fetch origin feature-login # fetch a specific feature branch
git fetch upstream # fetch from the upstream remote
git fetch --all # fetch from all remotes
Fetching and Pruning
When branches are deleted on the remote, your local remote-tracking references become stale. Prune them:
git fetch --prune
# or set it to happen automatically:
git config --global fetch.prune true
This removes references to remote branches that no longer exist, keeping your branch list clean.
Shallow Fetch
If you did a shallow clone, you can fetch more history:
git fetch --unshallow # fetch the complete history
git fetch --depth=100 # fetch up to 100 commits deep
When to Use fetch vs pull
Use git fetch when:
- You want to see what is new before integrating it
- You are not ready to merge (you have local work in progress)
- You want to avoid unexpected merge commits
- You are reviewing a colleague's branch
Use git pull when:
- You just want to get the latest and continue working
- You are on a feature branch and want to stay in sync
- You are confident there will be no conflicts
Many experienced developers prefer git 获取 + git 合并 over git 拉取 for the control it provides.
Fetching Tags
git fetch --tags # fetch all tags
git fetch --all --tags # fetch all remotes and all tags
Tags are not fetched by default (since Git 2.0), so you need --标签 if you want them.
示例代码
# --- The safe update workflow ---
git fetch origin
# See what commits are new on the remote
git log --oneline main..origin/main
# abc1234 feat: add search feature (by teammate)
# def5678 fix: resolve pagination bug (by teammate)
# See the actual changes
git diff main origin/main --stat
# src/search.js | 45 +++++++++++++++++
# src/pagination.js | 12 +++--
# Now decide: merge or rebase?
git merge origin/main
# or:
git rebase origin/main
# --- Fetch a specific branch to review ---
git fetch origin feature-login
git log --oneline origin/feature-login -5
git diff main origin/feature-login --stat
# --- Fetch from upstream (forking workflow) ---
git fetch upstream
git log --oneline main..upstream/main
git merge upstream/main
# --- Prune deleted remote branches ---
git fetch --prune
# - [deleted] (none) -> origin/old-feature
# --- Set automatic pruning ---
git config --global fetch.prune true
git fetch # prunes automatically from now on
# --- Fetch all remotes ---
git fetch --all
# --- Compare fetch vs pull ---
git fetch origin # safe: nothing changes locally
git log origin/main # see what the remote has
# ... decide to merge ...
git merge origin/main # now integrate the changes
Shows safe 获取-然后-review-然后-合并 workflow, 获取特定分支 for review, 获取从 upstream, pruning 删除分支, and comparison 之间获取 and 拉取. 键 habit 是: 获取首先, review diff, 然后合并当 ready.
动手试一试
- Code Diff
Review 获取更改使用 git diff 之前合并它们.
- CI/CD YAML Generator
获取触发能 是部分的 CI sync workflows.
常见错误
错误写法
Using git 拉取 and 是 surprised 通过 unexpected 合并提交
正确做法
git 拉取 = git 获取 + git 合并. 如果 remote and 局部有 diverged, 拉取创建合并提交. 到 avoid 这个, use git 获取首先到 see 什么是 新, 然后 git rebase 原点/主要到 keep 你的 history linear. Or use git 拉取 --rebase 到做 它 in one 步骤.
错误写法
Forgetting 到 prune and seeing stale remote 分支 in git 分支 -r
正确做法
当分支是 删除 on remote, 局部引用 persist 直到 pruned. 运行 git 获取 --prune 到 clean 它们上, or 设置 git 配置 --全局获取.prune 真到 prune automatically on 每个获取. 这个 keeps 你的分支列表准确.
相关资源
相关代码片段
- Git Remote Snippet
Remote sync 模式使用获取.
相关速查表
- Git Cheatsheet
获取, 获取 --prune, and 获取 --所有 commands.
练习 git 获取
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。