重学Git(1)

前言

Git这玩意,对于一个自学者来说用到的确实很有限,想想我做东西,用到的大部分就是:

git add .
git commit -m ''
git push origin master

其他的回滚甚至都是通过VS Code插件完成的,可视化加对比检查,方便的紧,但是我们真的只需要这些么?

CVCS和DVCS

让我们从起源开始讲起。

CVCS,中央仓库版本控制系统,DVCS,分布式版本控制系统。众所周知,git是属于后者,但并不意味着它没有中央仓库的概念,相对于CVCS,DVCS只是在本地多了一个本地仓库,讲个人代码的提交和上传分布进行了而已,而既然进行上传,肯定是有一个名义上的汇总仓库的,即使这个仓库智能可以和本地仓库类似。

但是DVCS也不是没有缺点的,他的缺点就是本地仓库存储所有代码的设计会占用过多的内存,这一点对于普通的开发工作并不会造成太大的影响,但是对于游戏开发等占用存储较高的工作,使用CVCS还是更加有效的。

简单的看一个git status

git status能够很清晰的展示你的项目版本状态:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   NeteaseCloudMusicApi
        new file:   mymusic.iml
        modified:   src/application/Rank/index.js
        new file:   src/application/Rank/store/index.js
        modified:   src/application/Recommend/index.js
        modified:   src/components/slider/index.js
        modified:   src/store/reducer.js
        modified:   yarn.lock

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

        modified:   NeteaseCloudMusicApi (modified content, untracked content)
        modified:   src/application/Recommend/store/index.js

从上面一段我们可以看出以下四点信息

  1. 当前分支是master分支
  2. 当前分支比远程“origin/master”领先一个提交,并且可以上传这个提交
  3. 有三个新文件和五个修改过的文件可以提交
  4. 有两个修改过的文件还没有暂存到git仓库,他们不会被追踪

我们也许习惯了使用commit -m,但是其实只输入git commit也可以进行提交,只不过会通过vi或者nano之类的编辑器打开,让你再次输入提交信息,比如在我的电脑上就打开vim,修改保存之后即自动提交。

多人协作

我们创建一个远程仓库,拉到本地两个文件夹。

一个文件夹添加新内容,暂存提交上传,另一个文件夹拉取,看似是实现了多人协作的效果,但是这样的多人协作是存在明显问题的。很明显的一点就是多个人不能同时开发,如果两个人同时开发,一个先push,一人后push,后push的那个人就会受到报错:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/starinsun/git-learning.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.

解释的很清楚,别人开发的东西你的本地仓库没有,如果你强制push,就相当于你的代码覆盖掉别人开发的,那么他人的劳动成果就被废除了。

这种情况下,我们一般有两种不同的方法来解决问题,一个就是后提交的人先pull一下远程代码,使得代码合并,然后再提交,即git merge的方式。当然我们可以看出这种协作模型是不理想的,因此还有一种方式就是基于branch的方式。

下一节在讲。

发布了362 篇原创文章 · 获赞 352 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43870742/article/details/104206898