了解git:connect branch到远程存储库

top*_*kip 5 git

我在github上有一个存储库,比如testrepo.现在我想建立一个本地存储库repo,它有一个分支origin-master,我希望能够从存储库编辑东西.

repo/origin-master  <-------->  origin/master
Run Code Online (Sandbox Code Playgroud)

克隆工作正常:

mkdir repo && cd repo && git init 
# not necessary of course:
echo "master" > master && git add master && git ci -m "master"
git remote add origin git@github.com:<username>/testrepo.git
git fetch origin
git branch --set-upstream origin-master origin/master 
git checkout origin-master
# create a new file:
echo "for origin-master" > orig-master && git add orig-master && git ci -m "orig-master"
Run Code Online (Sandbox Code Playgroud)

git push origin 
To git@github.com:<username>/testrepo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:<username>/testrepo.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

我怎么能告诉git如果我想推送到原点,我想将本地分支推origin-master送到origin/master

ell*_*eth 2

将默认推送行为设置为上游:

$ git config push.default upstream
$ git push origin
Run Code Online (Sandbox Code Playgroud)

git push origin与 相同git push origin :,默认情况下推送所有“匹配”分支。您的 origin-master 分支不匹配,因此它尝试获取匹配的分支( master) 并将其推送到 origin。

或者,您可以push在每个远程的基础上指定引用规范:

$ git config --add remote.origin.push origin-master:master
$ git push origin
Run Code Online (Sandbox Code Playgroud)

另请参阅git-push 示例git-config