1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Git 分支操作 Git 团队协作机制 GitHub 操作

Git 分支操作 Git 团队协作机制 GitHub 操作

时间:2021-12-07 16:01:56

相关推荐

Git 分支操作 Git 团队协作机制 GitHub 操作

文章目录

第 4 章 Git 分支操作4.1 什么是分支4.2 分支的好处4.3 分支的操作4.3.1 查看分支4.3.2 创建分支4.3.3 修改分支4.3.4 切换分支4.3.5 合并分支4.3.6 产生冲突4.3.7 解决冲突4.4 创建分支和切换分支图解第 5 章 Git 团队协作机制5.1 团队内协作第 6 章 GitHub 操作6.1 创建远程仓库6.2 远程仓库操作6.2.1 创建远程仓库别名6.2.2 推送本地分支到远程仓库6.2.3 克隆远程仓库到本地6.2.4 邀请加入团队6.2.5 拉取远程库内容6.3 跨团队协作6.4 SSH 免密登录

第 4 章 Git 分支操作

4.1 什么是分支

在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)

4.2 分支的好处

同时并行推进多个功能开发,提高开发效率。

各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。

4.3 分支的操作

4.3.1 查看分支

1)基本语法

git branch -v

2)案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git branch -v* master 087a1a7 my third commit (*代表当前所在的分区)

4.3.2 创建分支

1)基本语法

git branch 分支名

2)案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git branch hot-fixLayne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git branch -vhot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支 master的内容复制了一份)* master 087a1a7 my third commit

4.3.3 修改分支

--在 maste 分支上做修改Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ vim hello.txt--添加暂存区Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git add hello.txt--提交本地库Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git commit -m "my forth commit" hello.txt[master f363b4c] my forth commit1 file changed, 1 insertion(+), 1 deletion(-)--查看分支Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git branch -vhot-fix 087a1a7 my third commit (hot-fix 分支并未做任何改变)* master f363b4c my forth commit (当前 master 分支已更新为最新一次提交的版本)--查看 master 分支上的文件内容Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ cat hello.txthello git! hello atguigu! 2222222222222hello git! hello atguigu! 3333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! master testhello git! hello atguigu!

4.3.4 切换分支

1)基本语法

git checkout 分支名

2)案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git checkout hot-fixSwitched to branch 'hot-fix'--发现当先分支已由 master 改为 hot-fixLayne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)$--查看 hot-fix 分支上的文件内容发现与 master 分支上的内容不同Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)$ cat hello.txthello git! hello atguigu! 2222222222222hello git! hello atguigu! 3333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!--在 hot-fix 分支上做修改Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)$ cat hello.txthello git! hello atguigu! 2222222222222hello git! hello atguigu! 3333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! hot-fix test--添加暂存区Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)$ git add hello.txt--提交本地库Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)$ git commit -m "hot-fix commit" hello.txt

4.3.5 合并分支

1)基本语法

git merge 分支名

2)案例实操 在 master 分支上合并 hot-fix 分支

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git merge hot-fixAuto-merging hello.txtCONFLICT (content): Merge conflict in hello.txtAutomatic merge failed; fix conflicts and then commit the result.

4.3.6 产生冲突

冲突产生的表现:后面状态为 MERGING

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)$ cat hello.txthello git! hello atguigu! 2222222222222hello git! hello atguigu! 3333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!<<<<<<< HEADhello git! hello atguigu! master testhello git! hello atguigu!=======hello git! hello atguigu!hello git! hello atguigu! hot-fix test>>>>>>> hot-fix

冲突产生的原因:

合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替我们决定使用哪一个。必须人为决定新代码内容。

查看状态(检测到有文件有两处修改)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)$ git statusOn branch masterYou have unmerged paths.(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)Unmerged paths:(use "git add <file>..." to mark resolution)both modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")

4.3.7 解决冲突

1)编辑有冲突的文件,删除特殊符号,决定要使用的内容

特殊符号:<<<<<<< HEAD 当前分支的代码 ======= 合并过来的代码 >>>>>>> hot-fix

hello git! hello atguigu! 2222222222222hello git! hello atguigu! 3333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! master testhello git! hello atguigu! hot-fix test

2)添加到暂存区

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)$ git add hello.txt

3)执行提交(注意:此时使用 git commit 命令时不能带文件名)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)$ git commit -m "merge hot-fix"[master 69ff88d] merge hot-fix--发现后面 MERGING 消失,变为正常Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

4.4 创建分支和切换分支图解

master、hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD决定的。所以创建分支的本质就是多创建一个指针。

HEAD 如果指向 master,那么我们现在就在 master 分支上。

HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。

所以切换分支的本质就是移动 HEAD 指针。

第 5 章 Git 团队协作机制

5.1 团队内协作

5.2 跨团队协作

第 6 章 GitHub 操作

GitHub 网址:/

Ps:全球最大同性交友网站,技术宅男的天堂,新世界的大门,你还在等什么?

6.1 创建远程仓库

6.2 远程仓库操作

6.2.1 创建远程仓库别名

1)基本语法

git remote -v 查看当前所有远程地址别名

git remote add 别名 远程地址

2)案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git remote -vLayne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git remote add ori /atguiguyueyue/git-shTest.gitLayne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git remote -vori /atguiguyueyue/git-shTest.git (fetch)ori /atguiguyueyue/git-shTest.git (push)

/atguiguyueyue/git-shTest.git

这个地址在创建完远程仓库后生成的连接,如图所示红框中

6.2.2 推送本地分支到远程仓库

1)基本语法

git push 别名 分支

2)案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git push ori masterLogon failed, use ctrl+c to cancel basic credential prompt.Username for '': atguiguyueyueCounting objects: 3, done.Delta compression using up to 12 pressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.Total 3 (delta 0), reused 0 (delta 0)To /atguiguyueyue/git-shTest.git* [new branch] master -> master

此时发现已将我们 master 分支上的内容推送到 GitHub 创建的远程仓库。

6.2.3 克隆远程仓库到本地

1)基本语法

git clone 远程地址

2)案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong$ git clone /atguiguyueyue/git-shTest.gitCloning into 'git-shTest'...remote: Enumerating objects: 3, done.remote: Counting objects: 100% (3/3), done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), done.

/atguiguyueyue/git-shTest.git

这个地址为远程仓库地址,克隆结果:初始化本地仓库

--创建远程仓库别名Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest(master)$ git remote -vorigin /atguiguyueyue/git-shTest.git (fetch)origin /atguiguyueyue/git-shTest.git (push)

小结:clone 会做如下操作。1、拉取代码。2、初始化本地仓库。3、创建别名

6.2.4 邀请加入团队

1)选择邀请合作者

2)填入想要合作的人

3 ) 复 制 地 址 并 通 过 微 信 钉 钉 等 方 式 发 送 给 该 用 户 , 复 制 内 容 如 下 :

/atguiguyueyue/git-shTest/invitations

4)在 atguigulinghuchong 这个账号中的地址栏复制收到邀请的链接,点击接受邀请。

5)成功之后可以在 atguigulinghuchong 这个账号上看到 git-Test 的远程仓库。

6)令狐冲可以修改内容并 push 到远程仓库。

--编辑 clone 下来的文件Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest(master)$ vim hello.txtLayne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest(master)$ cat hello.txthello git! hello atguigu! 2222222222222hello git! hello atguigu! 33333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! 我是最帅的,比岳不群还帅hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! master testhello git! hello atguigu! hot-fix test--将编辑好的文件添加到暂存区Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest(master)$ git add hello.txt--将暂存区的文件上传到本地库Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest(master)$ git commit -m "lhc commit" hello.txt[master 5dabe6b] lhc commit1 file changed, 1 insertion(+), 1 deletion(-)--将本地库的内容 push 到远程仓库Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest(master)$ git push origin masterLogon failed, use ctrl+c to cancel basic credential prompt.Username for '': atguigulinghuchongCounting objects: 3, done.Delta compression using up to 12 pressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 309 bytes | 309.00 KiB/s, done.Total 3 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.To /atguiguyueyue/git-shTest.git7cb4d02..5dabe6b master -> master

7)回到 atguiguyueyue 的 GitHub 远程仓库中可以看到,最后一次是 lhc 提交的。

6.2.5 拉取远程库内容

1)基本语法

git pull 远程库地址别名 远程分支名

2)案例实操

--将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ git pull ori masterremote: Enumerating objects: 5, done.remote: Counting objects: 100% (5/5), done.remote: Compressing objects: 100% (1/1), done.remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0Unpacking objects: 100% (3/3), done.From /atguiguyueyue/git-shTest* branch master -> FETCH_HEAD7cb4d02..5dabe6b master -> ori/masterUpdating 7cb4d02..5dabe6bFast-forwardhello.txt | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ cat hello.txthello git! hello atguigu! 2222222222222hello git! hello atguigu! 33333333333333hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! 我是最帅的,比岳不群还帅hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu!hello git! hello atguigu! master testhello git! hello atguigu! hot-fix test

6.3 跨团队协作

1)将远程仓库的地址复制发给邀请跨团队协作的人,比如东方不败。

2)在东方不败的 GitHub 账号里的地址栏复制收到的链接,然后点击 Fork 将项目叉到自己的本地仓库。

叉入中…

叉成功后可以看到当前仓库信息。

3)东方不败就可以在线编辑叉取过来的文件。

4)编辑完毕后,填写描述信息并点击左下角绿色按钮提交。

5)接下来点击上方的 Pull 请求,并创建一个新的请求。

6)回到岳岳 GitHub 账号可以看到有一个 Pull request 请求。

进入到聊天室,可以讨论代码相关内容。

7)如果代码没有问题,可以点击 Merge pull reque 合并代码。

6.4 SSH 免密登录

我们可以看到远程仓库中还有一个 SSH 的地址,因此我们也可以使用 SSH 进行访问。

具体操作如下:

--进入当前用户的家目录Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)$ cd--删除.ssh 目录Layne@LAPTOP-Layne MINGW64 ~$ rm -rvf .sshremoved '.ssh/known_hosts'removed directory '.ssh'--运行命令生成.ssh 秘钥目录[注意:这里-C 这个参数是大写的 C]Layne@LAPTOP-Layne MINGW64 ~$ ssh-keygen -t rsa -C atguiguyueyue@Generating public/private rsa key pair.Enter file in which to save the key (/c/Users/Layne/.ssh/id_rsa):Created directory '/c/Users/Layne/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /c/Users/Layne/.ssh/id_rsa.Your public key has been saved in /c/Users/Layne/.ssh/id_rsa.pub.The key fingerprint is:SHA256:7CPfRLITKcYDhaqpEDeok7Atvwh2reRmpxxOC6dkY44 atguiguyueyue@The key's randomart image is:+---[RSA 2048]----+| .. || .. || . .. ||+ + o . . ||oO . = S . ||X . .. + = ||+@ * .. = . ||X.&o+. o = ||Eo+Oo . . |+----[SHA256]-----+--进入.ssh 目录查看文件列表Layne@LAPTOP-Layne MINGW64 ~$ cd .sshLayne@LAPTOP-Layne MINGW64 ~/.ssh$ ll -atotal 21drwxr-xr-x 1 Layne 197609 0 11 月 25 19:27 ./drwxr-xr-x 1 Layne 197609 0 11 月 25 19:27 ../ -rw-r--r-- 1 Layne 197609 1679 11 月 25 19:27 id_rsa-rw-r--r-- 1 Layne 197609 406 11 月 25 19:27 id_rsa.pub--查看 id_rsa.pub 文件内容Layne@LAPTOP-Layne MINGW64 ~/.ssh$ cat id_rsa.pubssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRXRsk9Ohtg1AXLltsuNRAGBsx3ypE1O1Rkdzpml1woa6y6G62lZri3XtCH0F7GQvnMvQtPISJFXXWo+jFHZmqYQa/6kOIMv2sszcoj2QtwllGXTPn/4T2h/cHjSHfc+ks8OYP7OWOOefpOCbYY/7DWYrl89k7nQlfd+A1FV/vQmcsa1LP5ihqjpjms2CoUUen8kZHbjwHBAHQHWRE+Vc371MG/dwINvCi8n7ibI86o2k0dW0+8SL+svPV/Y0G9m+RAqgec8b9U6DcSSAMH5uq4UWfnAcUNagb/aJQLytrH0pLa8nMv3XdSGNNoAGBFeW2+K81XrmkP27FrLI6lDef atguiguyueyue@

复制 id_rsa.pub 文件内容,登录 GitHub,点击用户头像→Settings→SSH and GPG keys

接下来再往远程仓库 push 东西的时候使用 SSH 连接就不需要登录了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。