1 Git 概述
Git是一个免费的、开源的分布式版本控制系统。公司里面是关联代码的工具。
1.1 何为版本控制
版本控制是一种记录文件内容变化,以便将来查阅特定版本修订情况的系统。
1.2 版本控制工具
- 集中式版本控制工具
- 分布式版本控制工具
- 分布式的版本控制系统出现之后,解决了集中式版本控制系统的缺陷:
- 服务器断网的情况下也可以进行开发(因为版本控制是在本地进行的)
-
- 每个客户端保存的也都是整个完整的项目(包含历史记录,更加安全)
- 分布式的版本控制系统出现之后,解决了集中式版本控制系统的缺陷:
1.3 Git工作机制
- 工作区:进行剪辑,可删可减
- 暂存区:可以删
- 本地库:不能删
1.4 Git和代码托管中心
代码托管中心是基于网络服务器的远程代码仓库,一般我们简单称为远程库。
2 Git安装
- 安装
- 打开
CZyue@LAPTOP-K3L5FM3D MINGW64 ~/Desktop
$ git --version
git version 2.37.2.windows.2
3 常用命令
3.1 命令
3.2 设置用户签名
CZyue@LAPTOP-K3L5FM3D MINGW64 ~/Desktop
$ git config --global user.name fenfen
CZyue@LAPTOP-K3L5FM3D MINGW64 ~/Desktop
$ git config --global user.email yqlmjhckn@163.com
3.3 初始化本地库
- 直接在本地右击调出git bash
- 初始化
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo
$ git init
Initialized empty Git repository in D:/学习/专业学习/3-基本技能类/Git/git demo/.git/
- 自动生成.git目录
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (master)
$ ll -a
total 4
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 ./
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:50 ../
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 .git/
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (master)
$ cd .git/
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo/.git (GIT_DIR!)
$ ll -a
total 11
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 ./
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 ../
-rw-r--r-- 1 CZyue 197121 23 Aug 30 19:53 HEAD
-rw-r--r-- 1 CZyue 197121 130 Aug 30 19:53 config
-rw-r--r-- 1 CZyue 197121 73 Aug 30 19:53 description
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 hooks/
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 info/
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 objects/
drwxr-xr-x 1 CZyue 197121 0 Aug 30 19:53 refs/
3.4 查看本地库状态
- 查看git本地库状态
- 新建一个文本后再看下状态
# 创建一个文件
$ vim hello.txt
$ ll
total 1
-rw-r--r-- 1 CZyue 197121 198 Aug 30 20:06 hello.txt
$ cat hello.txt
hello Git!
hello Git!
hello Git!
hello Git!
hello Git!
# 再次看下git本地库状态,有可以提交的了
$ git status
On branch master
No commits yet
Untracked files: # git还未追踪过的文件
(use "git add <file>..." to include in what will be committed)
hello.txt
3.5 添加暂存区
- 提交看看
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
# 警告是在说我帮你自动转换换行符啦
- 再看下git本地库状态
$ git status
On branch master
No commits yet
Changes to be committed: # 只是提交到了暂存区
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
- 删除暂存区文件
$ git rm --cached hello.txt # 只是删了暂存区,工作区还是有的
rm 'hello.txt'
3.6 提交本地库
- 提交到本地库
$ git commit -m "first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 21fb5ab] first commit # 主干master分支第一次提交
1 file changed, 18 insertions(+) # 一个文件被改变,16行文件被插入
create mode 100644 hello.txt
- 看下git本地库状态
$ git status
On branch master
nothing to commit, working tree clean
- 查看版本信息命令
$ git reflog
21fb5ab (HEAD -> master) HEAD@{0}: commit (initial): first commit # 有一个版本first commit
或者
$ git log
commit 21fb5ab3759730b817bbe240eccf17d18114b6e4 (HEAD -> master)
Author: fenfen <yqlmjhckn@163.com>
Date: Tue Aug 30 19:58:37 2022 +0800
first commit
3.7 修改文件
- 修改文件
$ vim hello.txt
hello world!!!
hello Git!
hello Git!
- 修改后的本地库
$ 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: hello.txt # 被修改过的文件
no changes added to commit (use "git add" and/or "git commit -a")
- 把修改后的文件再次提交到暂存区
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
- 提交本地库
$ git commit -m "second commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master 6e1f647] second commit
1 file changed, 1 insertion(+) # git按照行维护文件的
$ git status
On branch master
nothing to commit, working tree clean
- 查看下版本信息
$ git reflog
6e1f647 (HEAD -> master) HEAD@{0}: commit: second commit
21fb5ab HEAD@{1}: commit (initial): first commit
或者
$ git log
commit 6e1f647416a9fe798465821faebfe2366897586f (HEAD -> master)
Author: fenfen <yqlmjhckn@163.com>
Date: Tue Aug 30 20:37:35 2022 +0800
second commit
commit 21fb5ab3759730b817bbe240eccf17d18114b6e4
Author: fenfen <yqlmjhckn@163.com>
Date: Tue Aug 30 20:58:37 2022 +0800
first commit
- 查看最新代码
$ cat hello.txt
hello world!!!
hello Git!
hello Git!
- 第三次提交后的代码
$ cat hello.txt
hello world!!!
hello dear!!
hello Git!
hello Git!
- 文件显示
从始至终,显示的也是只有一个文件,因为不是靠副本控制的,底层靠的是head指针控制的
3.8 历史版本
- 查看版本
$ git reflog
$ git log
- 版本穿梭
- 复制版本号
$ git reset --hard 6e1f647
HEAD is now at 6e1f647 second commit
- 查看版本状态
$ git reflog
6e1f647 (HEAD -> master) HEAD@{0}: reset: moving to 6e1f647 # 日志记下来版本穿梭
148629d HEAD@{1}: commit: third commit
6e1f647 (HEAD -> master) HEAD@{2}: commit: second commit # 指针就跑到这边来了
21fb5ab HEAD@{3}: commit (initial): first commit
- 查看代码,发现回到了第二次
$ cat hello.txt
hello world!!!
hello Git
4 分支
4.1概述和优点
4.2 分支的操作
- 查看分支
$ git branch -v
* master 6e1f647 second commit # 确实只有一个分支
- 创建分支
- 语法
git branch 分支名
- 案例
$ git branch hot-fix
# 再次查看分支,有两
$ git branch -v
hot-fix 6e1f647 second commit
* master 6e1f647 second commit
- 修改分支
- 语法
git checkout 分支名
- 修改分支案例
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (master) #最后master说明是在master上面的
$ git checkout hot-fix # 切换下分支
Switched to branch 'hot-fix'
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (hot-fix) #切换好了,就在hot—fix上面了
# 再查看下分支,*在hot-fix上了
$ git branch -v
* hot-fix 6e1f647 second commit
master 6e1f647 second commit
- 分支上修改文件并上传
# 修改代码
$ vim hello.txt
hello world!!! 22222
hello Git 33333
# 看下本地库状态
$ git status
On branch hot-fix
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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
# 提交到暂存区
$ git add hello.txt
$ git status
On branch hot-fix
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
# 提交本地库
$ git commit -m "hot-fix first commit" hello.txt
[hot-fix 77561c6] hot-fix first commit
1 file changed, 2 insertions(+), 2 deletions(-)
# 看下最新代码
$ cat hello.txt
hello world!!! 22222
hello Git! 33333
# 看下版本情况
$ git reflog
77561c6 (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix first commit
6e1f647 (master) HEAD@{1}: checkout: moving from master to hot-fix
6e1f647 (master) HEAD@{2}: reset: moving to 6e1f647
148629d HEAD@{3}: commit: third commit
6e1f647 (master) HEAD@{4}: commit: second commit
21fb5ab HEAD@{5}: commit (initial): first commit
- 合并分支
- 语法
git merge 分支名
- 案例
# 必须站在master上合并才可以
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (hot-fix)
$ git checkout master
Switched to branch 'master'
# 看眼代码
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (master) # 代码恢复到原来的样子了
$ cat hello.txt
hello world!!!
hello Git!
# 合并了!
$ git merge hot-fix
Updating 6e1f647..77561c6
Fast-forward
hello.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
# 这时再去看代码,OHHHHH
$ cat hello.txt
hello world!!! 22222
hello Git! 33333
- 冲突合并
- 场景:合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改
# master修改代码并提交
$ vim hello.txt
hello world!!! 22222 master test
hello Git! 33333
$ git add hello.txt
$ git commit -m "master test" hello.txt
[master 883cb97] master test
1 file changed, 1 insertion(+), 1 deletion(-)
$ cat hello.txt
hello world!!! 22222 master test
hello Git! 33333
# 切换到hot-fix分支
$ git checkout hot-fix
Switched to branch 'hot-fix'
$ vim hello.txt
hello world!!! 22222
hello Git! 33333 hot-fix test
$ git add hello.txt
$ git commit -m "hot-fix test" hello.txt
[hot-fix 85399a7] hot-fix test
1 file changed, 1 insertion(+), 1 deletion(-)
# 切换分支到master进行和合并
$ git checkout master
Switched to branch 'master'
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result. # 失败了,发生了合并代码冲突
- 手动合并代码
# 打开文件看看先
$ cat hello.txt
<<<<<<< HEAD # 当前分支代码
hello world!!! 22222 master test
hello Git! 33333
=======
hello world!!! 22222 # 别的分支
hello Git! 33333 hot-fix test
>>>>>>> hot-fix
# 手动改吧
$ vim hello.txt
hello world!!! 22222 master test
hello Git! 33333 hot-fix test
# 保存后还得提交一次
$ git add hello.txt
$ git commit -m "merge tese" hello.txt #但是不能这么提交,文件名不要
fatal: cannot do a partial commit during a merge.
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学/专业学习/3-基本技能类/Git/git demo (master|MERGING)
$ git commit -m "merge tese"
[master 8159946] merge tese
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo (master) #合并完是没有merging的后缀的
$ cat hello.txt
hello world!!! 22222 master test
hello Git! 33333 hot-fix test
# 切回hot-fix还是没修改,只有master会修改,不信切回去看
$ git checkout hot-fix
Switched to branch 'hot-fix'
$ cat hello.txt #真没变
hello world!!! 22222
hello Git! 33333 hot-fix test
5 Git 团队协作机制
5.1 团队内协作
5.2 跨团队协作
6 GitHub操作
6.1 创建远程库和别名
-
上官网创建
-
给链接起个别名,
$ git remote add git-demo https://github.com/fenfenya/git-demo.git
$ git remote -v
git-demo https://github.com/fenfenya/git-demo.git (fetch)
git-demo https://github.com/fenfenya/git-demo.git (push)
6.2 推送本地库代码到远程库
- 案例
# 老是推不上去,先执行
git config --global http.sslVerify false
# 还不行把windows映射把GitHub的ip和网址写进去
# 推下代码
$ git push git-demo master
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
remote: Resolving deltas: 100% (6/6), done.
- 刷下官网,有了!
6.3 拉取远程库到本地
- 在官网把远程库代码改几句
- 拉去远程库代码拉到本地
- 拉一下
$ git pull git-demo master
hello.txt | 17 -----------------
1 file changed, 17 deletions(-)
- 拉好看下本地库和代码
# 拉取好的代码会自动帮你提交本地库
$ git status
On branch master
nothing to commit, working tree clean
# 查看代码
$ cat hello.txt
hello world!!! 22222 master test
hello Git! 33333 hot-fix test
6.4 克隆远程库到本地
- 上官网找一个github用户复制链接
- 案例
# 克隆
$ git clone https://github.com/Janusec/Application-Gateway.git
# 查看克隆后的消息
$ ll
total 5
drwxr-xr-x 1 CZyue 197121 0 Aug 31 20:25 Application-Gateway/
-rw-r--r-- 1 CZyue 197121 72 Aug 30 22:01 hello.txt
# 进去看看
$ cd Application-Gateway/
#连别名都有了 以orgion开头
$ git remote -v
origin https://github.com/Janusec/Application-Gateway.git (fetch)
origin https://github.com/Janusec/Application-Gateway.git (push)
6.5 团队协作
- 修改代码
- 然后记得添加暂存区并且提交本地库
- 提交远程库
$ git push https://github.com/Janusec/Application-Gateway.git master
- 但是要是一个团队才可以推送代码进去
邀请函链接一般通过沟通工具给到你,复制链接同意即可
- 被修改后的代码邀请者查看需要拉取到本地库
6.6 跨团队协作
- 查询
- 左上角可以查项目
- 精准定位账号
- fork到自己的github,就能对fork过来的代码进行修改了
- 自己的账号就显示是从别人那边fork来的了
- 也可以直接克隆本地改,fork可以在线改并且提交(只是自己库中的会变)
- 代码本人怎么看见修改呢?需要我们发起pull request请求
- 代码本人看到了,觉得好,就merge合并到自己的代码库中
6.7 SSH免密登录
- 添加ssh
- 来到C:\Users\CZyue右击bash here生成
- 使用ssh命令(非加密协议算法)生成
# rsa是非对称加密协议
$ ssh-keygen -t rsa -C yqlmjhckn@163.com
# $ ll
total 5
-rw-r--r-- 1 CZyue 197121 2602 Sep 1 21:24 id_rsa
-rw-r--r-- 1 CZyue 197121 571 Sep 1 21:24 id_rsa.pub
# 找到公钥
$ cat id_rsa.pub
ssh-rsa xxxxxxxxxxxxxxxxxxxx
- 上github官网关联ssh公钥
- 使用ssh登录拉取代码
#用ssh链接拉取代码
$ git pull git@github.com:fenfenya/git-demo.git master
Updating fb8cafd..b9b9d6b
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
# 看下代码变化率了
$ cat hello.txt
hello world!!! 22222 master test
hello Git! 33333 hot-fix test
hello dear! 44444 ssh test
- 使用ssh链接更新代码上传
# 修改代码
$ vim hello.txt
hello world!!! 22222 master test
hello Git! 33333 hot-fix test
hello dear! 44444 ssh pull test
hello sun! 55555 ssh push test
# 提交暂存区和远程库
$ git add hello.txt
$ git commit -m "ssh push test commit" hello.txt
[master 4d31da4] ssh push test commit
1 file changed, 2 insertions(+), 1 deletion(-)
# push 代码
$ git push git@github.com:fenfenya/git-demo.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
- 去网页端检查下变了
7 IDEA 集成Git
7.1 环境准备
- 在C:\Users\CZyue创建一个git.ignore,并放入需要忽略的文件后缀
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see
http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.classpath
.project
.settings
target
.idea
*.iml
- .gitcomfig中加入core配置
[user]
name = fenfen
email = yqlmjhckn@163.com
[http]
sslVerify = false
[core]
excludesfile = C:/Users/asus/git.ignore
- 在IDEA中定位git程序
- 创建一个maven工程
- 打开pom,然后打开setting
7.2 初始化、添加、提交
- 创建
- 在IDEA最上面
- 此时再看目录就有
- 提交暂存区,真香
- 写些代码上传看看
- 建个包、类
- 跳出问你要不要把这个添加到暂存区的提示,一般允许,现在选否,方便演示
- 写串代码
- 要是嫌一个一个添加类,我直接整个项目添加到暂存区
- 然后提交到本地库,初始化后变黑
7.3 切换代码版本
- 修改代码后变成蓝色,表示被追踪过且修改
-
右击项目提交即可
-
查看版本信息
- 切换版本
-
右击切换
-
那个黄色的就是指针了
7.4 创建分支&切换分支
- 创建分支
- 或者直接右击就有新建分支
- 切换分支
- 或者
7.5 合并分支
- 切换hot-fix分支然后修改代码
- 切换为master分支后依然发现master还是原来的代码
- 合并分支
7.6 代码冲突
- master分支改下代码
- hot-fix分支也改一下代码
- 合并分支
- 会出现手动合并代码的框框
- 这两个能点
7.7 设置GitHub账号并且分享项目到GitHub
- 设置Github账号
- IDEA集成GitHub
- 去GitHub看下
7.8 推送代码到远程库
- 修改代码并提交本地库
- 提交到远程库
- 或者
- 使用ssh链接push
-
复制ssh链接
-
选择自定义远程连接
- 粘贴ssh链接
7.9 拉取远程库代码到本地
- 小习惯
- push前要pull一下
- 本地代码尽量别动,不然会涉及到手动解决冲突的情况
- 拉取代码
7.10 克隆代码
8 国内代码托管中心-码云
8.1 账号注册&创建远程库
8.2 IDEA集成Gitee码云(pull和push)
- 安装一个码云插件
- 添加账号
- 分享项目到Gitee
- push代码到Gitee
- 去Gitee看下,有了
- 更改代码后pull到IDEA
8.3 导入GitHub项目
- 粘贴github链接然后导入
- 如若github更新了,gitee需要刷新后更新
- 点击项目名字后面的更新即可
- 刷新后查看代码显示最新
9 自建代码托管平台-GitLab
略