尝试使用这里的方法,但对我没有用。
我在git中有一个本地repo,是从远程repo development
克隆而来。我在本地分支上创建了一个名为newBranchName
的新功能分支,并调用git push origin newBranchName
将其设置为远程repo上的新分支。
现在当我尝试推送时,git似乎把我的newBranchName
本地分支一并推到了以前分支所跟踪的所有内容上。我希望这种情况停止。
下面是一个更详细的例子,我将创建一个本地分支,添加一个文件,进行本地提交,然后推送到远程服务器的新分支。到目前为止还好。
Administrator@BOXEN /path/to/working/dir (oldBranch)
$ git branch testingStuff
Administrator@BOXEN /path/to/working/dir (oldBranch)
$ git checkout testingStuff
Switched to branch 'testingStuff'
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ vim test.txt
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ git add test.txt
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ git commit -a
[testingStuff 11468d8] Testing git; can trash this branch.
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ git push origin testingStuff
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://url/to/remote/repo.git
* [new branch] testingStuff -> testingStuff
现在,我将编辑test.txt文件,提交更改并推送。这就是让我困惑的地方。
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ vim test.txt
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ git commit -a
[testingStuff 2be7063] more testing git
1 files changed, 1 insertions(+), 0 deletions(-)
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://url/to/remote/repo.git
11468d8..2be7063 testingStuff -> testingStuff
! [rejected] oldBranch -> remoteTrackedByOldBranch (non-fast-forward)
error: failed to push some refs to 'http://url/to/remote/repo.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.
我希望你能够帮助我进行远程推送到
testingStuff
,但是在我输入 git push
命令时停止向 remoteTrackedByOldBranch
推送,而不想删除任何分支。看起来许多类似问题的回答都建议删除而不是取消追踪。我也不想通过在 git push
命令中显式命名特定分支的方式来推送到特定分支,因为这样会导致过多的肌肉记忆错误。我只想将 git push
推送到 origin/testingStuff
。我已经不太聪明地修改了我的 .git/config 文件,但它仍然会推送到
remoteTrackedByOldBranch
。
编辑: 这是我进行上述操作后 .git/config 文件的内容:[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://url/to/remote/repo.git
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "oldBranch"]
remote = origin
merge = refs/heads/oldBranch
关于testingStuff
分支的信息并没有在其中。
编辑:git branch -avv
输出:
Administrator@BOXEN /path/to/working/dir (testingStuff)
$ git branch -avv
master 721aa61 initial setup
projectFork1 e132f5f Fixed Construction grid labels getting sliced.
projectFork2 1d20317 initial load
oldBranch 1d20317 initial load
* testingStuff 192f622 Still testing
remotes/origin/HEAD -> origin/master
remotes/origin/empty ec1c694 initial setup
remotes/origin/joeUserFork1 771f43e Initial Load
remotes/origin/master 721aa61 initial setup
remotes/origin/projectFork1 e132f5f Fixed Construction grid labels getting sliced.
remotes/origin/oldBranch 1d20317 initial load
remotes/origin/joeUserFork2 dc605e8 What was sent initially.
remotes/origin/testingStuff 192f622 Still testing
remotes/origin/upload_master 0d8c440 Initial Load
[branch master]
,以及(在上述情况中)[branch oldBranch]
,但是没有testingStuff
,我的新分支。我假设你的develop
应该代表我上面场景中的testingStuff
。我没有在那里设置rebase=true
,但我想这只是适用于你的工作流程。我会将我的配置粘贴到我的问题中。 - ruffin$ git config set push.default upstream
出现了错误:error: key does not contain a section: set
。我会在谷歌上搜索一下。 - ruffingit config push.default upstream
,然后执行git push
会出现错误,接着执行git push --set-upstream origin testingStuff
来显式地设置上游分支,然后再次执行git push
就可以像我想要的那样工作了。不过我仍然希望能让 larsks 的答案起作用。 - ruffin