6

试图使用这里的东西,但这并不能解决我的问题。

我在 git 中有一个本地仓库,从远程仓库克隆,development. 我在本地分支以使用名为的分支中的新功能newBranchName并调用git push origin newBranchName以在远程存储库上设置新分支。

现在,当我尝试推送时,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远程推送,但想remoteTrackedByOldBranch在我输入时停止推送git push。我不想删除任何分支——似乎许多类似问题的答案都建议删除而不是取消跟踪。我也不想知道如何仅通过在 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
4

2 回答 2

15

只推你所在的分支

默认情况下,每当您发出不带参数的 git push 时,git 都会推送您的所有跟踪分支。让它推你所在的分支

git config push.default upstream # git 1.7.10
git config push.default tracking # older vesions use "tracking" instead of "upstream"

如果您希望 git 始终像这样工作 - 您可以使用 --global 开关或简单地编辑您的~/.gitconfig文件以适应。

停止跟踪远程分支

git 的配置只是一个文件。调查和/或修复 git 配置的最简单方法通常是打开 .git/config 文件并进行编辑

例如,如果您的 .git/config 文件中有这样的部分:

[branch "develop"]
    remote = origin
    merge = refs/heads/develop
    rebase = true

你做到了:

[branch "develop"]
    rebase = true

您的分支不再跟踪任何内容。您也可以只删除整个部分,git 会执行相同的操作。

于 2012-05-15T16:50:30.240 回答
3

与远程分支的跟踪关系在与特定分支关联的branch.<BRANCHNAME>.remote配置branch.<BRANCHNAME>.merge项中维护。您可以像这样查看当前配置:

git config --get-regexp 'branch.remoteTRackedByOldBranch.*'

您可以像这样删除配置:

git config --remove-section branch.remoteTrackedByOldBranch

[请注意,这将删除与此分支关联的所有配置,但除了remoteandmerge设置之外不太可能有任何其他配置。您显然可以通过手动编辑.git/config文件来完成同样的事情。]

在此更改之后,git push不应再尝试将该分支推送到远程。

于 2012-05-15T16:43:23.137 回答