[] Git Git many people work together


Reprinted: https://www.cnblogs.com/onelikeone/p/6857910.html
reprint: https://www.cnblogs.com/zhaoyanjun/p/5882784.html

Create a form of organization

To find out github rights management, you need to understand a few concepts. organization (organization), repository (warehouse), Team (Team).

In understanding these concepts, listen to me tell a little story. Saying There was once a businessman, because he smart and capable, he opened a fruit stand, tea shops, and a cafe. Blink of an eye 10 years later, he has accumulated a lot of money, but also with very fast hardware rights in general is rich and powerful it was, Ever since the day he led 100 brothers set up a mafia organization: the Green Gang. After the organization was founded, he put 100 into four brothers team, each team 25 people. After the team was established, the brothers can not sit and starve it, Ever since he has opened a ktv, a supermarket, a large foot care. So the question becomes, how to make four team took these three projects, there must be clear lines of authority control, if access control shall not be treated, there will be problems his brother each other seats and seize territory. A project can have multiple management team, a team can control multiple projects, so many to many relationship between the team and the project.

The role of asset analysis stories of
businessmen owned assets:
three items: fruit stand, milk food stores, Internet cafes.
An organization: Green Gang

Green Gang organization owned assets:
three projects: Ktv, supermarkets, big foot care
4 team

Note: fruit stand, milk food stores, cafes assets belonging to these three projects only a businessman, not owned by any organization owned. It extends about, businessmen can again create a new project, such as a 4s shop; and he can then create a new organization, such as a flood to help the organization. Also in the flood to help the organization which can create more than one team.

You can now convert the ideas into github, a correspondence relationship

Businessman -> you register github account

Fruit stand -> Repository (warehouse)

Green Gang -> Organization (Organization)

Team -> Team (Team)

1, github combat - create an organization

In the story, a businessman can create projects and organizations. Corresponds to the user can be created on github repository and organizations.
Here Insert Picture Description
Here Insert Picture Description
Complete example

2, github combat - Creating a warehouse in the organization
when the organization has been created to look at the structure of the organization
Here Insert Picture Description
in Organ-Name organization, create a warehouse

3, github real - Create team in the organization

team after the completion of creation, the default of a member of the team only one person is the account. The following began to add other members of the team.

After the invitation is successful, people need to be invited to their mailbox click, confirm the invitation

4, github combat - adding to the warehouse team in the organization and set permissions.


You can see, warehouse control rights to the team, there are three
Admin administrator privileges (only read, clone, push, add members to the warehouse)
the Write write permissions (can only be read, clone, the Push)
the Read permission to read (can only be read, clone)

In addition any of the Team for multiple organizations can use to add authority here have all been completed.

总结
通过这篇文章可以在github愉快的使用权限管理了,但是github不能免费的创建私有仓库,这是一个很严重的问题。如果是开源项目,用github完全没有问题。如果是私有项目,可以有以下几个途径达到要求
1、在github花钱购买私有仓库。

2、使用国内比较出名的开源中国git托管服务:https://git.oschina.net/

3、使用GitLab,这需要在自己的服务器上部署。传送门:https://about.gitlab.com/gitlab-com/

不创建组织的形式

Git可以完成两件事情:

  1. 版本控制
  2. 多人协作开发

如今的项目,规模越来越大,功能越来越多,需要有一个团队进行开发。

如果有多个开发人员共同开发一个项目,如何进行协作的呢。

Git提供了一个非常好的解决方案 ---- 多人协作开发。

1.多人协作原理
典型的做法是,首先创建一个git服务器,被多个人所操作。

Here Insert Picture Description

1.多人协助实现
分为如下几个步骤:

  1. 创建一个git裸服务器 (git init --bare)
  2. 从裸服务器将版本库克隆至本地(git clone )
  3. 本地常规操作
  4. 推送版本至服务器 (git remote + git push origin master)
  5. 从远程服务器拉取版本(git pull)

一般而言,我们需要在Linux服务器上来搭建我们的git版本服务器,每个公司都会有。

由项目负责人开始。

我们现在是windows系统上模拟这个操作过程。

(1).创建一个git裸服务器 (git init --bare)
由负责人来完成的。服务器新建一个项目目录,如git-server
Here Insert Picture Description
创建完毕,我们发现git-server内容和上次的不太一样。

上次使用git init 创建之后,会有一个隐藏的.git目录。目录中的才是现在看到的内容。

也就是说,现在根本就没有.git目录了。

这就意味着在这个目录中,不能做常规的开发。

(2).从裸服务器将版本库克隆至本地(git clone )
在git版本服务器,一般是不做任何开发工作的。如果要开发项目,就需要将版本库从服务器克隆到本地。

假设有一个程序员甲,开始自己的工作了。

使用命令 git clone git版本服务器地址

在windows下面,就是使用绝对路径,如下:
Here Insert Picture Description
然后,甲就可以在这个目录下,进行常规开发。
Here Insert Picture Description

我们可以在这个目录下,创建自己的工作区目录,完成常规开发。

(3).本地常规操作
甲可以,在本地进行常规开发。

Here Insert Picture Description
Here Insert Picture Description
这个过程,可以反复进行。
Here Insert Picture Description
Here Insert Picture Description
我的第一个模块(功能)开发完毕。需要将其推送到服务器。

(4).推送版本至服务器 (git remote + git push origin master)
当在本地完成一个模块(功能),需要推送到服务器,供其他同事使用。

第一件事情,需要知道服务器在哪儿?

git remote

Here Insert Picture Description
第二件事情,直接推送即可

git push origin master

其中origin就是使用git remote得到的远程服务器的名称。

master表示是主分支。
Here Insert Picture Description
对于甲来说,它的工作已经告一段落了,该轮到乙程序员出场了。

乙程序员,首先将版本库从git服务器上克隆到本地。

Here Insert Picture Description
打开这个目录,然后进可以看到最新新的内容,如下:
Here Insert Picture Description
对于乙而言,可以在本地进行常规开发。与此同时,甲继续他的常规开发。

模拟乙程序员在本地的开发。

Here Insert Picture Description
Here Insert Picture Description
将完成的工作,推送到git服务器。
Here Insert Picture Description
回头,再看看甲的开发。
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
甲收工,准备下班了。在下班之前,需要将最新版本推送到git服务器。

开始使用命令,执行如下:

Here Insert Picture Description
结果出错了,why?

之所以会出错,原因在于:其他程序员已经将最新的一个版本提交到git服务器上,但是你在提交之前,已经不是最新的。

在这种情况下,甲,需要先从服务器拉取最新的版本。

(5).从远程服务器拉取版本(git pull)
在多人协助开发时,每个开发人员在推送自己的最新版本时,都需要确保当前版本是最新的,所以就需要先获取最新版本,也就是说需要从服务器拉取最新版本到本地。

需要使用 git pull命令
Here Insert Picture Description
如此一来,甲当前就是最新的版本。

然后再次使用 git push 命令推送至服务器。
Here Insert Picture Description
接下来需要分两种情况:

如果有新的开发人员加入进来,重复2~5过程。

如果不是新的开发人员,重复3~5过程。

比如,对于乙而言,其实它现在已经不是最新的版本了,所以需要使用 git pull 拉取最新版本。
Here Insert Picture Description
所以,对很多开发人员而言,一打开电脑,马上先git pull,拉取最新的。然后进行常规开发,

开发完毕之后,在git push之前,还需要使用git pull再拉取一遍。

如果还有一个新的程序员丙,加入了,怎么办呢?

需要先git clone
Here Insert Picture Description
然后就进行常规开发,推送版本、拉取版本。

在整个协作开发时,有时候会出现冲突。通常都是由于开发人员分工不明确导致的,所以如果出现这种情况,需要两个程序员协商解决。

3.分支
(1).什么是分支

在前面所有的操作当中,我们一直使用的是master主分支。以刚才的项目版本控制为例
Here Insert Picture Description
有四个版本,在我们的版本库中,都是存在于master主分支上的。

图示如下:

Here Insert Picture Description
如果我们的项目本身比较简单,只需要有主分支master就够了。

但是,实际上并不是这样的。

在这个世界上,有一种软件叫做开源软件 – 源代码开发,所有的人都可以免费使用。

开源软件是由世界上无数的程序员共同来开发。

每个程序员都可以创建一个自己的分支,这个自己分支和主master完全独立的两个分支。

Here Insert Picture Description

相应的,每个程序员都可以拥有自己的分支,可以进行任何的开发,此时和master没有什么关系的。

一旦开发完毕,就可以将你的分支合并到主分支上去。

什么时候会用到分支呢?

假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了。如果等代码全部写完再一次提交,又存在丢失每天进度的巨大风险,怎么办?

你可以创建一个属于自己的分支,别人看不见,还继续在原来的分支上工作,而你在自己的分支上进行开发,等开发完毕,合并即可。

在开源世界中,需用大量的程序员共同维护一个项目。也是需要使用分支,如Jquery。

(2).分支的基本操作
基本操作有如下几个:

  1. 查看当前分支 (git branch)

  2. 创建分支 (git branch 分支名)

  3. 切换分支(git checkout 分支名)

  4. 分支上的常规操作

  5. 分支的合并 (git checkout master + git merge 分支名)

  6. 分支的删除(git branch -d 分支名)

查看当前分支 (git branch)
Here Insert Picture Description
其中的 * 表示 当前分支。

默认情况下,只有一个master主分支。

创建分支 (git branch 分支名)
Here Insert Picture Description
切换分支(git checkout 分支名)

创建完成之后,就有了一个新的分支,但是并没有立即切换到新的分支,需要使用命令切换一下。
Here Insert Picture Description
分支上的常规操作

已经切换到b1分支上,就可以在b1分支进行常规开发和操作。
Here Insert Picture Description
使用git add 和git commit提交。

使用git log查看即可:
Here Insert Picture Description
与之对应的,我们再次切换到master分支上看看是什么情况:
Here Insert Picture Description
说明在master分支上,并没有新提交的内容。

分支的合并 (git checkout master + git merge 分支名)

分支的合并,一定是在 主分支上进行的。

只能在主分支合并其它分支。

需要两步:

1) 切换到主分支

2) 使用git merge 分支名 进行合并
Here Insert Picture Description
Here Insert Picture Description
再次查看master的一个log情况,如下:
Bold Style

分支的删除(git branch -d 分支名)

使用命令git branch -d 分支名
Here Insert Picture Description
如果你发现你的分支中所做的开发没有任何用处,也可以不合并直接删除。

(3).分支的原理
分支的过程及原理如下:

默认只有master的情况下,master总是指向最新的版本,而HEAD指针总是指向master的。

Here Insert Picture Description
现在,我创建了一个新的分支dev,将当前分支指定为dev,此时,master和dev都指向当前最新版本,但是HEAD指针已经指向了dev分支。

Here Insert Picture Description

接下来,我们提交了新的版本,dev指向最新版本,而master则原地不动。

HEAD指向当前分支dev的。
Here Insert Picture Description
当在dev分支上完成开发之后,可以将它合并到主分支master上。

When you merge, you need to switch to the master, meaning HEAD pointing to the master, the merger when in fact the most recent version of the master and dev synchronization.

Here Insert Picture Description
dev branch's mission has been completed, there is no effect, and will delete it off. Only one main branch.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42322103/article/details/95301934
Git
Recommended