git的本地结构
git add:从工作区提交到暂存区
git commit:从暂存区提交到本地库
代码托管中心
代码托管中心的任务是帮我们维护远程库 本地库和远程库的交互方式
- 团队内部协作
- push 命令:将本地库内容推送到远程库
- clone 命令:从远程库克隆到本地库
- pull 命令:从远程库拉取资源到本地库(可能由于信息不同步,一部分资料本地没有)
- 跨团队协作
- pull request命令:A团队远程库拉取B团队远程库时,pull请求需要经过B团队的审核,审核同意后才能拉取
- merge 命令:审核通过,则将B远程库中更新的东西合并到A库中
- fork 命令: A团队将B团队的远程库,复制一份形成新的远程库
初始化本地仓库
- 创建一个文件夹 GitRepository
- 打开Git Bash
// 查看git 安装的版本
git --version
//清屏操作
clear
// 设置签名。
// 设置用户名和邮箱
git config --global user.name "ZhongHD" //设置全局变量:用户名
git config --global user.email "chinass@126.com"
// 本地仓库的初始化
cd /d/program/Git/GitRepository //先cd到目标路径
git init // 初始化git仓库
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/program/Git/GitRepository (master)
$ ll -la
--------------------------------------------------------
total 8
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 ./
drwxr-xr-x 1 ZHD 197121 0 Nov 15 16:40 ../
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 .git/
其中.git目录是隐藏目录 查看.git下的文件内容,.git里面的内容不要所以手动修改
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/program/Git/GitRepository (master)
$ cd .git
-------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/program/Git/GitRepository/.git (GIT_DIR!)
$ ll -la
--------------------------------------------------------
total 11
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 ./
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 ../
-rw-r--r-- 1 ZHD 197121 23 Nov 15 17:04 HEAD
-rw-r--r-- 1 ZHD 197121 130 Nov 15 17:04 config
-rw-r--r-- 1 ZHD 197121 73 Nov 15 17:04 description
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 hooks/
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 info/
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 objects/
drwxr-xr-x 1 ZHD 197121 0 Nov 15 17:04 refs/
其中config是配置文件,查看config里面的内容
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository/.git (GIT_DIR!)
$ head config
-----------------------------------------------
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
git常用命令
1. add和commit命令
展示:
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository/.git (GIT_DIR!)
$ git add ~/Desktop/GitDemo.txt
----------------------------------------------
fatal: this operation must be run in a work tree
- 因此 必须要在git运行的目录创建文件。
- 在/GitRespository目录下创建一个GitDemo.txt文件
- 将文件提交到git的暂存区
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add GitDemo.txt
- 将暂存区的内容提交到本地库
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
// -m 表示message
$ git commit -m "这是我提交的第一个文件GitDemo.txt" GitDemo.txt
-----------------------------------------------------
[master (root-commit) 5ac02b0] 这是我提交的第一个文件GitDemo.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 GitDemo.txt
【注意事项】
- 不放在本地仓库的文件Git不进行管理
- 即使放在本地仓库的文件,git也不进行管理,必须通过add,commit命令操作才可以将内容提交到本地库
2. status命令
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
---------------------------------------------------
On branch master //显示在主分支
nothing to commit, working tree clean // 显示暂存区为空
接下来我们在GitRespository目录下新建一个Demo02.txt文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
------------------------------------------------
On branch master
Untracked files: // 未被追踪的文件
(use "git add <file>..." to include in what will be committed)
Demo02.txt
nothing added to commit but untracked files present (use "git add" to track)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add Demo02.txt
------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
-------------------------------------------------------
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: Demo02.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "这是我提交的第二个命令" Demo02.txt
------------------------------------------------------
[master 756b2d2] 这是我提交的第二个命令
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Demo02.txt
- 修改GitRepository目录下的Demo02.txt文件,例如增加内容
000
111
222
333
aaa
bbb
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
--------------------------------------------------------
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed) // 可以对暂存区的文件进行更新
(use "git restore <file>..." to discard changes in working directory)
modified: Demo02.txt
no changes added to commit (use "git add" and/or "git commit -a")
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add Demo02.txt
-------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
------------------------------------------------------
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: Demo02.txt // 经修改的文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "修改了文件Demo02.txt中的内容" Demo02.txt
---------------------------------------------------------
[master ab87f9c] 修改了文件Demo02.txt中的内容
1 file changed, 6 insertions(+) // 显示Demo02.txt中增加了6行内容,与实际情况一致
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
----------------------------------------------------------
On branch master
nothing to commit, working tree clean
3. log命令
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git log // git会从近到远展示日志信息
-------------------------------------------------------
commit ab87f9c5f8eae46e8383bdf2a713eaa7f1e18474 (HEAD -> master)
Author: ZhongHD <16***90@qq.com>
Date: Tue Nov 16 16:51:11 2021 +0800
修改了文件Demo02.txt中的内容
commit 756b2d2bacdbff0ee08068038de0e13550c82a1e // 表示当前历史记录对应的索引(key)
Author: ZhongHD <16***90@qq.com>
Date: Tue Nov 16 16:34:22 2021 +0800 // 当前历史记录对应的内容(value)
这是我提交的第二个命令
commit 5ac02b04b9d378992296284b85af246ef83b4faf
Author: ZhongHD <16***90@qq.com>
Date: Tue Nov 16 10:55:14 2021 +0800
这是我提交的第一个文件GitDemo.txt
4. reset命令
reset命令:前进或后退历史版本
4.1. 具体命令
git reset --hard [索引数]
- 新建test.txt,提交到暂存区,再提交到本地库
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test.txt
--------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "我创建了一个test.txt文件" test.txt
--------------------------------------------------
[master fb01918] 我创建了一个test.txt文件
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
- 修改test.txt内容,插入“aaa”,重新提交
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test.txt
---------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "insert aaa" test.txt
---------------------------------------------------
[master 1623315] insert aaa
1 file changed, 1 insertion(+), 3 deletions(-)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test.txt
----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "insert bbb" test.txt
----------------------------------------------------
[master 5c64d2c] insert bbb
1 file changed, 2 insertions(+), 1 deletion(-)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test.txt
----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "insert ccc" test.txt
----------------------------------------------------
[master 9c5c623] insert ccc
1 file changed, 2 insertions(+), 1 deletion(-)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reflog
----------------------------------------------------
9c5c623 (HEAD -> master) HEAD@{0}: commit: insert ccc
5c64d2c HEAD@{1}: commit: insert bbb
1623315 HEAD@{2}: commit: insert aaa
4b51dcb HEAD@{3}: commit: insert something
fb01918 HEAD@{4}: commit: 我创建了一个test.txt文件
ab87f9c HEAD@{5}: commit: 修改了文件Demo02.txt中的内容
756b2d2 HEAD@{6}: commit: 这是我提交的第二个命令
5ac02b0 HEAD@{7}: commit (initial): 这是我提交的第一个文件GitDemo.txt
目前的指针指在“insert ccc”这个版本,其中HEAD@(i)中的 i 表示的是指针回退到这个版本所需要的步数,HEAD表示这个指针的名称 例如要从“insert ccc”版本回退到“insert aaa”版本
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reset --hard 1623315 // 后面加的是所需要跳转的索引数
HEAD is now at 1623315 insert aaa
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reflog
1623315 (HEAD -> master) HEAD@{0}: reset: moving to 1623315
9c5c623 HEAD@{1}: commit: insert ccc
5c64d2c HEAD@{2}: commit: insert bbb
1623315 (HEAD -> master) HEAD@{3}: commit: insert aaa
4b51dcb HEAD@{4}: commit: insert something
fb01918 HEAD@{5}: commit: 我创建了一个test.txt文件
ab87f9c HEAD@{6}: commit: 修改了文件Demo02.txt中的内容
756b2d2 HEAD@{7}: commit: 这是我提交的第二个命令
5ac02b0 HEAD@{8}: commit (initial): 这是我提交的第一个文件GitDemo.txt
- 查看文件test.txt的内容,发现内容已经发生回退
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ tail test.txt
aaa
- 如果回退后悔了,还是想要ccc的版本,则继续使用reset命令
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reset --hard 9c5c623
HEAD is now at 9c5c623 insert ccc
4.2. reset的hard、mixed、soft参数
- hard参数
- 本地库的版本进行回退时,暂存区和工作区的代码同时跟着回退
- 本地库指针移动的同时,暂存区、工作区的指针同步移动
- mixed参数 (不常用)
- 本地库指针移动时
- 暂存区指针跟着本地库移动
- 工作区的指针 不移动
- soft参数 (也不太常用)
5. 删除文件&找回文件
5.1. 找回本地库删除的文件
1.新建一个文件test2.txt
2.将它add到暂存区中
3.commit到本地库中
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "添加test2.txt文件" test2.txt
-----------------------------------------------------
[master 3471fae] 添加test2.txt文件
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ rm test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
----------------------------------------------------------
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "删除test2.txt" test2.txt
----------------------------------------------------
[master a611dcb] 删除test2.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reflog
-------------------------------------------------
a611dcb (HEAD -> master) HEAD@{0}: commit: 删除test2.txt
3471fae HEAD@{1}: commit: 添加test2.txt文件
9c5c623 HEAD@{2}: reset: moving to 9c5c623
1623315 HEAD@{3}: reset: moving to 1623315
9c5c623 HEAD@{4}: commit: insert ccc
5c64d2c HEAD@{5}: commit: insert bbb
1623315 HEAD@{6}: commit: insert aaa
4b51dcb HEAD@{7}: commit: insert something
fb01918 HEAD@{8}: commit: 我创建了一个test.txt文件
ab87f9c HEAD@{9}: commit: 修改了文件Demo02.txt中的内容
756b2d2 HEAD@{10}: commit: 这是我提交的第二个命令
5ac02b0 HEAD@{11}: commit (initial): 这是我提交的第一个文件GitDemo.txt
这里git并不是真把文件给删除了,而是通过指针回到了HEAD@(0)这个索引
- 找回本地库中删除的文件,实际上就是将历史版本切换到感概添加文件的那个索引
$ git reset --hard HEAD@{1}
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reflog
---------------------------------------------
3471fae (HEAD -> master) HEAD@{0}: reset: moving to 3471fae
9c5c623 HEAD@{1}: reset: moving to 9c5c623
a611dcb HEAD@{2}: commit: 删除test2.txt
3471fae (HEAD -> master) HEAD@{3}: commit: 添加test2.txt文件
9c5c623 HEAD@{4}: reset: moving to 9c5c623
1623315 HEAD@{5}: reset: moving to 1623315
9c5c623 HEAD@{6}: commit: insert ccc
5c64d2c HEAD@{7}: commit: insert bbb
1623315 HEAD@{8}: commit: insert aaa
4b51dcb HEAD@{9}: commit: insert something
fb01918 HEAD@{10}: commit: 我创建了一个test.txt文件
ab87f9c HEAD@{11}: commit: 修改了文件Demo02.txt中的内容
756b2d2 HEAD@{12}: commit: 这是我提交的第二个命令
5ac02b0 HEAD@{13}: commit (initial): 这是我提交的第一个文件GitDemo.txt
-------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ ls
-------------------------------------------------
Demo02.txt GitDemo.txt test.txt test2.txt
5.2. 找回暂存区删除的文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ rm test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ ls
Demo02.txt GitDemo.txt test.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
-----------------------------------------------------
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reflog
--------------------------------------------------------
3471fae (HEAD -> master) HEAD@{0}: reset: moving to 3471fae
9c5c623 HEAD@{1}: reset: moving to 9c5c623
a611dcb HEAD@{2}: commit: 删除test2.txt
3471fae (HEAD -> master) HEAD@{3}: commit: 添加test2.txt文件
9c5c623 HEAD@{4}: reset: moving to 9c5c623
1623315 HEAD@{5}: reset: moving to 1623315
9c5c623 HEAD@{6}: commit: insert ccc
5c64d2c HEAD@{7}: commit: insert bbb
1623315 HEAD@{8}: commit: insert aaa
4b51dcb HEAD@{9}: commit: insert something
fb01918 HEAD@{10}: commit: 我创建了一个test.txt文件
ab87f9c HEAD@{11}: commit: 修改了文件Demo02.txt中的内容
756b2d2 HEAD@{12}: commit: 这是我提交的第二个命令
5ac02b0 HEAD@{13}: commit (initial): 这是我提交的第一个文件GitDemo.txt
发现只要移动指针到HEAD@{3},就能恢复文件。(删除操作实际上是指针往回滚了一次)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reset --hard 3471fae
------------------------------------------------
HEAD is now at 3471fae 添加test2.txt文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ ls
-------------------------------------------------
Demo02.txt GitDemo.txt test.txt test2.txt
- 实际上还有还有更简单的操作,只需要 git reset --hard HEAD (看当前这个HEAD指针指向哪儿了,重复这个操作)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git reset --hard HEAD
HEAD is now at 3471fae 添加test2.txt文件
6. diff命令:比较文件的差异
6.1. 比较工作区和暂存区文件差异
- 新建文本test3.txt,随意录入内容,例如“aaaa”
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ touch test3.txt
----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ ls
Demo02.txt GitDemo.txt test.txt test2.txt test3.txt
----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ vi test3.txt
------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ tail test3.txt
aaaa
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test3.txt
-------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "提交了test3.txt" test3.txt
[master c98da77] 提交了test3.txt
1 file changed, 1 insertion(+)
create mode 100644 test3.txt
------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git status
On branch master
nothing to commit, working tree clean
- 更改工作区中test3.txt中的内容,增加内容“bbb”
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ vi test3.txt
-------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ tail test3.txt
aaaabbbbb
- 经过以上操作,导致工作区中和暂存区中文件内容不一致
- 通过git diff命令比对工作区和暂存区中文件在哪里不一致
git diff [文件名] // 将工作区文件和暂存区中文件进行比较
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git diff test3.txt
--------------------------------------------------
diff --git a/test3.txt b/test3.txt
index 7284ab4..6100b7b 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1 +1 @@
-aaaa // git是按行为单位管理数据的,它先把原先文件中的aaaa给删除了,然后又增加了一行aaaabbbbb
\ No newline at end of file
+aaaabbbbb
- 针对之前空的test2.txt文件,往里面增加内容“qqq”
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ head test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ vi test2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ head test2.txt
qqq
6.1.1. 比较多个文件的差异
git diff //比较的是工作区中和暂存区中的所有文件差异
- 执行git diff命令,后面不加参数,即可查看工作区与暂存区不同的所有文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git diff
------------------------------------------------
diff --git a/test2.txt b/test2.txt
index e69de29..1b7ae83 100644
--- a/test2.txt
+++ b/test2.txt
@@ -0,0 +1 @@
+qqq
diff --git a/test3.txt b/test3.txt
index 7284ab4..6100b7b 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1 +1 @@
-aaaa
\ No newline at end of file
+aaaabbbbb
6.2. 比较工作和本地库文件差异
git diff 历史版本索引 文件名
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git diff HEAD test3.txt // 工作区文件和本地库当前版本文件相比较
----------------------------------------------------------
diff --git a/test3.txt b/test3.txt
index 7284ab4..6100b7b 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1 +1 @@
-aaaa
\ No newline at end of file
+aaaabbbbb
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "insert bbbbb" test3.txt
--------------------------------------------------------
[master ff88f98] insert bbbbb
1 file changed, 1 insertion(+), 1 deletion(-)
- 改变test3.txt文件中的内容为“a”,然后git add到暂存区
- 改变test3.txt文件中的内容为“ad”
- 经过以上步骤,工作区、暂存区、本地库三者的test3.txt文件均不相同
- 本地库内容为 “aaaabbbbb”
- 暂存区内容为"a"
- 工作区内容为"ad"
此时再来审视git diff的两个命令
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git diff HEAD test3.txt
------------------------------------------------------
diff --git a/test3.txt b/test3.txt
index 6100b7b..bf1e18f 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1 +1 @@
-aaaabbbbb
+ad // 此处可以发现,它是在和工作区的文件相比较
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git diff test3.txt
---------------------------------------------------
diff --git a/test3.txt b/test3.txt
index 7898192..bf1e18f 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1 +1 @@
-a
+ad // 暂存区,也是在和工作区的文件相比较
因此可以得出结论,git diff的比较本质上是工作区和暂存区或者本地库的比较
- git diff [文件名]
- git diff 历史版本索引数 文件名
分支
7. 什么是分支
- 何为分支
- 在版本控制中,使用多条线同时推进多个任务。这里说的多条线,就是多个分支。
- 分支的好处
- 同时多个分支可以并行开发,互相不耽误、互不影响,提高开发效率
- 如果有一个分支功能开发失败,直接删除这个分支即可,不会对其他分支产生任何影响
8. 操作分支
- 在工作区新建一个test4.txt文件,随便输入内容abc
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ touch test4.txt
-------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ vi test4.txt
--------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ head test4.txt
abc
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test4.txt
-------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "添加了test4.txt文件" test4.txt
--------------------------------------------------------
[master b68a2b0] 添加了test4.txt文件
1 file changed, 1 insertion(+)
create mode 100644 test4.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git branch -v // -v可以查看当前git中的所有分支
--------------------------------------------------
* master b68a2b0 添加了test4.txt文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git branch mybranch // 创建新分支mybranch
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git branch -v
--------------------------------------------
* master b68a2b0 添加了test4.txt文件 // 这里的*表示当前在哪个分支
mybranch b68a2b0 添加了test4.txt文件
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git checkout mybranch
Switched to branch 'mybranch'
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git branch -v
--------------------------------------------------
master b68a2b0 添加了test4.txt文件
* mybranch b68a2b0 添加了test4.txt文件 //并且可以看到这里master和mybranch分支对应的版本号都是一样的
9. 分支冲突
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git status
On branch mybranch
nothing to commit, working tree clean
- 在当前分支mybranch下,修改test4文件,随意增加内容。
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ vi test4.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ head test4.txt
--------------------------------------------------
abc
啊啊啊啊,我是新增加的内容啊啊啊--by mybranch
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git add test4.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git commit -m "在分支mybranch中为test4.txt文件增加内容" test4.txt
-----------------------------------------------------------------
[mybranch dd8bd30] 在分支mybranch中为test4.txt文件增加内容
1 file changed, 1 insertion(+)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git checkout master
---------------------------------------------------
Switched to branch 'master'
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git branch -v // 此时发现master和mybranch版本号不一样了
-----------------------------------------------------
* master b68a2b0 添加了test4.txt文件
mybranch dd8bd30 在分支mybranch中为test4.txt文件增加内容
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ head test4.txt
abc
发现master中的内容并没有变
- 然后我们在主分支中增加test4.txt的内容,随意加,家的和mybranch不同。
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ vi test4.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ head test4.txt
------------------------------------------------------
abc
啊啊啊啊啊,这是在master分支中增加的内容啊--by master
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test4.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "在主分支中增加了内容" test4.txt
--------------------------------------------------------
[master d40dd49] 在主分支中增加了内容
1 file changed, 1 insertion(+)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git checkout mybranch
-----------------------------------------------------
Switched to branch 'mybranch'
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git branch -v
------------------------------------------------------
master d40dd49 在主分支中增加了内容 // 发现主分支的版本号也改变了
* mybranch dd8bd30 在分支mybranch中为test4.txt文件增加内容
上述操作说明,各自分支进行的开发,对其他分支都是不影响的。
9.1. 合并分支
目的:将mybranch分支合并到master主分支 步骤:
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch)
$ git checkout master
Switched to branch 'master'
- 将mybranch中的内容与master分支中的内容进行合并
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git merge mybranch
---------------------------------------------------------------
Auto-merging test4.txt
CONFLICT (content): Merge conflict in test4.txt // 发现合并时有冲突
Automatic merge failed; fix conflicts and then commit the result.
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master|MERGING) //这个标识就表示当前处于合并的状态中
$ cat test4.txt
------------------------------------------------------
abc
<<<<<<< HEAD // 当前分支修改的内容
啊啊啊啊啊,这是在master分支中增加的内容啊--by master
=======
啊啊啊啊,我是新增加的内容啊啊啊--by mybranch
>>>>>>> mybranch // 被合并的分支中修改的内容
思考:什么时候会出现冲突问题? 在同一个文件的同一个位置出现修改
冲突解决 公司团队内部之间讨论,留下哪一部分内容(需要人为决定),删除其他内容,最终在test4.txt文件中留下如下内容。(特殊符号什么的都要删掉)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master|MERGING) //这个标识就表示当前处于合并的状态中
$ cat test4.txt
------------------------------------------------------
abc
啊啊啊啊,我是新增加的内容啊啊啊--by mybranch
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master|MERGING)
$ git status
-------------------------------------------------------
On branch master
All conflicts fixed but you are still merging. // 仍处于合并状态中
(use "git commit" to conclude merge) // commit之后才能去除合并状态
Changes to be committed:
modified: test4.txt
- 进行commit操作 (不能带文件名),消除merge状态
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master|MERGING)
$ git commit -m "合并后的test4.txt" test4.txt // 此处不能带有文件名
----------------------------------------------------------------
fatal: cannot do a partial commit during a merge.
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master|MERGING)
$ git commit -m "解决了冲突问题" // commit之后就取消了合并状态
-----------------------------------------------------------
[master 25fcfb3] 解决了冲突问题
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
与远程库进行交互
10. 注册GitHub或Gitee
省略
11. 创建远程仓库
在github中手动创建,一般与本地仓库名称一样即可。一般由项目经理进行创建。
12. 将本地库的资源推送远程库
- 获取远程库地址
- HTTP地址
- https://github.com/ZhongAlexMrD/GitRepository.git
- SSH地址
- git@github.com:ZhongAlexMrD/GitRepository.git
远程库地址比较长,每次复制比较麻烦。Git支持本地将github地址通过别名进行保存。
- 保存远程地址为别名
- 查看别名
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git remote -v // 查看别名。发现目前没有任何别名
- 设置别名
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git remote add GitRpositoryAddress https://github.com/ZhongAlexMrD/GitRepository.git
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git remote -v
---------------------------------------------------
GitRpositoryAddress https://github.com/ZhongAlexMrD/GitRepository.git (fetch) // 表示可以从这个地址取回东西
GitRpositoryAddress https://github.com/ZhongAlexMrD/GitRepository.git (push) // 表示可以推送东西到这个地址
- 设置的别名仅限于本当前仓库
其中设置别名的语法是
git remote add [HTTP_alias] [HTTP_address] // 别名+地址
13. 推送操作(Push)
语法
git push [远程仓库地址/别名] [要推送的本地仓库的分支]
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git push GitRpositoryAddress master // 会弹出窗口要求登录github。国内登录有时候会遇到connect error的问题(被墙了)
----------------------------------------------------------
Enumerating objects: 48, done.
Counting objects: 100% (48/48), done.
Delta compression using up to 12 threads
Compressing objects: 100% (38/38), done.
Writing objects: 100% (48/48), 12.57 KiB | 1.57 MiB/s, done.
Total 48 (delta 15), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (15/15), done.
To https://github.com/ZhongAlexMrD/GitRepository.git
* [new branch] //表示在远程库创建了新的分支 master -> master // 表示将本地库的master分支推送到远程库的master分支
14. 克隆操作(clone)
远程库中已经有部分代码了,如果是团队开发的话,另一个人在进行本地开发之前,应该先将远程库中的代码克隆下来。
- 首先要选择克隆下来的文件要放在本地的哪个盘中(C盘还是D盘?)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d // 选择在d盘中进行克隆操作
$ git clone GitRpositoryAddress
-------------------------------------------------------------
fatal: repository 'GitRpositoryAddress' does not exist // 因为之前的GitRpositoryAddress别名是在GitRepository仓库下设置的,并不是全局设置,因此在GitRepository仓库之外应该是无法生效的。
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d
$ git remote -v // 只能在本地仓库中才能查看远程仓库地址。
-----------------------------------------------------------------
fatal: not a git repository (or any of the parent directories): .git
因此只能采用把远程库的http地址复制下来的方式。(另,国内访问github日常断网…)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d
$ git clone https://github.com/ZhongAlexMrD/GitRepository.git // 因此只能把http地址复制到这边
--------------------------------------------------------------------------
Cloning into 'GitRepository'...
remote: Enumerating objects: 48, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 48 (delta 15), reused 48 (delta 15), pack-reused 0
Receiving objects: 100% (48/48), 12.57 KiB | 2.51 MiB/s, done.
Resolving deltas: 100% (15/15), done.
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d
$ cd GitRepository
------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ ls
------------------------------------------------------
Demo02.txt git与github.md test2.txt test4.txt
GitDemo.txt test.txt test3.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git remote -v // 此时可以发现,这边的地址别名只是本仓库的别名。之前的GitRepositoryAddress并不会在这里面
---------------------------------------------------------
origin https://github.com/ZhongAlexMrD/GitRepository.git (fetch)
origin https://github.com/ZhongAlexMrD/GitRepository.git (push)
克隆操作可以帮我们完成三件事
- 初始化本地库
- 将远程库内容完整德克隆到本地库
- 自动创建远程库地址的别名
15. 要求加入团队(push)
团队B将从远程库拉取下来的代码,在本地开发后,再次推送到远程库(该远程库由团队A创建),加入团队
- 我们新建一个Demo2.txt文件,表示更新了团队B本地库的信息
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ touch Demo2.txt
-------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ vi Demo2.txt
-------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ head Demo2.txt
-------------------------------------------------------
我创建了Demo2.txt文件,是普通开发人员创建
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git add Demo2.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git commit -m "创建了Demo2.txt" Demo2.txt
[master 39aa3c0] 创建了Demo2.txt
1 file changed, 1 insertion(+)
create mode 100644 Demo2.txt
$ git push origin master
------------------------------------------------------
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 336 bytes | 336.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 >local object.
To https://github.com/ZhongAlexMrD/GitRepository.git
5adf5cb…39aa3c0 master -> master
发现直接就提交到团队A创建的远程库中了,没有任何验证,这是不对的。 这是因为这里用的一台机器提交结果,git在本地添加了缓存,直接取了缓存中的结果
- 下面删除git缓存
在windows搜索栏中搜索“管理你的凭据”,删除github凭据。然后再新建文件,再提交则需要验证.此时登录另一个账号B密码(远程库由账号A创建)。
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ touch Demo03.txt
------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git add .
----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git commit -m "新建Demo03.txt" Demo03.txt
------------------------------------------------------
[master 4e9c501] 新建Demo03.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Demo03.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git push origin master
remote: Permission to ZhongAlexMrD/GitRepository.git denied to DioxinMrD.
fatal: unable to access 'https://github.com/ZhongAlexMrD/GitRepository.git/': The requested URL returned error: 403
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git push origin master
-----------------------------------------------------------------
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 242 bytes | 242.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/ZhongAlexMrD/GitRepository.git
79cf8a8..26e6999 master -> master
16. 远程库修改的拉取pull
经过上述账号B的开发并提交远程库,对于账号A来说,远程库中的内容和账号A本地库中的内容是不一致的,因此账号A需要从远程库拉取内容到本地
16.1. 使用fetch + merge 操作代替pull操作
- 用账号A(一般项目经理)登录远程库的github,确认远程库中的内容是否进行了更新
- 账号A进行拉取操作
-
先是抓取操作 git fetch [远程库地址] [远程库分支]
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git fetch origin master // 该操作对于远程库来说是一个读取操作,因此不需要账号密码验证
--------------------------------------------------------------------------
From https://github.com/ZhongAlexMrD/GitRepository
* branch master -> FETCH_HEAD
在抓取操作执行之后,只是将远程库的内容下载到本地,但是工作区中的内容并没有更新 -
查看远程库的分支中具体是什么内容
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git checkout origin/master // 切换到origin/master分支
------------------------------------------------------
Note: switching to 'origin/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 26e6999 创建Demo03.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository ((26e6999...))
$ ll
-----------------------------------------------------
total 37
-rw-r--r-- 1 ZHD 197121 23 Nov 30 11:27 Demo02.txt
-rw-r--r-- 1 ZHD 197121 0 Dec 7 17:34 Demo03.txt
-rw-r--r-- 1 ZHD 197121 56 Dec 7 15:01 Demo2.txt
-rw-r--r-- 1 ZHD 197121 0 Nov 30 11:27 GitDemo.txt
-rw-r--r-- 1 ZHD 197121 32261 Nov 30 11:27 git与github.md
-rw-r--r-- 1 ZHD 197121 11 Nov 30 11:27 test.txt
-rw-r--r-- 1 ZHD 197121 0 Nov 30 11:27 test2.txt
-rw-r--r-- 1 ZHD 197121 10 Nov 30 11:27 test3.txt
-rw-r--r-- 1 ZHD 197121 89 Nov 30 11:27 test4.txt
-
验证远程库分支中的文件内容都正确,则可进行合并操作
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository ((26e6999...))
$ git checkout master
-------------------------------------------------------
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git merge origin/master
-----------------------------------------------------
Already up to date. // 因为我这边是拉取了和本地一模一样的内容到本地库
16.2. 使用pull命令拉取
git pull origin master
- 何时使用pull?何时使用fetch+merge?
- fetch+merge操作比较保险,用于代码比较重要的场合
- pull 用于代码比较简单,也不是很重要的场合,省事
git pull = git fetch + git merge FETCH_HEAD
git merge = git fetch + git rebase FETCH_HEAD
17. 删除github远程库
https://blog.csdn.net/xuchaoxin1375/article/details/110987951
18. 协作开发合作时冲突的解决办法
- 首先来人为制造下冲突,在账号B的本地库创建文件并提交
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ touch test20211209.txt
--------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ vi test20211209.txt
---------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ head test20211209.txt
----------------------------------------------------
aaaaaabc
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git add test20211209.txt
---------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git commit -m "创建test20211209.txt文件" test20211209.txt
-------------------------------------------------------
[master 2f7f42d] 创建test20211209.txt文件
1 file changed, 1 insertion(+)
create mode 100644 test20211209.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/ZhongAlexMrD/GitRepository.git
26e6999..2f7f42d master -> master
- 使用账号A进行拉取操作(因为拉取是读取操作,因此不需要登录)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git pull origin master
-----------------------------------------------------------
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 3), reused 5 (delta 1), pack-reused 0
Unpacking objects: 100% (7/7), 734 bytes | 40.00 KiB/s, done.
From https://github.com/ZhongAlexMrD/GitRepository
* branch master -> FETCH_HEAD
39aa3c0..2f7f42d master -> origin/master
Updating 39aa3c0..2f7f42d
Fast-forward
Demo03.txt | 0
test20211209.txt | 1 +
2 files changed, 1 insertion(+)
create mode 100644 Demo03.txt
create mode 100644 test20211209.txt
- 至此为止,远程合作没有问题
- 现在操作同一个文件的同一个位置的时候就会引起冲突
- 在账号A的本地库对其进行修改,并提交远程库
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ vi test20211209.txt
------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ head test20211209.txt
------------------------------------------------------
aaaaaabc
aaa - by 账号A
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add test20211209.txt
-----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "更新了test20211209.txt by 账号A" test20211209.txt
-------------------------------------------------------
[master 5e782fd] 更新了test20211209.txt by 账号A
1 file changed, 1 insertion(+)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git push origin master
---------------------------------------------------------
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 319 bytes | 319.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
error: RPC failed; curl 28 OpenSSL SSL_read: Connection was reset, errno 10054
send-pack: unexpected disconnect while reading sideband packet
fatal: the remote end hung up unexpectedly
Everything up-to-date
- 账号A是先修改的一方,所以提交没有问题
- 切换到账号B的本地库(删除电脑本地的凭据)
- 改动test20211209.txt的相同位置,然后进行推送
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ vi test20211209.txt
----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ head test20211209.txt
-----------------------------------------------------
aaaaaabc
bbbb - by 账号B
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git add test20211209.txt
-----------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git commit -m "更新了test20211209.txt by 账号B" test20211209.txt
-----------------------------------------------------
[master a0710cf] 更新了test20211209.txt by 账号B
1 file changed, 1 insertion(+)
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ git push origin master
---------------------------------------------------
To https://github.com/ZhongAlexMrD/GitRepository.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ZhongAlexMrD/GitRepository.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.
git pull origin master
- 人为解决这个冲突(同本地库解决冲突的方式)
- 解决完冲突后,账号B重新向远程库进行推送
git add .
git commit -m "解决了冲突" //解决冲突问题时,不可以带文件名,否则提交失败
git push origin master
19. 跨团队合作
两个不同团队进行合作。(前面一个案例是在同一个团队中,不同开发人员的合作)
- 团队A的项目经理Aa创建远程库A
- 团队B的人员取得远程库的地址,项目经理Bb登录github访问该地址,进行fork操作,得到远程库B
- 团队B将fork得到的远程库B,克隆到本地(clone的地址应该是远程库B的地址),得到本地库B
- 团队B成员对本地库B进行更改
touch test2.txt // 团队B对本地库B进行操作
head test2.txt
--------------------------
nnnn
git add .
git commit -m "团队B创建test2.txt" test2.txt
git push [远程库B的地址] master
- 团队B登录github,在github中手动进行pull request操作
- 团队B提交pull request操作后,等待团队A的审核
- 团队A登录github,项目经理Aa在github中点击“pull resquest”查看请求,进行审核操作。(可以互相留言)
- 在files changed里面可以查看具体更改的文件内容
- 没有问题,则进行merge pull request操作
- 合并完之后则会在团队A的远程库A中出现团队B修改的内容
20. SSH免密登录
对于同一个团队的远程库,每次向远程库提交内容都要输入账号密码。不过windows10系统自带有"管理凭据"功能,可以避免重复输入。如果不是windows10系统,则比较麻烦 解决方法:不使用HTTP地址提交,使用SSH地址提交。例如
git@github.com:ZhongAlexMrD/GitRepository.git
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master)
$ cd ~
ZHD@LAPTOP-PQIEGIQ8 MINGW64 ~
$ ssh-keygen -t rsa -C chinass@126.com // 这个邮箱必须是注册github的邮箱
----------------------------------------------------
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ZHD/.ssh/id_rsa): // 默认按三个回车
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/ZHD/.ssh/id_rsa
Your public key has been saved in /c/Users/ZHD/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:QJMay9znrHjHPfkMCY6lqtzSO8CxGiaS5FtVufaIJ9A 1603527940@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| o.. |
| ...+ |
| o.=o . |
| ...=E.+. |
|oo oo o+S |
|=o+. o Boo . |
|+ooo .=o..o. |
|.o..+.o o +o |
| oo++ . oo |
+----[SHA256]-----+
- 打开id_rsa.pub文件,对立面的内容进行复制操作
- 打开github账号,找到settings->ssh and GPG keys
- 生成密钥之后就可以正常进行push操作了
- 在本地库中为ssh地址取别名
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git remote add origin_ssh git@github.com:ZhongAlexMrD/GitRepository.git
--------------------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git remote -v
--------------------------------------------------------------------
origin https://github.com/ZhongAlexMrD/GitRepository.git (fetch)
origin https://github.com/ZhongAlexMrD/GitRepository.git (push)
origin_ssh git@github.com:ZhongAlexMrD/GitRepository.git (fetch)
origin_ssh git@github.com:ZhongAlexMrD/GitRepository.git (push)
- 下面测试一下
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ touch test100.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git add .
------------------------------------------------------
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git commit -m "创建文件test100.txt"
------------------------------------------------------
[master 767bd85] 创建文件test100.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test100.txt
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master)
$ git push origin_ssh master
------------------------------------------------------
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: git.zhlh6.cn
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 252 bytes | 252.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:ZhongAlexMrD/GitRepository.git
425cd90..767bd85 master -> master
ssh的优点: 不用每次提交都输入密码 ssh的缺点:只能针对一个账户
IDEA、Eclipse或者VS Code集成Git
这个比较简单,详情自行百度 可参考 https://blog.csdn.net/kuailexiaomeng/article/details/99604799
视频链接:https://www.bilibili.com/video/BV1go4y1D7Gd?p=29&spm_id_from=pageDriver
|