为什么我在Git仓库中推送新分支时会遇到问题?

5
作为我在关于svn转git迁移中无法使用的分支的问题的后续,我遇到了另一个问题:我无法将新分支推送到我的中央Git存储库。
$ git clone ssh://server/opt/git/our_app.git
$ cd our_app
$ git branch my-test-branch
$ git checkout my-test-branch
$ echo test > test.txt
$ git add test.txt
$ git commit test.txt -m "test commit"
[master ed81ec0] test commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
$ git push
Everything up-to-date

所以,这不会将我的分支推送到服务器。一位同事建议我查看我的.git/config文件,它看起来像这样:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://server/opt/git/our_app.git
[branch "master"]
    remote = origin
    merge = refs/heads/master

我被建议手动添加一个推送条目:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://server/opt/git/our_app.git
    push = refs/heads/*:refs/heads/*

现在情况看起来更好了:
$ git push
Password: 
Counting objects: 1, done.
Delta compression using up to 1 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (1/1), 5 bytes, done.
Total 1 (delta 1), reused 0 (delta 0)
To ssh://server/opt/git/our_app.git
 * [new branch]      my-test-branch -> my-test-branch

虽然这样做可以实现目的,但仍感觉像是一种hack。有没有更合适的方法?

3
git push 的默认行为是将与远程端现有目的地匹配的分支推送到远程。新分支没有这样的匹配目的地。您可以使用 push.default 配置变量更改默认行为。 - Amber
3个回答

4

要推送一个新分支,请使用git push origin my-test-branch


4

如果您只想推送与当前分支名称相同的分支,则只需使用以下命令:

git push origin my-test-branch

如果您希望您的更改出现在源存储库的主分支上:

git push origin my-test-branch:master

4
您可以使用以下命令将分支推送到远程仓库: git push origin my-test-branch 然后,您可以使用以下命令查看所有远程分支: git branch -r 这里有一个介绍如何创建/删除远程分支的链接: 传送门

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接