问题
If you create a local repository by:
git init
Then how to set a default remote for git push
so if you create a new branch and want to git push
its commits you don't need to specify the remote destination?
Take into account that the local repository is not cloned form a remote and just initialized so it does not have any commits to be pushed nor branches.
so far I only managed to set the remote for the master because it is the default name for the first branch but this does not solve the problem because other branches that will be created have at this point unknown names.
Is there any config entry you can use to set default push destination for branches in this case?
回答1:
You can set push.default
to current like this:
git config push.default current
It's described in man git-config
:
push.default
Defines the action git push should take if no refspec is
explicitly given. Different values are well-suited for
specific workflows; for instance, in a purely central
workflow (i.e. the fetch source is equal to the push
destination), upstream is probably what you want. Possible
values are:
(...)
current - push the current branch to update a branch with the same
name on the receiving end. Works in both central and non-central
workflows.
To check how it works in practice first add a remote repository you will push to after creating a new local repository, it can even reside on the same filesystem:
$ git remote add origin <REMOTE_REPO_ADDRESS>
and then try git push
- first with master
branch and then with dev
:
$ touch a
$ git add a
$ git commit -minitial
[master (root-commit) c89b8e4] initial
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /tmp/git-init-remote-
* [new branch] master -> master
$ git checkout -b dev
Switched to a new branch 'dev'
$ touch b
$ git add b
$ git commit -mdev
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To /tmp/git-init-remote-
* [new branch] dev -> dev
来源:https://stackoverflow.com/questions/52682174/git-config-default-push-destination-for-new-branches