1. 本地分支跟踪远程分支(设置本地分支的上游分支):
git branch --set-upstream-to=origin/dev dev2 // 本地 dev2 分支关联远程dev分支。
2. 没有跟踪远程分支的本地分支无法push到远程仓库(push 是以分支为操作单位的?):
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:dev2 o [12:26:45] C:1
$ git checkout -b test
Switched to a new branch 'test'
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:test o [12:26:50]
$ touch testFile
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:test x [12:27:29]
$ git add testFile
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:test x [12:27:32]
$ git commit -m 'add testFile'
[test e258220] add testFile
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 testFile
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:test o [12:27:38]
$ git push
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin test
3. git pull可以直接在本地创建分支并跟踪同名的远程分支:
// 远程添加dev分支后
$ git pull
From xxx.xxx.xxx:xxxx/originRep
* [new branch] dev -> origin/dev // 本地新增的dev分支并跟踪了origin/dev分支
Already up to date.
4. 多个本地分支的上游分支可以是同一个远程分支:
本地新增dev2分支,可以关联远程dev分支
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:dev2 o [12:08:01] C:1
$ git branch --set-upstream-to=origin/dev dev2 // 设置本地dev2分支的上游分支为origin/dev
Branch 'dev2' set up to track remote branch 'dev' from 'origin'.
在div2分支上直接运行 git push会有如下提示:
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:dev2 o [12:08:29]
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:dev
To push to the branch of the same name on the remote, use
git push origin HEAD
To choose either option permanently, see push.default in 'git help config'.
由于dev2与上游分支dev的名字不一样,直接运行git push,git 会使用默认的dev2名字进行push ,导致上游分支与当前分支不匹配的为,所以需要声明上游分支的名字:git push origin HEAD:dev
# apple @ LTMac in ~/Documents/test_workspace_lt/UIelement on git:dev2 o [12:08:46] C:128
$ git push origin HEAD:dev
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git.oschina.net:liong/UIelement.git
38a235d..b5b93b8 HEAD -> dev
网友评论