2012-05-15 59 views
5

我在github上有一个仓库,比如testrepo。现在我想设置一个本地存储库repo,它有一个分支origin-master,我希望能够从存储库编辑东西。了解git:连接分支到远程存储库

repo/origin-master <--------> origin/master 

克隆正常工作:

mkdir repo && cd repo && git init 
# not necessary of course: 
echo "master" > master && git add master && git ci -m "master" 
git remote add origin [email protected]:<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" 

git push origin 
To [email protected]:<username>/testrepo.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to '[email protected]:<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. 

我怎么能告诉混帐,如果我要推到原点,我想当地分支机构origin-masterorigin/master

+0

显示不起作用的顺序,不是那个顺序。 – jthill

回答

2

设置默认行为推到上游:

$ git config push.default upstream 
$ git push origin 

git push origin相同git push origin :,默认情况下全押 “匹配” 的分支。您的原始主分支不匹配,因此它会尝试将的分支匹配(master)并将其推送到原点。

或者,你可以在每个远程基础上指定push refspecs:

$ git config --add remote.origin.push origin-master:master 
$ git push origin 

又见git-push examplesgit-config

4

请参阅this post以获得推荐给非裸露(即带有工作副本)回购的良好建议。基本上这里发生的事情是,当你按这种方式推送时,远程回购并不会更新它的工作副本(真实文件),而只是它的历史记录。然后你需要git reset --hard来刷新这些文件,但这很危险,因为你会丢失未提交的修改。

作为一般建议,我会说在处理多个非裸存储库时更愿意使用拉推:push to only bare repositories

+0

好的,这是很好的建议,我会这样做。 – topskip

相关问题