如何在远程分支上获取最新信息?

Pin*_*aas 3 git git-branch

我试图从远程github分支中获取最新代码.假设这个分支名为myDevBranch,我该如何在本地获取?我试过了:

git fetch myDevBranch 
Run Code Online (Sandbox Code Playgroud)

返回:

fatal: 'myDevBranch' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

看起来像一个安全问题,还是有另一种方式在本地获取远程分支?

Moh*_*ady 12

你没有取一个分支,你fetch是一个遥控器,所以正确的线路将是

git fetch origin # or whatever your remote is called
Run Code Online (Sandbox Code Playgroud)

然后更新所有跟踪分支,更新的代码将成为一个名为的分支origin/myDevBranch,再次将您的上游名称替换为origin

要更新本地分支,您可以合并上游,git merge origin/myDevBranch但您需要确保您HEAD指向此远程的本地分支(也称为myDevBranch),

或者您可以结帐git checkout origin/myDevBranch但但这会使您处于分离的头模式,您可以使用该远程创建一个新的本地分支git checkout -b

如果您HEAD正指向您当前分支,那么你可以做一个git pull,请记住,pull将两者都做fetchmerge,如果你的分支已经发散为任何原因,你会得到一个conflict,你将需要你自己来解决.

如果你需要rebase那么你可以做一本手册fetch,rebase或者你可以做一个git pull --rebase

  • 您还可以使用“git pull”来获取计算机中远程分支中的任何内容。它将远程存储库中的更改合并到当前分支中。在默认模式下,`git pull`是`git fetch`的简写,后跟 git `merge FETCH_HEAD`。_来源:[https://git-scm.com/docs/git-pull] (3认同)