目录
前言
默认阅读本文的读者都晓得了一定的git语法、会在目标文件夹下打开Git Bash编辑界面。心急的读者可以直接跳到总结部分,想看看我是怎么尝试、探索的过程的读者,不妨继续往下看,谢谢!
想法
看到过“一个仓管管理一个项目”的想法,这样得创建好多仓库,太麻烦;还看到一篇正经的讨论如何管理的文章:超详细图解说明:一个代码仓库如何管理多个项目,但是乍一看还是麻烦,以我小白的思维有点难以消化。
于是我改变思路:
- 首先,我在gitee上只是存放我一个人的代码,不和别人协作;
- 其次,我不清楚其他人是怎么理解一个项目应该怎么存的,反正我自己就定义一个仓库下每个第一级目录就是一个项目,项目要发展就在第一级目录下不断延伸就行;
- 最后,如果是团队协作开发项目,那一个项目必然不小(几个G甚至更大),文件夹和文件相当多,用一个仓库只管理一个这样大的项目好像也不为过。
尝试
本地创建文件夹Myprojects-open,初始化为本地仓库:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open
$ git init
Initialized empty Git repository in D:/Myprojects-open/.git/
配置身份信息,这个信息建议在gitee新建仓库后的“克隆/下载”选项的SSH块直接复制:
git config --global user.name "Your Name"git config --global user.email "[email protected]"
gitee创建仓库Myprojects-open,啥也没有:
在本地,将Myprojects-open(L表示本地)和Myprojects-open(R表示远程)连接,连接名为“Myprojects-open”(我后面连接名都为本地文件夹名):
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git remote add Myprojects-open [email protected]:He-Wenxuan-LZU/myprojects-open.git
我在本地仓库Myprojects-open(L)开发新项目了,项目为Test1,在Myprojects-open(L)创建新文件夹Test1,而且已经写了一个文件test1.txt,内容为“我是test1”,现在直接上传我写好的新项目Test1文件夹:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ ls
Test1/
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push -u Myprojects-open Test1
error: src refspec Test1 does not match any
error: failed to push some refs to 'gitee.com:He-Wenxuan-LZU/myprojects-open.git'
失败,说明不能直接上传文件夹,这点从push语法也提前看出(我是小白,试过才意识到)。那就把本地的master整个push过去:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push -u Myprojects-open master
error: src refspec master does not match any
error: failed to push some refs to 'gitee.com:He-Wenxuan-LZU/myprojects-open.git'
失败,前面新建项目、新建文件忘了add、commit,那就过一遍再push master:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"创建项目文件夹Test1"[master (root-commit) cf47760] 创建项目文件夹Test1
1file changed, 1 insertion(+)
create mode 100644 Test1/test1.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push -u Myprojects-open master
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
![rejected] master -> master (fetch first)
error: failed to push some refs to 'gitee.com:He-Wenxuan-LZU/myprojects-open.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards'in'git push --help'for details.
失败,好像是第一次(fetch first),还得先git pull:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git pull --rebase Myprojects-open master
From gitee.com:He-Wenxuan-LZU/myprojects-open
* branch master -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.
使用“rebase”是将远程仓库的数据获取到本地,但不是覆盖,所以Myprojects-open(L)中新项目Test1还在,还多了仓库有的许可证:
再次尝试push master:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push Myprojects-open master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 368 bytes |368.00 KiB/s, done.
Total 4(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
aed9897..9e06b96 master -> master
代码执行成功,gitee仓库Myprojects-open(R)出现了我的新项目Test1:
继续在本地Myprojects-open(L)开发新项目Test2,还是空的,直接push master:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ ls
LICENSE Test1/
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ mkdir Test2
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ ls
LICENSE Test1/ Test2/
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"创建新项目Test2,还是空的"
On branch master
nothing to commit, working tree clean
失败,说明git只能捕捉文件的变化,不能捕捉文件夹的变化。那就在文件夹Test2创建文件test2.txt,内容为“我是test2”。再次push master:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"先创建新项目Test2,后面补建文件test2.txt"[master cf85350] 先创建新项目Test2,后面补建文件test2.txt
1file changed, 1 insertion(+)
create mode 100644 Test2/test2.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push Myprojects-open master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 415 bytes |415.00 KiB/s, done.
Total 4(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
9e06b96..cf85350 master -> master
代码执行成功,查看gitee仓库Myprojects-open(R):
假设我在本地开发项目时,想起项目Test1少了一个文件,在本地文件夹Test1下新建test3.txt,内容为”我是test3“,再提交:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"在项目Test1补充文件test3.txt"[master 5f3e0aa] 在项目Test1补充文件test3.txt
1file changed, 1 insertion(+)
create mode 100644 Test1/test3.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push Myprojects-open master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 423 bytes |423.00 KiB/s, done.
Total 4(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
cf85350..5f3e0aa master -> master
代码执行成功,查看gitee仓库Myprojects-open(R):
又想给项目Test2拓展新板块,在本地文件Test2下创建新文件夹Test2_1,内有新文件test4.txt,内容为“我是test4”。再次上传更新:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"给项目Test2加新板块Test2_1,内有新文件test4.txt"[master 828cae7] 给项目Test2加新板块Test2_1,内有新文件test4.txt
1file changed, 1 insertion(+)
create mode 100644 Test2/Test2_1/test4.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push Myprojects-open master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 452 bytes |452.00 KiB/s, done.
Total 5(delta 1), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
5f3e0aa..828cae7 master -> master
代码执行成功,查看gitee仓库Myprojects-open(R):
接着考虑:最近一次本地和远程内容一致后,我在Myprojects-open(L)开发的项目又需要减少某些内容,然后将这个更新上传。比如,Myprojects-open(L)里项目Test1删掉test3.txt,Myprojects-open(R)也要删除,尝试同样的push master:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"删掉了项目Test1里的test3.txt"[master 7f63a2f] 删掉了项目Test1里的test3.txt
1file changed, 1 deletion(-)
delete mode 100644 Test1/test3.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push Myprojects-open master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 377 bytes |377.00 KiB/s, done.
Total 3(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
828cae7..7f63a2f master -> master
成功,查看Myprojects-open(R),可以看见远程仓库没有test3.txt:
如果只是更新文件内容,不改变文件的多少,比如我要修改test1.txt的内容为“我现在不是test1了,哈哈”,也和前面同样的操作:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"修改了项目Test1里文件test1.txt的内容"[master 5232271] 修改了项目Test1里文件test1.txt的内容
1file changed, 1 insertion(+), 1 deletion(-)
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git push Myprojects-open master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 430 bytes |430.00 KiB/s, done.
Total 4(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
7f63a2f..5232271 master -> master
当然每次更新都不一定要“add .”将全部都提交到暂存区(不会写删除的就add全部,反正不会错),比如我在Myprojects-open(L)更改项目Test1的test1.txt内容,然后更新到Myprojects-open(R):
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ cd Test1
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test1 (master)
$ gitadd test1.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test1 (master)
$ git commit -m"我更该了项目Test1里文件test1.txt的内容"[master 15f2ec8] 我更该了项目Test1里文件test1.txt的内容
1file changed, 1 insertion(+), 1 deletion(-)
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test1 (master)
$ git push Myprojects-open master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 436 bytes |436.00 KiB/s, done.
Total 4(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
b33bd09..15f2ec8 master -> master
成功,查看Myprojects-open(R):
以上都是“本地先更新,再将更新应用到远程,使远程和本地一致”的情况。
再考虑一种情况:我开发很多项目后,自己本地存不下了(这也是我为什么会思考要多个项目都存上远程的原因),上传完就删掉了,一段时间后我想下载以前的项目再看看。比如我把Myprojects-open(L)的项目Test1删掉了,如下图:
此时Myprojects-open(R)还有,我想下回来,使用pull,语句执行成功了,但是结果不是我们想要的,Test1文件夹并没有出现在本地:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git pull --rebase Myprojects-open master
From gitee.com:He-Wenxuan-LZU/myprojects-open
* branch master -> FETCH_HEAD
Current branch master is up to date.
换个pull方式也不行:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git pull Myprojects-open master
From gitee.com:He-Wenxuan-LZU/myprojects-open
* branch master -> FETCH_HEAD
Already up to date.
也就是说,我所知的pull方式都行不通了。
那就clone呗:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git clone [email protected]:He-Wenxuan-LZU/myprojects-open.git
Cloning into 'myprojects-open'...
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 26(delta 3), reused 0(delta 0), pack-reused 0
Receiving objects: 100% (26/26), done.
Resolving deltas: 100% (3/3), done.
不好的地方就是会把你不想重看的项目一并下载来。实在介意这个,就直接到gitee官网上的可视化界面操作下载就行;看到这里,觉得麻烦也可以在gitee官网上直接新建、上传不同项目文件夹。
接着考虑新情况:因为旧项目开发好后为节省存储,上传后我本地就删除了,因此我又可以在Myprojects-open(L)继续添加新项目了,开发好新项目也要上传保存。这时就会出现问题,如果直接push master就会把Myprojects-open(R)覆盖,以前存在远程的项目会因为这次push消失,这和前面删除test3.txt的例子同理。只要我还是只要Myprojects-open(R)这一个远程仓库,就避免不了这个情况。
因此先前的想法都得推翻,写到这里,我又产生新想法:一个初始文件夹一个项目不变,但是每个项目文件夹都创建对应的branch(分支),push到远程对应的branch,这要就算在Myprojects-open(L)新建项目只需不断新建branch即可。继续尝试。
比如我想新建项目Test3,内有文件test3_1.txt,内容为“我是test3_1.txt”,本例使用的push命令会自动在Myprojects-open(R)创建同名branch:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ git commit -m"我新增了项目Test3,内有文件test3_1.txt"[master 2fd10d7] 我新增了项目Test3,内有文件test3_1.txt
1file changed, 1 insertion(+)
create mode 100644 Test3/test3_1.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ cd Test3
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test3 (master)
$ git branch Test3
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test3 (master)
$ git checkout Test3
Switched to branch 'Test3'
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test3 (Test3)
$ git push Myprojects-open Test3
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 621 bytes |621.00 KiB/s, done.
Total 6(delta 1), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for'Test3' on Gitee by visiting:
remote: https://gitee.com/He-Wenxuan-LZU/myprojects-open/pull/new/He-Wenxuan-LZU:Test3...He-Wenxuan-LZU:master
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
* [new branch] Test3 -> Test3
代码执行成功,仓库gitee仓库Myprojects-open(R):
结果失败,因为把项目Test2也一并传上来了,说明在Test3项目目录下创建branch,但branch不只代表Test3这一个目录,而是代表整个Myprojects-open(L)的当前状态,做不到独立地管理每个项目,这一想法也不行。
写到这里,我又有新想法:上句表明branch只能代表仓库Myprojects-open(L)的状态,那就每个项目分配一个本地仓库!然后上传到Myprojects-open(R)的对应branch!其实不管怎么说,项目在本地总会是一个大文件夹管更多小文件夹和文件的形式。而说是本地仓库也只是对这个项目文件夹进行“git init”赋予仓库身份、多个“.git"记录文件而已,而且仍要创建branch。接着尝试。
我在Myprojects-open(L)下以相同方式创建新项目Test4和Test5,在两个项目文件夹下都进行git初始化、配置身份信息、连接Myprojects-open(R)、commit更新以及创建branch,下面只是Test5的语句(身份信息就不展示了):
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open (master)
$ cd Test5
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (master)
$ git init
Initialized empty Git repository in D:/Myprojects-open/Test5/.git/
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (Test5)
$ git remote add Test5 [email protected]:He-Wenxuan-LZU/myprojects-open.git
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (master)
$ gitadd.
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (master)
$ git commit -m"在项目Test5里创建了文件test5.txt"[master (root-commit) fa503a1] 在项目Test5里创建了文件test5.txt
1file changed, 1 insertion(+)
create mode 100644 test5.txt
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (master)
$ git branch Test5
最后push:
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (Test5)
$ git remote add Test5 [email protected]:He-Wenxuan-LZU/myprojects-open.git
Administrator@LAPTOP-2DA8EBKS MINGW64 /d/Myprojects-open/Test5 (Test5)
$ git push -u Test5 Test5
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 259 bytes |259.00 KiB/s, done.
Total 3(delta 0), reused 0(delta 0), pack-reused 0(from 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for'Test5' on Gitee by visiting:
remote: https://gitee.com/He-Wenxuan-LZU/myprojects-open/pull/new/He-Wenxuan-LZU:Test5...He-Wenxuan-LZU:master
To gitee.com:He-Wenxuan-LZU/myprojects-open.git
* [new branch] Test5 -> Test5
branch 'Test5'set up to track 'Test5/Test5'.
代码执行成功,查看项目Test4和Test5在Myprojects-open(R)的存在形式:
一半成功(因为不是我想象中的Test文件夹展示出来,而只是test文件),接着尝试在项目Test4中扩展文件夹Test4_1,内有文件test4_1.txt,代码不再重复,只展示结果:
成功!Test4是更新独立,没有影响到Test5,说明给每个项目文件夹赋予仓库身份,再创建branch,提交到Myprojects-open(R)的对应branch是可行的。这样一来,项目在本地的文件夹形式在Myprojects-open(R)上就变为分支形式,看不到Test这样的项目文件夹。
总结
合适的人群:不是团队协作开发一个项目,且项目不大,且单纯地把代码存在gitee仓库里的读者。
操作:
- 本地使用这样的项目组织形式:创建文件夹ALL,每个项目都放在一个各自的项目文件夹A、B、C等,A、B、C等都放在ALL里面;
- 在本地的总文件夹ALL和A、B、C等项目文件夹下进行git初始化,其他文件夹不做初始化,在每个项目文件夹下为每个项目创建branch,切换branch后再连接远程仓库(这个必须得先commit一次才行);
- 以后本地对每个项目做改动,直接进入对应的项目目录,切换到对应的分支,commit后直接push到gitee仓库对应分支就行。
- 只是这样也就不能做分支融合,不过对于只想存代码的我来说,足够使用了,而且没有出现复杂的git语句。
外延:如果是团队协作,但是每个人负责的板块的独立的,成员甲不要下载乙的代码,只需开发自己的板块,这种情况,倒也能使用上述方法,只需将上述项目A、B、C替换为团队项目板块。
创作不易,如果觉得有所帮助,求点赞收藏!谢谢!
版权归原作者 何文轩v2021 所有, 如有侵权,请联系我们删除。