1

我在我的 github 帐户中创建了一个 repo。

然后我做了

git remote add upstream git@github.com:********/*********.git

现在我有upstream遥控器 - 原版和origin- 我的叉子。

我的master分支正在跟踪origin/master

我永远不会推送到origin/master,因为我要做的所有拉取请求都来自主题分支。

同时,我会经常从中提取最新的更改upstream/master以从中创建新的主题分支。

所以我想以这样的方式配置git:

  • 我的本地分支机构master正在跟踪upstream/master
  • 当我在master分支中并且我这样做git push时,这将失败,因为upstream它是只读的。
  • 当我在master分支中并且我这样做git pull时,我将从中提取最新的更改,upstream/master并且我将立即能够git checkout -b my-new-topic-branch从最新的分支中进行操作upstream/master

对工作流程有任何意见吗?如何为此设置 git?

master我做了:

git branch -u upstream/master

但是当我切换到一个主题分支时:

git remote show origin
...
  Local refs configured for 'git push':
    master  pushes to master  (fast-forwardable)
...

更新:

解决方案:

(c8cc3a6) » git branch -d master                                   
Deleted branch master (was c8cc3a6).

(c8cc3a6) » git branch --track master upstream/master
Branch master set up to track remote branch master from upstream.

(c8cc3a6) » git remote show origin
...
  Local refs configured for 'git push':
    master                       pushes to master                       (fast-forwardable)
...

(c8cc3a6) » git remote show upstream
...
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

(c8cc3a6) » git checkout master
Switched to branch 'master'

(master) » git pull
Already up-to-date.

(master) » git push
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

(master) » git checkout -b test-branch
Switched to a new branch 'test-branch'

(test-branch) » git push
...
To git@github.com:********/*******.git
 * [new branch]      HEAD -> test-branch

或者更容易理解的方式:

(test-branch) » git branch -d master
Deleted branch master (was c8cc3a6).

(test-branch) » git checkout -b master upstream/master  
Branch master set up to track remote branch master from upstream.
Switched to a new branch 'master'
4

1 回答 1

1

在您.git/config的本地存储库中,使该[branch "master"]部分如下所示:

[branch "master"]
remote = upstream
merge = refs/heads/master

这将处理项目 1 和 3(跟踪和拉取配置)。我不确定有没有办法完美地完成第 2 项,但您可能会发现,由于upstream不是您的存储库,它会拒绝您的推送(除非该存储库的所有者允许随机贡献者推送,我我不确定是否可以在github上完成...)。

于 2013-06-05T14:25:53.020 回答