一、Git的概念
- Git是先进的分布式版本控制系统,而Github是常用的Git代码托管中心。
二、版本控制
版本控制工具
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ad3d8ddae2f2994789d3ad7d204c14e7.png)
- 分布式的版本控制工具,如GIt
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1b875adc0b6820d1483d85a3c7ee0e27.png)
- 【注】: 分布式的一大优势在于,可以避免“单点故障”,即中心服务器down机之后,版本历史信息就丢失了
Git的优势
![](https://i-blog.csdnimg.cn/blog_migrate/43f8f47e2a47ac24e263ea2c228d2391.png)
Git的分区
![](https://i-blog.csdnimg.cn/blog_migrate/a20e9e97b7bf0afbf10b4094033cbf1e.png)
Git和代码托管中心
![](https://i-blog.csdnimg.cn/blog_migrate/6185f12bae016860a9640195906af3a9.png)
本地库和远程库
![](https://i-blog.csdnimg.cn/blog_migrate/b35d22f95505fac349253ac7e152c089.png)
三、Git命令行操作
1.本地库操作
本地库初始化
$ cd GitPath/
$ git init
设置签名
![](https://i-blog.csdnimg.cn/blog_migrate/c5ccc9c70b05cf8decf6b295366b2a8b.png)
$ git config user.name wjtang
$ git config user.email 1254752330@qq.com
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = wjtang
email = 1254752330@qq.com
$ git config --global user.name wjtang_global
$ git config --global user.email 1254752330@qq.com_global
$ cd ~
$ ls -la | grep .git
-rw-r--r-- 1 wjtang staff 49 3 16 20:22 .gitconfig
$ cat .gitconfig
[user]
name = wjtang_global
email = 1254752330@qq.com_global
查看状态
$ cd GitPath/
$ git status
添加
$ git add good.txt
提交
$ git commit good.txt
查看历史
$ git log
$ git log --oneline
$ git reflog
版本前进后退
- 本质: 调整HEAD指针
- 方式:基于索引值操作(推荐);使用^符号;使用~符号
$ git reflog
![](https://i-blog.csdnimg.cn/blog_migrate/1f9884ca86db98427b1b835e4fbf663b.png)
$ git reset --hard 7a867b0
$ git reset --hard HEAD^
$ git reset --hard ~3
--soft
--mixed
--hard
删除文件后找回
$ rm test
$ git add test
$ git commit -m "delete test" test
$ git reset --hard HEAD^
比较文件差异
git diff apple.txt
git diff HEAD apple.txt
git diff HEAD^ apple.txt
git diff
分支操作
- 概念:版本控制过程中,多条线同时推进多个任务
- 好处:
- 多个分支,允许试错;
- 并行推进,提高效率;
![](https://i-blog.csdnimg.cn/blog_migrate/28a39befd49088f1617122f4beade9f3.png)
具体操作
git branch -v
git branch hot_fix
git checkout hot_fix
git checkout master
git merge hot_fix
处理合并冲突
![](https://i-blog.csdnimg.cn/blog_migrate/5967cfc7dc3ac42250934584b55b019e.png)
git add good.txt
git commit -m "cope with the confict"# 3. git commit 注意,这里不用加文件名;解决冲突之后commit不用加文件名
四、Git基本原理
1.Hash
2.文件管理机制
集中式版本控制
![](https://i-blog.csdnimg.cn/blog_migrate/ce8ae7483c2de050971ff6faee1bb6d3.png)
分布式版本控制
![](https://i-blog.csdnimg.cn/blog_migrate/96812697d02f2ba66f6fc66e7caeaa8c.png)
Git文件管理细节
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uN4tMqBI-1616313145449)(/Users/wjtang/Library/Application Support/typora-user-images/image-20210318165550896.png)]](https://i-blog.csdnimg.cn/blog_migrate/be4db1dd1e3a303393297e2eb5c953eb.png)
3.分支管理机制
- Git新建分支时,只是新建了指针,而非复制一遍目录和文件,效率得到了很大的提升;
- HEAD进行版本前进、回退时,也只是移动了指针;
五、GitHub
Git和代码托管
![](https://i-blog.csdnimg.cn/blog_migrate/4197c5a9ac5b9914371dd1360bb969df.png)
本地库与远程库交互
GitHub创建远程库
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fR6VLZ0v-1616313145451)(/Users/wjtang/Library/Application Support/typora-user-images/image-20210321132036001.png)]](https://i-blog.csdnimg.cn/blog_migrate/3309a04ec05ccf29771f24a615dcfb27.png)
创建本地库
mkdir huashan
cd huashan
git init
vim huashanjianfa.txt
本地库与远程库交互
git remote add origin https://github.com/wjtang123/huashan.git
git remote -v
git add huashanjianfa.txt
git commit -m "Test interaction" huashanjianfa.txt
git push origin master
git clone https://github.com/wjtang123/huashan.git
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NOwsk8VM-1616313145453)(/Users/wjtang/Library/Application Support/typora-user-images/image-20210321140207714.png)]](https://i-blog.csdnimg.cn/blog_migrate/381ea5d959d457a58279182c11e9449e.png)
vim huashan/huashanjianfa.txt
cd huashan/
git add huashanjianfa.txt
git commit -m "令狐冲做的修改" huashanjianfa.txt
git push origin master
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xjpyv7Jf-1616313145454)(/Users/wjtang/Library/Application Support/typora-user-images/image-20210321142018028.png)]](https://i-blog.csdnimg.cn/blog_migrate/0441552950578c47b0e131a07bba474c.png)
- Pull 操作; pull = fetch + merge
git fetch origin master
git checkout origin/master
git merge origin/master
git pull origin master
多个远程库之间的交互
fork
![](https://i-blog.csdnimg.cn/blog_migrate/406057209fcbeb7b23e3d0714b9fe113.png)
Pull request
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k42gwubN-1616313145455)(/Users/wjtang/Library/Application Support/typora-user-images/image-20210321151609441.png)]](https://i-blog.csdnimg.cn/blog_migrate/aae37e0f3eac72740bb623f5ca0d1797.png)
- 【注】:IDE中的项目如果使用Git,有的文件不需要提交的,记得忽视