git add:从工作区提交到暂存区 git commit:从暂存区提交到本地库
代码托管中心的任务是帮我们维护远程库
本地库和远程库的交互方式
// 查看git 安装的版本 git --version //清屏操作 clear // 设置签名。 // 设置用户名和邮箱 git config --global user.name "ZhongHD" //设置全局变量:用户名 git config --global user.email "[email protected]" // 本地仓库的初始化 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
展示:
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository/.git (GIT_DIR!) $ git add ~/Desktop/GitDemo.txt ---------------------------------------------- fatal: this operation must be run in a work tree
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
【注意事项】
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
..." 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
..." 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
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
..." to update what will be committed) // 可以对暂存区的文件进行更新 (use "git restore..." 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
..." 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
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ git log // git会从近到远展示日志信息 ------------------------------------------------------- commit ab87f9c5f8eae46e8383bdf2a713eaa7f1e18474 (HEAD -> master) Author: ZhongHD <16***[email protected]> Date: Tue Nov 16 16:51:11 2021 +0800 修改了文件Demo02.txt中的内容 commit 756b2d2bacdbff0ee08068038de0e13550c82a1e // 表示当前历史记录对应的索引(key) Author: ZhongHD <16***[email protected]> Date: Tue Nov 16 16:34:22 2021 +0800 // 当前历史记录对应的内容(value) 这是我提交的第二个命令 commit 5ac02b04b9d378992296284b85af246ef83b4faf Author: ZhongHD <16***[email protected]> Date: Tue Nov 16 10:55:14 2021 +0800 这是我提交的第一个文件GitDemo.txt
reset命令:前进或后退历史版本
git reset --hard [索引数]
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
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
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ tail test.txt aaa
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ git reset --hard 9c5c623 HEAD is now at 9c5c623 insert ccc
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
..." to update what will be committed) (use "git restore..." 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
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
..." 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
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ git reset --hard HEAD HEAD is now at 3471fae 添加test2.txt文件
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
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ vi test3.txt ------------------------------------------------- ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ tail test3.txt aaaabbbbb
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
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
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
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(-)
此时再来审视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的比较本质上是工作区和暂存区或者本地库的比较
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分支对应的版本号都是一样的
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch) $ git status On branch mybranch nothing to commit, working tree clean
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中的内容并没有变
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文件增加内容
上述操作说明,各自分支进行的开发,对其他分支都是不影响的。
目的:将mybranch分支合并到master主分支
步骤:
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (mybranch) $ git checkout master Switched to branch '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
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)
省略
在github中手动创建,一般与本地仓库名称一样即可。一般由项目经理进行创建。
远程库地址比较长,每次复制比较麻烦。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] // 别名+地址
语法
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分支
远程库中已经有部分代码了,如果是团队开发的话,另一个人在进行本地开发之前,应该先将远程库中的代码克隆下来。
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)
克隆操作可以帮我们完成三件事
团队B将从远程库拉取下来的代码,在本地开发后,再次推送到远程库(该远程库由团队A创建),加入团队
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在本地添加了缓存,直接取了缓存中的结果
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
邀请加入团队
接受邀请
然后再重新push,,登录账号B,发现可以成功提交了
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
经过上述账号B的开发并提交远程库,对于账号A来说,远程库中的内容和账号A本地库中的内容是不一致的,因此账号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. // 因为我这边是拉取了和本地一模一样的内容到本地库
git pull origin master
git pull = git fetch + git merge FETCH_HEAD git merge = git fetch + git rebase FETCH_HEAD
https://blog.csdn.net/xuchaoxin1375/article/details/110987951
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
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
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
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.
在冲突的情况下,应该先拉取下来,然后修改冲突,然后再推送到远程库
账号B进行远程库的拉取操作
git pull origin master
git add . git commit -m "解决了冲突" //解决冲突问题时,不可以带文件名,否则提交失败 git push origin master
两个不同团队进行合作。(前面一个案例是在同一个团队中,不同开发人员的合作)
touch test2.txt // 团队B对本地库B进行操作 head test2.txt -------------------------- nnnn git add . git commit -m "团队B创建test2.txt" test2.txt git push [远程库B的地址] master
对于同一个团队的远程库,每次向远程库提交内容都要输入账号密码。不过windows10系统自带有"管理凭据"功能,可以避免重复输入。如果不是windows10系统,则比较麻烦
解决方法:不使用HTTP地址提交,使用SSH地址提交。例如
[email protected]:ZhongAlexMrD/GitRepository.git
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /d/GitRepository (master) $ cd ~
ZHD@LAPTOP-PQIEGIQ8 MINGW64 ~ $ ssh-keygen -t rsa -C [email protected] // 这个邮箱必须是注册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]-----+
ZHD@LAPTOP-PQIEGIQ8 MINGW64 /GitRepository (master) $ git remote add origin_ssh [email protected]: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 [email protected]:ZhongAlexMrD/GitRepository.git (fetch) origin_ssh [email protected]: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的缺点:只能针对一个账户
这个比较简单,详情自行百度
可参考 https://blog.csdn.net/kuailexiaomeng/article/details/99604799
视频链接:https://www.bilibili.com/video/BV1go4y1D7Gd?p=29&spm_id_from=pageDriver