手把手教你Git 入门到精通

在这里插入图片描述
在这里插入图片描述

1 Git 概述

Git是一个免费的、开源的分布式版本控制系统。公司里面是关联代码的工具。

1.1 何为版本控制

版本控制是一种记录文件内容变化,以便将来查阅特定版本修订情况的系统。

1.2 版本控制工具

  • 集中式版本控制工具
  • 分布式版本控制工具
    • 分布式的版本控制系统出现之后,解决了集中式版本控制系统的缺陷:
      • 服务器断网的情况下也可以进行开发(因为版本控制是在本地进行的)
        1. 每个客户端保存的也都是整个完整的项目(包含历史记录,更加安全)

在这里插入图片描述

1.3 Git工作机制

在这里插入图片描述

  • 工作区:进行剪辑,可删可减
  • 暂存区:可以删
  • 本地库:不能删

1.4 Git和代码托管中心

代码托管中心是基于网络服务器的远程代码仓库,一般我们简单称为远程库。

2 Git安装

  1. 安装
  2. 打开

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Nr3oNNHf-1662641608631)(C:\Users\CZyue\AppData\Roaming\Typora\typora-user-images\image-20220830161940823.png)]

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 初始化本地库

  1. 直接在本地右击调出git bash
  2. 初始化
CZyue@LAPTOP-K3L5FM3D MINGW64 /d/学习/专业学习/3-基本技能类/Git/git demo
$ git init
Initialized empty Git repository in D:/学习/专业学习/3-基本技能类/Git/git demo/.git/
  1. 自动生成.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 查看本地库状态

  1. 查看git本地库状态

在这里插入图片描述

  1. 新建一个文本后再看下状态
# 创建一个文件
$  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 添加暂存区

  1. 提交看看
$  git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it  
# 警告是在说我帮你自动转换换行符啦
  1. 再看下git本地库状态
$ git status
On branch master

No commits yet

Changes to be committed:			# 只是提交到了暂存区
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt
  1. 删除暂存区文件
$ git rm --cached hello.txt			# 只是删了暂存区,工作区还是有的
rm 'hello.txt'

3.6 提交本地库

  1. 提交到本地库
$ 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
  1. 看下git本地库状态
$ git status
On branch master
nothing to commit, working tree clean
  1. 查看版本信息命令
$ 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 修改文件

  1. 修改文件
$  vim hello.txt
hello world!!!
hello Git!
hello Git!
  1. 修改后的本地库
$ 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")
  1. 把修改后的文件再次提交到暂存区
$ 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
  1. 提交本地库
$ 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
  1. 查看下版本信息
$ 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
  1. 查看最新代码
$ cat hello.txt
hello world!!!
hello Git!
hello Git!
  1. 第三次提交后的代码
$ cat hello.txt
hello world!!!
hello dear!!
hello Git!
hello Git!
  1. 文件显示

​ 从始至终,显示的也是只有一个文件,因为不是靠副本控制的,底层靠的是head指针控制的

在这里插入图片描述

3.8 历史版本

  1. 查看版本
$ git reflog
$ git log
  1. 版本穿梭
  • 复制版本号
$ 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 分支的操作

  1. 查看分支
$ git branch -v
* master 6e1f647 second commit	# 确实只有一个分支
  1. 创建分支
  • 语法
git branch 分支名
  • 案例
$ git branch hot-fix

# 再次查看分支,有两
$ git branch -v
  hot-fix 6e1f647 second commit
* master  6e1f647 second commit
  1. 修改分支
  • 语法
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
  1. 合并分支
  • 语法
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
  1. 冲突合并
  • 场景:合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改
# 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 创建远程库和别名

  1. 上官网创建

  2. 给链接起个别名,

$ 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 推送本地库代码到远程库

  1. 案例
# 老是推不上去,先执行
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 拉取远程库到本地

  1. 在官网把远程库代码改几句
  2. 拉去远程库代码拉到本地
  • 拉一下
$ 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 克隆远程库到本地

  1. 上官网找一个github用户复制链接
  2. 案例
# 克隆
$ 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 团队协作

  1. 修改代码
  2. 然后记得添加暂存区并且提交本地库
  3. 提交远程库
$ git push https://github.com/Janusec/Application-Gateway.git master
  1. 但是要是一个团队才可以推送代码进去

在这里插入图片描述

邀请函链接一般通过沟通工具给到你,复制链接同意即可

  1. 被修改后的代码邀请者查看需要拉取到本地库

6.6 跨团队协作

  1. 查询
  • 左上角可以查项目

在这里插入图片描述

  • 精准定位账号

在这里插入图片描述

  • fork到自己的github,就能对fork过来的代码进行修改了

在这里插入图片描述

  • 自己的账号就显示是从别人那边fork来的了

  • 也可以直接克隆本地改,fork可以在线改并且提交(只是自己库中的会变)

在这里插入图片描述

  • 代码本人怎么看见修改呢?需要我们发起pull request请求

在这里插入图片描述

在这里插入图片描述

  • 代码本人看到了,觉得好,就merge合并到自己的代码库中

在这里插入图片描述

6.7 SSH免密登录

  1. 添加ssh
  • 来到C:\Users\CZyue右击bash here生成

在这里插入图片描述

  1. 使用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
  1. 上github官网关联ssh公钥

在这里插入图片描述

在这里插入图片描述

  1. 使用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
  1. 使用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 环境准备

  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
  1. .gitcomfig中加入core配置
[user]
	name = fenfen
	email = yqlmjhckn@163.com
[http]
	sslVerify = false

[core]
	excludesfile = C:/Users/asus/git.ignore
  1. 在IDEA中定位git程序
  • 创建一个maven工程

在这里插入图片描述

在这里插入图片描述

  • 打开pom,然后打开setting

在这里插入图片描述

在这里插入图片描述

7.2 初始化、添加、提交

  1. 创建
  • 在IDEA最上面

在这里插入图片描述

在这里插入图片描述

  • 此时再看目录就有

在这里插入图片描述

  1. 提交暂存区,真香

在这里插入图片描述

  1. 写些代码上传看看
  • 建个包、类

在这里插入图片描述

  • 跳出问你要不要把这个添加到暂存区的提示,一般允许,现在选否,方便演示

在这里插入图片描述

  • 写串代码

在这里插入图片描述

  • 要是嫌一个一个添加类,我直接整个项目添加到暂存区

在这里插入图片描述

  • 然后提交到本地库,初始化后变黑

在这里插入图片描述

在这里插入图片描述

7.3 切换代码版本

  1. 修改代码后变成蓝色,表示被追踪过且修改

在这里插入图片描述

  1. 右击项目提交即可

  2. 查看版本信息

在这里插入图片描述

  1. 切换版本
  • 右击切换

  • 那个黄色的就是指针了

在这里插入图片描述

7.4 创建分支&切换分支

  1. 创建分支

在这里插入图片描述

  • 或者直接右击就有新建分支

在这里插入图片描述

  1. 切换分支

在这里插入图片描述

  • 或者

在这里插入图片描述

7.5 合并分支

  1. 切换hot-fix分支然后修改代码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. 切换为master分支后依然发现master还是原来的代码

在这里插入图片描述

  1. 合并分支

在这里插入图片描述

7.6 代码冲突

  1. master分支改下代码
  2. hot-fix分支也改一下代码

在这里插入图片描述

  1. 合并分支

在这里插入图片描述

在这里插入图片描述

  • 会出现手动合并代码的框框

在这里插入图片描述

  • 这两个能点

在这里插入图片描述

在这里插入图片描述

7.7 设置GitHub账号并且分享项目到GitHub

  1. 设置Github账号

在这里插入图片描述

  1. IDEA集成GitHub

在这里插入图片描述

在这里插入图片描述

  1. 去GitHub看下

在这里插入图片描述

7.8 推送代码到远程库

  1. 修改代码并提交本地库

在这里插入图片描述

  1. 提交到远程库

在这里插入图片描述

  • 或者

在这里插入图片描述

  1. 使用ssh链接push

在这里插入图片描述

  • 复制ssh链接

  • 选择自定义远程连接

在这里插入图片描述

  • 粘贴ssh链接

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

7.9 拉取远程库代码到本地

  1. 小习惯
  • push前要pull一下
  • 本地代码尽量别动,不然会涉及到手动解决冲突的情况
  1. 拉取代码

在这里插入图片描述

在这里插入图片描述

7.10 克隆代码

在这里插入图片描述

在这里插入图片描述

8 国内代码托管中心-码云

8.1 账号注册&创建远程库

在这里插入图片描述

8.2 IDEA集成Gitee码云(pull和push)

  1. 安装一个码云插件

在这里插入图片描述

  1. 添加账号

在这里插入图片描述

在这里插入图片描述

  1. 分享项目到Gitee

在这里插入图片描述

  1. push代码到Gitee

在这里插入图片描述

在这里插入图片描述

  • 去Gitee看下,有了
    在这里插入图片描述
  1. 更改代码后pull到IDEA

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

8.3 导入GitHub项目

  1. 粘贴github链接然后导入

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  1. 如若github更新了,gitee需要刷新后更新
  • 点击项目名字后面的更新即可

在这里插入图片描述

  • 刷新后查看代码显示最新

在这里插入图片描述

9 自建代码托管平台-GitLab

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值