如果设置了不同的上游,如何 Git 从 master 拉取开发分支?

How to Git pull from master on a dev branch if you had set a different upstream?

我一开始是在master分支。然后,我创建了一个新分支 newBranch 并切换到它。然后我提交了更改并推送。

现在,我的问题是如何从 master 提取更改。我做了 git fetch,这似乎工作正常,但是当我做了 git status 时,它只显示了我的 newBranch 的变化。我希望能够将更改从 master 拉入 newBranch

这是我在终端中输入的命令:

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master


$ git fetch

$ git pull

$ git switch -b newBranch

$ git branch -a

  master
* newBranch
  remotes/origin/HEAD -> origin/master 


$ git add *

$ git commit -m "First commit"

$ git push --set-upstream origin newBranch

$ git fetch origin master
From git.domain.com:dev-folder
 * branch            master     -> FETCH_HEAD


$ git status   # This command doesn't show changes from master ???
On branch newBranch
Your branch is up to date with 'origin/newBranch'.

git fetch command downloads the commits (and their associated objects) from a remote repository into your own, but it doesn't merge them into the current branch. That's what the git pull 命令用于:

Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

在您的情况下,您可以将来自 origin/master 的提交合并到您的本地 newBranch 分支中:

git pull origin master

相当于 运行 git fetch origin 后跟 git merge origin/master.

如果你更喜欢 rebase 你的本地提交在 origin/master 之上,你可以告诉 git pull 做一个 rebase 而不是合并:

git pull --rebase origin master

这又等同于 运行 git fetch origingit rebase origin/master.