Skip to content

git 推送 & git 拉取

Syncing 使用 remotes using git 推送 and git 拉取 — upstream tracking and force 推送 safety.

你将学到

  • 如何 git 推送发送局部提交到 remote
  • 如何 git 拉取获取 and 合并 remote 更改
  • 设置 upstream tracking 使用 -u
  • 当 and 为什么到 use force 推送 (and 它的 dangers)

概念讲解

git push — 推送更改到上游

git 推送 uploads your local commits to the remote repository. This is how you share your work with teammates and publish changes to GitHub.

git push origin main

This sends your local 主要 branch to the 主要 branch on 原点. If you have set up tracking (with -u), you can simply run git 推送 without specifying the remote and branch.

Upstream Tracking

When you create a new local branch and want to push it to the remote for the first time, use the -u flag:

git checkout -b feature-login
git push -u origin feature-login

The -u (or --设置-upstream) flag tells Git to remember the connection. After this, git 推送 and git 拉取 on this branch know which remote branch to use without you specifying it each time.

git pull — Getting Changes Down

git 拉取 does two things in sequence:

  1. git 获取 — downloads new commits from the remote
  2. git 合并 — merges them into your current branch
git pull

Always pull before you push when collaborating. If someone else pushed changes you do not have, your push will be rejected. Pull first to integrate their work, then push.

Push Rejections

If the remote has commits you do not have, Git rejects your push:

! [rejected]        main -> main (fetch first)
error: failed to push some refs

The fix is to pull first, resolve any conflicts, then push again:

git pull
# resolve conflicts if any
git push

Force Pushing

git 推送 --force (or -f) overwrites the remote branch with your local version, discarding any remote commits that are not in your local history. This is dangerous on shared branches because it can erase other people's work.

# Dangerous — overwrites remote history
git push --force origin main

# Safer — only force push if no one else added commits
git push --force-with-lease origin main

--force-with-lease is the safer alternative: it only force pushes if the remote has not changed since you last fetched. Always prefer this over --force.

When Force Push Is Appropriate

Force pushing is acceptable on:

  • Your own feature branches that no one else is using
  • After a rebase or amend that rewrote local history

Never force push to 主要 or any shared branch.

示例代码

              # First push of a new branch — set up tracking
git checkout -b feature-payment
git push -u origin feature-payment

# Subsequent pushes — tracking is set, so just:
git push

# Pull the latest changes from teammates before pushing
git pull
# resolve any merge conflicts if they occur
git push

# If push is rejected (remote has newer commits):
git pull --rebase   # rebase your commits on top of remote
git push

# Force push — ONLY on your own branches, never on main
git push --force-with-lease origin feature-payment

# Push all branches at once (rarely needed)
git push --all

# Push tags (covered in the git-tags lesson)
git push --tags

# Delete a remote branch
git push origin --delete feature-payment
            

Shows 设置 upstream tracking, 标准推送/拉取 cycle, handling 推送 rejections 使用拉取 --rebase, safe force pushing 使用 --force-使用-lease, pushing 标签, and 删除 remote 分支. 拉取-之前-推送 discipline 是大多数重要 habit 到学习.

动手试一试

常见错误

错误写法

Force pushing 到 shared 分支如 主要 and erasing teammates' 提交

正确做法

从不 use --force on shared 分支. Use --force-使用-lease on 你的 own feature 分支仅. 如果你 需要到 rewrite shared history, 坐标使用你的 team 首先 and use git revert 相反的 force pushing.

错误写法

Forgetting 到拉取之前 pushing and 获取 rejection 错误

正确做法

制作 '拉取之前推送' habit. 运行 git 拉取 (or git 拉取 --rebase) 之前每个 git 推送当 collaborating. 这个 integrates remote 更改首先 and avoids rejection 错误.

相关代码片段

相关速查表

练习 git 推送 & git 拉取

8 个练习

练习 git 推送 & git 拉取

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误