Git添加远程和推送到

Git添加远程和推送到,git,Git,我无法从的克隆更新分叉裸Git存储库的分支 原始的裸Git存储库 创建一个裸Git存储库repo0.Git,并在repo0中克隆它: ROOT=$PWD git init --bare repo0.git git clone repo0.git 工作repo0,然后推到repo0.git: cd $ROOT/repo0 echo 'First line' > foo git add foo git commit -m 'foo: First line' git push origin m

我无法从的克隆更新分叉裸Git存储库的分支 原始的裸Git存储库

创建一个裸Git存储库
repo0.Git
,并在
repo0
中克隆它:

ROOT=$PWD
git init --bare repo0.git
git clone repo0.git
工作
repo0
,然后推到
repo0.git

cd $ROOT/repo0
echo 'First line' > foo
git add foo
git commit -m 'foo: First line'
git push origin master
cd $ROOT
git clone --bare repo0.git repo1.git
git clone repo1.git
cd $ROOT/repo1
echo 'Second line' >> foo
git add foo
git commit -m 'foo: Second line'
git push origin master
git push repo1 repo1-master

 # output:
...
To (...)/repo1.git
* [new branch]      repo1-master -> repo1-master
Fork
repo0.git
repo1.git

cd $ROOT/repo0
echo 'First line' > foo
git add foo
git commit -m 'foo: First line'
git push origin master
cd $ROOT
git clone --bare repo0.git repo1.git
git clone repo1.git
cd $ROOT/repo1
echo 'Second line' >> foo
git add foo
git commit -m 'foo: Second line'
git push origin master
git push repo1 repo1-master

 # output:
...
To (...)/repo1.git
* [new branch]      repo1-master -> repo1-master
repo1
中工作,并推送到
repo1.git

cd $ROOT/repo0
echo 'First line' > foo
git add foo
git commit -m 'foo: First line'
git push origin master
cd $ROOT
git clone --bare repo0.git repo1.git
git clone repo1.git
cd $ROOT/repo1
echo 'Second line' >> foo
git add foo
git commit -m 'foo: Second line'
git push origin master
git push repo1 repo1-master

 # output:
...
To (...)/repo1.git
* [new branch]      repo1-master -> repo1-master
repo0
中,添加
remo1.git
并获取它:

cd $ROOT/repo0
git remote add repo1 $ROOT/repo1.git
git fetch repo1
创建跟踪分支
repo1 master
到远程跟踪分支
repo1/master

git checkout -b repo1-master repo1/master

# output:
Branch repo1-master set up to track remote branch master from repo1.
Switched to a new branch 'repo1-master'
git branch -a -vv

# output:
  master                107e940 [origin/master] foo: First line
* repo1-master          801d0ca [repo1/master] foo: Second line
  remotes/origin/master 107e940 foo: First line
  remotes/repo1/master  801d0ca foo: Second line
git branch -a -vv

# output:
  master                107e940 foo: First line
* repo1-master          126d61c [ahead 1] foo: Third line
  remotes/origin/master 107e940 foo: First line
  remotes/repo1/master  801d0ca foo: Second line
确认
repo1 master
是到的跟踪分支 远程跟踪分支
repo1/master

git checkout -b repo1-master repo1/master

# output:
Branch repo1-master set up to track remote branch master from repo1.
Switched to a new branch 'repo1-master'
git branch -a -vv

# output:
  master                107e940 [origin/master] foo: First line
* repo1-master          801d0ca [repo1/master] foo: Second line
  remotes/origin/master 107e940 foo: First line
  remotes/repo1/master  801d0ca foo: Second line
git branch -a -vv

# output:
  master                107e940 foo: First line
* repo1-master          126d61c [ahead 1] foo: Third line
  remotes/origin/master 107e940 foo: First line
  remotes/repo1/master  801d0ca foo: Second line
repo1主分支上工作:

echo 'Third line' >> foo
git add foo
git commit -m 'foo: Third line'
确认
repo1 master
仍然是到的跟踪分支 远程跟踪分支
repo1/master

git checkout -b repo1-master repo1/master

# output:
Branch repo1-master set up to track remote branch master from repo1.
Switched to a new branch 'repo1-master'
git branch -a -vv

# output:
  master                107e940 [origin/master] foo: First line
* repo1-master          801d0ca [repo1/master] foo: Second line
  remotes/origin/master 107e940 foo: First line
  remotes/repo1/master  801d0ca foo: Second line
git branch -a -vv

# output:
  master                107e940 foo: First line
* repo1-master          126d61c [ahead 1] foo: Third line
  remotes/origin/master 107e940 foo: First line
  remotes/repo1/master  801d0ca foo: Second line
推送至repo1.git

cd $ROOT/repo0
echo 'First line' > foo
git add foo
git commit -m 'foo: First line'
git push origin master
cd $ROOT
git clone --bare repo0.git repo1.git
git clone repo1.git
cd $ROOT/repo1
echo 'Second line' >> foo
git add foo
git commit -m 'foo: Second line'
git push origin master
git push repo1 repo1-master

 # output:
...
To (...)/repo1.git
* [new branch]      repo1-master -> repo1-master
推送操作会在
repo1.git
上创建一个新的分支
repo1 master
,而不是 如我所愿,在
repo1
上更新
master
分支


我做错了什么?

我想您需要设置
push.default

git config push.default upstream
从git文档:

default定义了在不使用refspec的情况下git push应该采取的操作 明确给出。不同的值非常适合特定的情况 工作流程;例如,在纯粹的中心工作流中(即获取 源等于推送目的地),上游可能是什么 你想要的。可能的值为:

nothing(无)-除非选择了refspec,否则不要推出任何内容(错误) 明确给出。这主要是为了那些想要避免的人 总是直言不讳地犯错误

当前-推送当前分支以更新具有相同属性的分支 接收端的名称。在中央和非中央工作 工作流程

上游-将当前分支推回其更改所在的分支 通常集成到当前分支(称为 @{上游})。此模式只有在您按下按钮时才有意义 与您通常从中提取的存储库相同(即中央工作流)

简单-在集中式工作流中,使用添加的 如果上游分支机构的名称不同,则拒绝推送的安全性 从本地的那个

当按下与您使用的遥控器不同的遥控器时 正常情况下,以电流的形式工作。这是最安全的选择,也是最好的选择 适合初学者

此模式将成为Git2.0中的默认模式

匹配-推送两端具有相同名称的所有分支。这 使您正在推送的存储库记住分支集 这将被推出(例如,如果您总是推动maint和master 没有其他分支,您推送到的存储库将拥有 这两个分支,以及您的本地maint和master将被推送 那里)


要将分支推送到其配置的跟踪分支,只需键入

git push

git-push
实际上是git-push:的快捷方式:

git-push-repo1
仍然有效
git-push
被拒绝:为了防止丢失历史记录,拒绝了非快进更新,在再次推之前合并远程更改。有关详细信息,请参阅“git push--help”中的“关于快进的说明”部分。
git push repo
也被拒绝,相同的错误消息在我的示例中有一个错误(现已修复),仍然有一条错误消息,但告诉我更改
push.default
,正如e4rthdog建议的那样,似乎工作得很好,谢谢!我正在阅读git文档,感谢您提供它。现在我将使用
git config push.default nothing
避免错误,并使用
git push repo1 repo1 master:master