记录一下Git的使用。首先需要准备两个邮箱,用来操作多人开发。
我们这里使用gitee。github实在是太卡了。
项目经理首先在gitee上创建云仓库

创建成功之后会跳转到如下页面,会有一些入门教程

然后我们的项目经理搭建项目:
初始化
$ git init
Initialized empty Git repository in D:/gongj/gongjieTest/git-test/.git/
$ touch README.md

然后执行以下命令

补充
当你是第一次在进行commit的时候,git需要你输入自己的邮箱和用户名。
--global代表是全局的,如果不加是在你当前项目里有效。
image.png
发现我们执行push的时候出现了以下提示,因为这是我们的第一次提交,git并不知道你要push到哪里去,所以我们需要提供一下地址。

git给我们提供了两个地址,我们这里就使用https,ssh我们之后再说。
将https的地址copy下来:https://gitee.com/gongjienianq/git-test.git,发现这地址还是比较长的,我们不可能去记住它,能不能给它起一个别名呢。
git肯定是提供了的。git remote 关联远程库
$ git remote -v
$ git remote add origin https://gitee.com/gongjienianq/git-test.git
$ git remote -v
origin https://gitee.com/gongjienianq/git-test.git (fetch)
origin https://gitee.com/gongjienianq/git-test.git (push)
- git remote -v 查看关联的所有的远程仓储名称及地址
- git remote add url 添加一个远程仓库
然后就准备push啦,执行命令之后弹出一个框,这需要输入你的git账号和密码。
$ git push origin master
git push <别名> <分支名>

账号密码输入成功之后
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 249 bytes | 249.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/gongjienianq/git-test.git
* [new branch] master -> master

项目已经搭建好了,项目经理想了想,我们不能在master上直接开发,项目经理就创建了两个分支。


- git branch -v:查看所有分支
- git branch <分支名>:新建分支
- git checkout <分支名>:切换分支
我们先可以看云仓库现在有几个分支,可以看到现在就只有master分支

我们现在已经到了dev分支了
$ touch nianq.txt
$ git status
$ git add .
$ git commit -m "提交第一句代码"
$ git push --set-upstream origin dev
现在就有dev分支和master分支

提交uat分支
$ git checkout uat
$ git push origin uat

分支也有了,项目经理让手底下的一个小喽啰将dev分支的代码拉下来进行开发。
$ git clone -b dev https://gitee.com/gongjienianq/git-test.git
-
-b <name> 指定要克隆的分支名
image.png
小喽啰也进行了开发然后提交
$ git remote -v
origin https://gitee.com/gongjienianq/git-test.git (fetch)
origin https://gitee.com/gongjienianq/git-test.git (push)
$ touch gongjie123.txt
$ git status
$ git add .
$ git commit -m "提交"
$ git push
在执行git push的时候需要登录

输入正确的账号密码之后,又会提示你

您没有权限推送到此存储库,这需要你的项目经理将你添加到团队中。
项目经理在gitee上找到该项目。

我这里就选择直接添加,输入gitee用户的邮箱。

然后等你邀请的人同意,就可以成为项目的正式开发者了。

再次执行git push

多人合作成功

在团队开发中,发送冲突是显而易见的事情,那怎么解决冲突呢!
那为什么会造成冲突呢。假设现在有A、B两位员工,AB两位员工将代码各自拉取到本地进行开发,A优先将代码开发完毕,将代码提交到远程仓库,过了一会,B也开发完毕,也想将代码提交到远程仓库,这时发现提交不上去。原因是B的这个版本不是服务器上最新的版本,恰巧B本地的代码和远程仓库的代码在同一个文件的同一个地方做了修改,因此产生了冲突,这是我们应该先使用git pull命令更新最新的代码,解决掉冲突再上传代码。接下来我们模拟一下冲突,根据下面的图片我们可以看到两者代码是最新的。


小喽啰在自己本地的gongjie123.txt添加了几行代码,然后进行提交

$ git add .
$ git commit -m "小喽啰完成需求"
$ git push

然后项目经理在同一时间也对gongjie123.txt进行了修改。

$ git add .
$ git commit -m "项目经理提交代码"
$ git push
执行完git push命令就会出现以下错误:


$ git pull origin dev
注:pull = fetch + merge
- git fetch 远程仓库别名 分支名:抓取远程库的指定分支到本地,但没有合并
-
git merge 远程仓库别名 分支名:将抓取下来的远程的分支,跟当前所在分支进行合并
image.png
MERGING就代表你这有冲突,需要解决合并。
image.png
删除不必要的部分(不要乱删别人的代码,最好和别人协商一下)
以下是我的修改
image.png
$ git add .
$ git commit -m "解决冲突,并提交代码"
$ git push origin dev
注意commit操作不要加文件名
注意:上面的每次切换账号都需要将windows凭据删除,不然不会弹出那个需要输入账号密码的框。

Git的常用命令:https://www.jianshu.com/p/11aca675d9df
ssh方式拉取代码:https://www.jianshu.com/p/6bc033293d04
网友评论