push.default配置-Git详解(8)

可以省略refspec参数,修改 .git/default文件,进行配置。
包含五个模式:

  • nothing
    类似严格模式,push 的时候要跟上完整的 refspec
  • current
  • upstream
    本地分支与远程分支不同名时,推送到远程分支。
  • simple
    默认的模式,只推送同名分支,切严格
  • matching
    本地和远程分支同名的,会推送

示例

重命名远程仓库名称

1
2
3
$ git clone https://git.coding.net/cn-dream/test01.git
$ cd test01
$ git remote rename origin upstream

配置使用simple模式

1
2
$ git config --system push.default
$ git config --system push.default simple

不同名分支推送

1
2
3
4
5
6
7
8
$ git push ghub feature:master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 317 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/monk8/test01.git
4a551a5..8c0abea feature -> master

config文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "upstream"]
url = https://git.coding.net/cn-dream/test01.git
fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "master"]
remote = upstream
merge = refs/heads/master
[remote "ghub"]
url = https://github.com/monk8/test01.git
fetch = +refs/heads/*:refs/remotes/ghub/*
[push]
default = simple
[branch "feature"]
remote = ghub
merge = refs/heads/master

给分支加上跟踪信息,pull时会提示

1
$ git branch --set-upstream-to=origin/v0 v0