如何在git中更新分支?

如何在git中更新分支?,git,Git,(1) 我在Github中分叉一个人的回购,让我们将此人的回购称为remoteRepo,将我的Github的回购称为myRepo。 (2) 我把它克隆到本地电脑上。 我用这个命令, $git clone [remoteRepo] -b [branch_name] /my/local/folder 现在,remoteRepo已经改变了。我将更新我的本地文件,以便与他的源代码保持一致 我确实喜欢这个 $ git remote add upstream [remoteRepo] $ git fetc

(1) 我在Github中分叉一个人的回购,让我们将此人的回购称为remoteRepo,将我的Github的回购称为myRepo。 (2) 我把它克隆到本地电脑上。 我用这个命令,

$git clone [remoteRepo] -b [branch_name] /my/local/folder
现在,remoteRepo已经改变了。我将更新我的本地文件,以便与他的源代码保持一致

我确实喜欢这个

$ git remote add upstream [remoteRepo]
$ git fetch upstream
$ git fetch upstream 
$ git merge upstream/[branch_name]
但它不起作用。没有更新,原因是什么? 我从一开始就遵循文件

您尚未进行任何本地更改。您已经掌握了远程回购的最新信息,因此无需其他操作


您尚未进行任何本地更改。您已经掌握了远程回购的最新信息,因此没有其他事情可做。

我不太理解您的问题。如果要更改远程存储库的位置,可以进入.git文件夹,打开配置文件并更改远程存储库

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@remote.com:folder/git_location.git
还有一些命令可以从命令行更改遥控器。
我不太明白你的问题。如果要更改远程存储库的位置,可以进入.git文件夹,打开配置文件并更改远程存储库

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@remote.com:folder/git_location.git
还有一些命令可以从命令行更改遥控器。
你怎么知道它不起作用?请尝试“git log”确认自动合并提交消息,并尝试“git diff HEAD^”查看合并的操作。此外,您还可以将原始合并稍微简化为“git pull upstream myBranch”

[编辑]

它对我有用。下面是完整的日志。要诊断您的问题-当您执行“git fetch”时,您应该看到一些确认该提取的内容。在“远程添加”之后,“git branch-a”是否列出了远程分支?您还可以运行“git remote show upstream”以确认您的存储库已“连接”到另一个存储库。您正在从克隆的存储库中提取;其他人正在从中抽离;他们把工作又推回去了吗?否则,您将看不到它们的更改

日志:

$ git clone dev1 -b br1 dev2
Cloning into 'dev2'...
done.
$ cd dev2
$ git remote add upstream ../dev1
$ git branch -a
* br1
  remotes/origin/HEAD -> origin/master
  remotes/origin/br1
  remotes/origin/master

# NOW change content in the 'dev1' (the remote repository)
$ cd ../dev1
$ git checkout br1
Switched to branch 'br1'
$ ls
bar.c   foo.c

$ FL='baz.c'; echo "stuff" > $FL; git add $FL; git commit -m "$FL"
[br1 ef45921] baz.c
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 baz.c
$ ls
bar.c   baz.c   foo.c

 # Now return to 'dev2' and 'pull'

$ cd ../dev2
$ ls
bar.c   foo.c

$ git pull upstream
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From ../dev1
 * [new branch]      br1        -> upstream/br1
 * [new branch]      master     -> upstream/master
You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
$ git pull upstream br1
From ../dev1
 * branch            br1        -> FETCH_HEAD
Updating 207c1a2..ef45921
Fast-forward
 baz.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 baz.c

 # Confirm 'baz.c' exists on 'dev2'
$ ls
bar.c   baz.c   foo.c

你怎么知道它不起作用?请尝试“git log”确认自动合并提交消息,并尝试“git diff HEAD^”查看合并的操作。此外,您还可以将原始合并稍微简化为“git pull upstream myBranch”

[编辑]

它对我有用。下面是完整的日志。要诊断您的问题-当您执行“git fetch”时,您应该看到一些确认该提取的内容。在“远程添加”之后,“git branch-a”是否列出了远程分支?您还可以运行“git remote show upstream”以确认您的存储库已“连接”到另一个存储库。您正在从克隆的存储库中提取;其他人正在从中抽离;他们把工作又推回去了吗?否则,您将看不到它们的更改

日志:

$ git clone dev1 -b br1 dev2
Cloning into 'dev2'...
done.
$ cd dev2
$ git remote add upstream ../dev1
$ git branch -a
* br1
  remotes/origin/HEAD -> origin/master
  remotes/origin/br1
  remotes/origin/master

# NOW change content in the 'dev1' (the remote repository)
$ cd ../dev1
$ git checkout br1
Switched to branch 'br1'
$ ls
bar.c   foo.c

$ FL='baz.c'; echo "stuff" > $FL; git add $FL; git commit -m "$FL"
[br1 ef45921] baz.c
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 baz.c
$ ls
bar.c   baz.c   foo.c

 # Now return to 'dev2' and 'pull'

$ cd ../dev2
$ ls
bar.c   foo.c

$ git pull upstream
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From ../dev1
 * [new branch]      br1        -> upstream/br1
 * [new branch]      master     -> upstream/master
You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
$ git pull upstream br1
From ../dev1
 * branch            br1        -> FETCH_HEAD
Updating 207c1a2..ef45921
Fast-forward
 baz.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 baz.c

 # Confirm 'baz.c' exists on 'dev2'
$ ls
bar.c   baz.c   foo.c

查看带有
git分支的分支列表

使用git checkout签出要合并更改的分支

使用
git merge


然后按下更改按钮查看带有git branch的分支列表

使用git checkout签出要合并更改的分支

使用
git merge


并推送更改

remoteRepo]已更改,因此我还想更新我的repo。当您键入“git fetch upstream”并随后合并时,您已经完成了该操作。你的回购只是“快进”到上游回购。考虑到你上面给出的步骤,这不应该发生。你需要更具体地说明你做了什么。从一个新的回购开始,看看是否可以复制它。[remoteRepo]已更改,因此我也想更新我的回购。当您键入“git fetch upstream”并随后进行合并时,您已经完成了该操作。你的回购只是“快进”到上游回购。考虑到你上面给出的步骤,这不应该发生。你需要更具体地说明你做了什么。从一个新的回购开始,看看是否可以复制它。实际上,我分叉了[remoteRepo],并且由于它发生了更改,我想与remoteRepo同步。实际上,我分叉了[remoteRepo],并且由于它发生了更改,我想与remoteRepo同步。是的,我做到了。我只有一个分支。我做了git签出,git合并上游/[myBranch]。仍然没有更新。谢谢,我忘了什么,请尝试“git branch-a”查看所有本地和远程分支,您需要将名为“remote/branch\u name”的删除分支与本地分支“branch\u name”合并。只使用“git pull”也可以做到,它是一个“git fetch”和“git merge”的组合。是的,我做到了。我只有一个分支。我做了git签出,git合并上游/[myBranch]。仍然没有更新。谢谢,我忘了什么,请尝试“git branch-a”查看所有本地和远程分支,您需要将名为“remote/branch\u name”的删除分支与本地分支“branch\u name”合并。仅仅使用“git pull”也可以做到这一点,它是一个“git fetch”和“git merge”的组合。因为我在[remoteRepo]中检查了一个修改过的文件,它改变了,但我在github中的repo没有改变,我的本地repo中的文件也没有改变。我不知道你有什么存储库,哪个repo从哪个存储库中获取,以及什么repo发生了变化。你能澄清一下吗?[从你上面的评论来看,听起来你有一个本地repo、一个远程repo和一个github repo.]因为我在[remoteRepo]中检查了一个修改过的文件,它变了,但我在github中的repo没有变,我在本地repo中的文件也没有变。我不知道你有什么存储库,哪个repo从哪个存储库获取,回购协议的内容也发生了变化。你能澄清一下吗?[从您的上述评论中,听起来您有本地回购、远程回购和github回购。]您有没有收到任何迹象表明有任何东西来自上游?它应该是这样的:
37b8a40..60d5d8f branch_name->上游/branch_name
如果你没有得到这样的东西,没有东西掉下来,因此没有东西可以合并。重新开始