20210114关于本地git与远程仓库关联的创建与取消(8)分支的合并 - ziyouzy/2021blog GitHub Wiki
当本地执行了git init和git commit -m“”后会在文件夹内存在master分支
这里要注意个细节,第一次commit是个无中生有的过程,如果你想再去创建新分支,只能是基于此master分支branch出一个子分支
或者从远程关联仓库fetch一个分支到本地
于是可以这样理解,一个git文件夹只能存在一个分支,而如上两种方式所的的分支用“子分支”来概括其实并不贴切,而应该说是“非主分支”
同时,主分支是不能撤销的,比如把“主分支”这个“身份”移交给某个“非主分支”
回到整体,我想实现的终归还是远程主分支与本地主分支的数据同步
同时基于当前的深入学习,以及git自身的使用规则,我想出了如下步骤:
1.远程仓库创建主分支,本地仓库也创建主分支
2.本地拉取远程的主分支
3.本地主分支与拉来的远程非主分支合并
4.本地主分支再去push远程主分支
但是有个问题是,比如readme文档,怎么说呢
即使再github上这四部都顺利完成了,那你怎么去同步gitee呢?
github和gitee的readme文档肯定是不同的啊,也就代表了这种模式一个项目必然会存在两个根
这并不可取啊!
于是我又尝试了这条指令
git push origin2 main
首先说说各个字段的含义,origin2不用多说,代表了远程仓库的别名,main则代表了这个远程仓库的main分支
而本地分支名其实是被省略了,默认会上传当前本地的分支
指令反馈结果依然是这个错误:
zqy@ubuntu:~/WorkShop/testGit/githubtest1$ git push origin2 main
To [email protected]:ziyouzy/test1.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '[email protected]:ziyouzy/test1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
也就是说,还是识别到了两个“根”,这样是无法实现同步的
而进行git push origin2 master获得了成功,因为master分支本身就是前期push过去的,新增加的pushtest.txt顺利上传到了远程分支
也就是说,他识别到了两个分支拥有相同的根,所以可以实现同步
于是我在本地再次创建第三个分支,把这个分支再次上传到origin2 pushtest2分支,看看效果:
git branch pushtest2
git checkout pushtest2
git push origin2
语句执行后:
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 302 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'pushorcreatebranch2' on GitHub by visiting:
remote: https://github.com/ziyouzy/test1/pull/new/pushorcreatebranch2
remote:
To [email protected]:ziyouzy/test1.git
* [new branch] pushorcreatebranch2 -> pushorcreatebranch2
可以看到,没有指定远程分支的名称,所以在远程创建了与本地名称相同的新分支
之后本地的pushorcreatebranch2递交了个commit,再次执行git push origin2
所得效果是更新了刚刚在远程创建的 pushorcreatebranch2
之后再去尝试git push origin2 push333,也就是说给他个远程仓库不存在的分支名字:
zqy@ubuntu:~/WorkShop/testGit/githubtest1$ git push origin2 push333
error: src refspec push333 does not match any.
error: failed to push some refs to '[email protected]:ziyouzy/test1.git'
然而却报错了
虽说还是感觉git有很多硬性的规矩,但是也是觉得可能是因为我对他设计的理念还不是很懂,所以才会觉得毫无规律可言
总之,现在的知识点似乎也是够用了