Git—入门知识点梳理

Git

1、Git

1.1 概述

免费开源的分布式版本控制工具
性能优于其他版本控制根据,如:Subversion、CVS、Perforce和ClearCase

1.2 介绍

1.2.1 版本控制

1.2.1.1 用途

记录历史版本
方便版本切换

1.2.1.2 工具
  • 集中式版本控制工具
    常见:SVN、CVS、VSS
    含义:只使用一台服务器来做版本控制,所有使用者必须下载最新版本的文件编写代码
    在这里插入图片描述
  • 分布式版本控制工具
    常见:Git、Mercurial、Bazaar、Darcs……
    含义:每个人的电脑上都会有一个完整的版本库
    在这里插入图片描述

1.2.2 工作机制

在这里插入图片描述

1. 工作区
指代码存放的磁盘位置
在这里插入图片描述

可以删减——删除无版本记录

2. 暂存区
将代码添加到暂存区
临时存储

可以删减——删除无版本记录

3. 本地库
提交本地库
会生成对应的版本

不可以删减
如果想删减之前的代码,只能删除代码后再提交一个新版本,之前有问题的版本无法删除
因为Git的底层原理是新版本永远依赖上一个版本生成,实在想删除某一个版本,只能删除整个本地库

4. 远程库
push到远程库

如果是代码提交到了开源的代码托管中心,全世界都可以看到你的代码,也无法删减

1.2.3 代码托管中心(远程库)

1. 局域网(不开源)
GitLab
2. 互联网(开源)
外网——Github
国内网站——Gitee

1.3 安装

1.3.1 基于官网发布的最新版本2.31.1

Git安装好了后就可以使用赠送的linux命令来操作window系统
之前操作window系统得使用dos命令

1.3.2 步骤

  1. 查看GNU协议,直接点击下一步
    在这里插入图片描述

  2. 非中文路径
    在这里插入图片描述

  3. Git选项配置,按默认来
    在这里插入图片描述

  4. Git安装目录名,按默认来
    在这里插入图片描述

  5. Git默认编辑器,使用Vim编辑器,按默认来
    在这里插入图片描述

  6. 选第一个,之后默认的分支名字都是master,按默认来
    在这里插入图片描述

  7. 不修改环境变量,只在Git Bash里使用Git,建议选第一个
    在这里插入图片描述
    Git默认选第二个,指可以在cmd命令行、或者第三方软件和Git Bash中使用Git,但一般不会有人在cmd命令行或者第三方软件中使用

  8. 选择客户端后台连接协议,按默认来
    在这里插入图片描述

  9. 配置行末换行符,按默认来
    在这里插入图片描述
    配置Git文件的行末换行符,Windows使用CRLF,Linux使用LF,选择第一个自动转换,然后继续下一步
    Git会根据我们的运行环境来选择是哪个换行符

  10. Git终端选择Gitbash的linux命令,按默认来
    在这里插入图片描述

  11. Git pull合并的模式(拉取代码到本地库模式),按默认来
    在这里插入图片描述

  12. Git的凭据管理器,记录登录行为,按默认来
    在这里插入图片描述

  13. 其他配置,按默认来
    在这里插入图片描述

  14. 实验室功能,技术还不成熟,有已知的bug,不要勾选
    在这里插入图片描述

1.3.3 安装成功证明

桌面右键 - Git Bash Here
在这里插入图片描述
出现Git版本便证明安装成功

111@Daier-WuHongyu  MINGW64 ~/Desktop
$ git --version
git version 2.31.1.windows.1

1.4 命令

1.4.1 设置用户签名、邮箱

步骤

命令:

git config --global user.name 用户名
git config --global user.email 邮箱
111@Daier-WuHongyu MINGW64 ~/Desktop
$ git config --global user.name wuyanzu

111@Daier-WuHongyu MINGW64 ~/Desktop
$ git config --global user.email  fin@163.com

git不会验证邮箱是否真实存在

结果

111@Daier-WuHongyu MINGW64 ~/Desktop
$ cat ~/.gitconfig
[user]
        name = wuyanzu
        email = fin@163.com

用处
用于区分不同的使用者,第一次安装Git需要设置用户签名,否则会报错
这个用户名和邮箱与之后登录Github等代码托管平台无任何关联,只代表了当前windows环境下的Git客户端账号和邮箱

1.4.2. 初始化本地库

命令:git init

步骤
在需要初始化本地库的目录下,右键选择Git Bash Here
在这里插入图片描述

命令:git init
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo
$ git init
Initialized empty Git repository in D:/Git-space/git-demo/.git/

结果
生成.git的隐藏文件
在这里插入图片描述
当前目录会带上master标记

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ ll -a
total 4
drwxr-xr-x 1 111 197121 0 Nov 14 23:36 ./
drwxr-xr-x 1 111 197121 0 Nov 14 22:50 ../
drwxr-xr-x 1 111 197121 0 Nov 14 23:36 .git/

查看隐藏文件

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ ll -a
total 4
drwxr-xr-x 1 111 197121 0 Nov 14 23:36 ./
drwxr-xr-x 1 111 197121 0 Nov 14 22:50 ../
drwxr-xr-x 1 111 197121 0 Nov 14 23:36 .git/

用处
让Git管理某个目录,首先得获得到该目录的管理权

1.4.3 查看本地库状态

命令:git status

步骤

1 第一次输入git status

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

含义:

On branch master
指当前在master分支上
No commits yet
目前还没提交过任何东西
nothing to commit (create/copy files and use “git add” to track)
目前还没有东西需要提交

2 添加一个文件

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ vim hello.txt
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!

3 第二次输入git status

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)

含义
On branch master
指当前在master分支上
No commits yet
目前还没提交过任何东西
Untracked files:
(use “git add …” to include in what will be committed)
hello.txt

nothing added to commit but untracked files present (use “git add” to track)
表示存在未被追踪的文件,指文件存在工作区
hello.txt为红色

1.4.4. 添加暂存区

命令: git add 文件名

步骤
1 将hello.txt添加到暂存区

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory

含义
warning: LF will be replaced by CRLF in hello.txt.
我们用linux命令vim编辑的文档hello.txt默认的换行符是LF,会自动转换成windows版本的CRLF

2 第三次输入git status

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt

含义:On branch master
指当前在master分支上
No commits yet
目前还没提交过任何东西
Changes to be committed:
(use “git rm --cached …” to unstage)
new file: hello.txt
指已经追踪到了hello.txt文件
如果想删掉暂存区的文件需要使用git rm --cached 命令

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git rm --cached hello.txt
rm 'hello.txt'

只是删除暂存区的文件,文件还在工作区中
hello.txt为绿色

1.4.5. 将暂存区的文件提交到本地库

命令
git commit -m “日志信息” 文件名
步骤
1 提交到本地库

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git commit -m "first commit" hello.txt
[master (root-commit) 74a9a77] first commit
 1 file changed, 5 insertions(+)
 create mode 100644 hello.txt

含义
[master (root-commit) 74a9a77] first commit
主干分支有第一次提交
版本号
74a9a77
1 file changed, 5 insertions(+)
一个文件被改变,五行被插入
2 第四次输入git status
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
含义
On branch master
指当前在master分支上
nothing to commit, working tree clean
已经有自己的版本信息了,并且提交后还没新增代码,没任何文件需要提交
3 查看版本信息
查看版本信息
命令

git reflog
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reflog
74a9a77 (HEAD -> master) HEAD@{0}: commit (initial): first commit

含义
查看到当前的版本号:74a9a77
完整版前7位
当前的指针(HEAD ->)指向第一次提交的版本
查看版本详细信息
命令
git log

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git log
commit 74a9a778aeebb2b1715ce3730b6897717f9431ed (HEAD -> master)
Author: wuyanzu <fin@163.com>
Date:   Tue Nov 15 10:21:54 2022 +0800
first commit

含义
查看到当前完整版的版本号

74a9a778aeebb2b1715ce3730b6897717f9431ed 

查看到版本提交的用户和邮箱

Author: wuyanzu <fin@163.com>
Date:   Tue Nov 15 10:21:54 2022 +0800

当前的指针(HEAD ->)指向第一次提交的版本

1.4.6. 修改当前本地库中的 hello.txt

1 修改文件

命令:

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ vim hello.txt
hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git!

2 第五次输入git status

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

含义
On branch master
指当前在master分支上
modified: hello.txt
检测到了被修改的文件:hello.txt
hello.txt为红色

3 添加暂存区

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory

4 第六次输入git status

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.txt

hello.txt为绿色
5 提交本地库

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git commit -m "second commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master b234478] second commit
 1 file changed, 1 insertion(+), 1 deletion(-)

含义
1个文件被修改,1行新增,1行删除
Git是按照行来管理文件的,改一行只能通过删除1行,新增1行来完成
6 第七次输入git status

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean

工作区又干净了
7 查看版本

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reflog
b234478 (HEAD -> master) HEAD@{0}: commit: second commit
74a9a77 HEAD@{1}: commit (initial): first commit

查看到了两个版本,指针指向第二个版本
8 再修改一次
修改文件
命令:

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ vim hello.txt
hello atguigu! hello git! second wirte
hello atguigu! hello git! third  write
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git!
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reflog
6fcf5dc (HEAD -> master) HEAD@{0}: commit: third commit
b234478 HEAD@{1}: commit: second commit
74a9a77 HEAD@{2}: commit (initial): first commit

1.4.7. 历史版本

a、查看版本信息
查看版本信息

命令
git reflog

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reflog
6fcf5dc (HEAD -> master) HEAD@{0}: commit: third commit
b234478 HEAD@{1}: commit: second commit
74a9a77 HEAD@{2}: commit (initial): first commit

含义
查看到当前的版本号
6fcf5dc
完整版前7位
当前的指针(HEAD ->)指向第一次提交的版本
查看版本详细信息
命令

git log
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git log
commit 6fcf5dc08530417c1213304c7f1ad1515a5e1c55 (HEAD -> master)
Author: wuyanzu <fin@163.com>
Date:   Tue Nov 15 13:52:45 2022 +0800

    third commit

commit b234478533d62f7aebfd3c519df45b9bed1565e4
Author: wuyanzu <fin@163.com>
Date:   Tue Nov 15 11:31:34 2022 +0800

    second commit

commit 74a9a778aeebb2b1715ce3730b6897717f9431ed
Author: wuyanzu <fin@163.com>
Date:   Tue Nov 15 10:21:54 2022 +0800

    first commit

含义
查看到当前完整版的版本号
74a9a778aeebb2b1715ce3730b6897717f9431ed
查看到版本提交的用户和邮箱

Author: wuyanzu <fin@163.com>
Date:   Tue Nov 15 10:21:54 2022 +0800

当前的指针(HEAD ->)指向第一次提交的版本
b 版本穿梭
命令
git reset --hard 版本号
7位精简版本号
步骤
查看日志

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reflog
6fcf5dc (HEAD -> master) HEAD@{0}: commit: third commit
b234478 HEAD@{1}: commit: second commit
74a9a77 HEAD@{2}: commit (initial): first commit

版本穿梭,切换到第二次提交的版本

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reset --hard b234478
HEAD is now at b234478 second commit

查看日志

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git reflog
b234478 (HEAD -> master) HEAD@{0}: reset: moving to b234478
6fcf5dc HEAD@{1}: commit: third commit
b234478 (HEAD -> master) HEAD@{2}: commit: second commit
74a9a77 HEAD@{3}: commit (initial): first commit

指针切换到b234478
切换成第二个版本的文档

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ cat hello.txt
hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git!

版本穿梭不仅可以往后穿梭,还可以往前穿梭

1.5 分支

1.5.1 分支的概念

在这里插入图片描述
在公司中,用户使用的是生产环境的master分支,我们开发使用自己的dev分支,我们开发完后,会去测试分支上与其他开发好的代码合并在test分支联调,最终合并上传在master分支上

1.5.2 分支的好处

推进各功能并行开发

1.5.3 分支的操作

  1. 查看分支
    命令
    git branch -v
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git branch -v
* master b234478 second commit

含义
当前在master分支上,且是第二次提交的版本

  1. 创建分支
    命令
    git branch 分支名
    复制主干分支,创建一个紧急修复的热修分支
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git branch hot-fix

可以看到现在有两个分区,hot-fix 和 master,* 表示当前正处在master分支上

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git branch -v
  hot-fix b234478 second commit
* master  b234478 second commit
  1. 切换分支
    命令
    git checkout 分支名
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (hot-fix)
$

发现当前分支已由master改为hot-fix

  1. 修改分支
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (hot-fix)
$ vim hello.txt
hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git! hot-fix commit
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (hot-fix)
$ git reflog
5c3c08b (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix first commit
b234478 (master) HEAD@{1}: checkout: moving from master to hot-fix
b234478 (master) HEAD@{2}: reset: moving to b234478
6fcf5dc HEAD@{3}: commit: third commit
b234478 (master) HEAD@{4}: commit: second commit
74a9a77 HEAD@{5}: commit (initial): first commit

当前指针指向hot-fix下的first commit

  1. 合并分支(正常合并)
    命令
    git merge 分支名
    站在master分支上,将hot-fix分支的内容合并过来

a. 切换分支

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (hot-fix)
$  git checkout master
Switched to branch 'master'

b. 合并分支,将hot-fix分支的内容合并过来

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git merge hot-fix
Updating b234478..5c3c08b
Fast-forward
 hello.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  1. 产生冲突(不正常合并)

a、master 分支第四行添加
master test,后commit

hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git! master test
hello atguigu! hello git! hot-fix commit

b、hot-fix 分支第四行添加
hot-fix test,后commit

hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git! hot-fix test
hello atguigu! hello git! hot-fix commit

c、在master上合并hot-fix test

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (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.

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master|MERGING)

含义
Automatic merge failed; fix conflicts and then commit the result.
自动merge失败,请解决冲突后重新提交
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master|MERGING)
处于合并中的状态

d、查看冲突

hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
<<<<<<< HEAD
hello atguigu! hello git! master test
=======
hello atguigu! hello git! hot-fix test
>>>>>>> hot-fix
hello atguigu! hello git! hot-fix commit

e、解决冲突

hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git!
hello atguigu! hello git! master test hot-fix test
hello atguigu! hello git! hot-fix commit

手动使用vim一行行修改

f、commit时不带文件名提交

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master|MERGING)
$ git commit -m "merge" hello.txt
fatal: cannot do a partial commit during a merge.

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master|MERGING)
$ git commit -m "merge test"
[master 0431059] merge test

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)

状态从 (master|MERGING)变成 (master)

1.6 Git团队协作机制

  1. 团队内协作

在这里插入图片描述
a、岳不群push自己本地库代码到远程库(GitHub、GitLab、Gitee)
b、令狐冲从远程库clone代码到自己的本地库
c、令狐冲修改完代码后push到远程库
d、岳不群可以从远程库pull最新的代码

  1. 跨团队协作

在这里插入图片描述

a、团队外的东方不败fork岳不群远程库的代码到自己的远程库(相当于复制)
b、东方不败从远程库clone代码到自己的本地库
c、东方不败修改完代码后push到自己的远程库
d、东方不败提交拉取自己远程库的请求
e、岳不群merge,审核通过后,便合并到一起了
f、岳不群可以pull最新版本的代码

总结
从这个案例我们可以看出为什么Git叫分布式版本控制工具,因为不管是令狐冲、岳不群还是东方不败,都是在自己电脑上管理多个版本,远程库只push最终确认上线的版本,体现了分布式版本控制的概念。

1.7 idea集成Git

配置Git忽略文件
为什么
打包时,要忽略掉.idea 和 .iml 文件,因为这些文件与实际功能无关,不用在服务器上运行
在这里插入图片描述
怎么做

  1. 文件名
    创建忽略规则文件xxxx.ignore(前缀名随便起,建议是git.ignore)

  2. 位置
    家目录下

在这里插入图片描述

内容
参考模板
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

.gitconfig

[user]
	name = wuyanzu
	email = fin@163.com
[core]
	excludesfile = C:/Users/111/git.ignore
  1. 定位Git程序
    创建一个maven过程git-test
    配置git的安装目录,然后test一下

在这里插入图片描述
3. 初始化、添加、提交本地库
可以看到当前的项目没有.git文件,证明本地库还没初始化,我们可以使用git初始化本地库,VCS(version control setting)
在这里插入图片描述
红色指未被追踪,未提交到暂存区;
黄色为忽略文件
在这里插入图片描述
绿色指未提交到本地库
在这里插入图片描述
创建GitTest类文件

package com.atguigu.git;

public class GitTest {
    public static void main(String[] args) {
        System.out.println("hello git");
    }
}

点选项目,一起添加到暂存区,提交本地库
在这里插入图片描述
在这里插入图片描述
提交本地库后会变成黑色
在这里插入图片描述

  1. 版本切换
    添加一行,蓝色指被追踪过,但被修改了

在这里插入图片描述
追踪过的文件可以直接提交本地库

查看所有版本
在这里插入图片描述

绿色表示master分支指向第三个版本
黄色表示当前界面看到的头指针指向第三个版本
切换版本
在这里插入图片描述

头指针回到了第二个版本

  1. 创建hot-fix分支
    new branch

在这里插入图片描述
分支名

在这里插入图片描述

正处在hot-fix分支

在这里插入图片描述

切换分支
在这里插入图片描述

  1. 合并分支(正常合并)
    在hot-fix添加第四行

在这里插入图片描述

package com.atguigu.git;

public class GitTest {
    public static void main(String[] args) {
        System.out.println("hello git");
        System.out.println("hello git2");
        System.out.println("hello git3");
        System.out.println("hello git4");
    }
}

站在master分支上合并hot-fix分支
在这里插入图片描述

master分支上也会有四行文件

package com.atguigu.git;

public class GitTest {
    public static void main(String[] args) {
        System.out.println("hello git");
        System.out.println("hello git2");
        System.out.println("hello git3");
        System.out.println("hello git4");
    }
}
  1. 合并分支(不正常合并)
    在hot-fix添加第五行
package com.atguigu.git;

public class GitTest {
    public static void main(String[] args) {
        System.out.println("hello git");
        System.out.println("hello git2");
        System.out.println("hello git3");
        System.out.println("hello git4");
        System.out.println("hot-fix-test5");
    }
}

master也添加第五行,并提交工作区

package com.atguigu.git;

public class GitTest {
    public static void main(String[] args) {
        System.out.println("hello git");
        System.out.println("hello git2");
        System.out.println("hello git3");
        System.out.println("hello git4");
        System.out.println("master test");
    }
}

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

站在master分支上合并hot-fix分支,需要手动合并代码
在这里插入图片描述

手动合并
在这里插入图片描述

合并后会自动提交到本地库
在这里插入图片描述

2、GitHub

2.1 创建远程库

步骤

1、New repository
在这里插入图片描述
2、设置远程库名
同一个账号远程库不允许重名

3、设置公开还是私有

  • Public
    • 全世界都可以看到你的代码,免费
  • Private
    • 私有,超过5个人收费
    • 不建议使用,如果公司要搭建私有代码库,选Gitlab

在这里插入图片描述

2.2 远程库操作

  1. 创建远程库别名
    命令
    git remote add 别名 远程地址
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git remote add fin-bigdata https://github.com/Fin-WuHongYu/Fin-Bigdata.git
  1. 查看当前所有远程地址别名
    命令
    git remote -v
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git remote -v
fin-bigdata     https://github.com/Fin-WuHongYu/Fin-Bigdata.git (fetch)
fin-bigdata     https://github.com/Fin-WuHongYu/Fin-Bigdata.git (push)
  1. 推送本地分支上的内容到远程仓库
    命令
    git push 别名 分支
111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git push fin-bigdata master

在这里插入图片描述

通过浏览器绑定的GitHub账号登录
在这里插入图片描述

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git push fin-bigdata master
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 16 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (18/18), 1.34 KiB | 457.00 KiB/s, done.
Total 18 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/Fin-WuHongYu/Fin-Bigdata.git
 * [new branch]      master -> master

含义
成功将当前分支推送到远程库GitHub
网页上刷新即可看到GitHub上有本地库代码hello.txt

在这里插入图片描述

  1. 拉取远程库代码
    命令
    git pull 别名 分支
    步骤
    在GitHub页面上编辑 hello.txt

在这里插入图片描述
在第三行添加 github-web-test

拉取远程库代码

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git pull fin-bigdata master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 670 bytes | 39.00 KiB/s, done.
From https://github.com/Fin-WuHongYu/Fin-Bigdata
 * branch            master     -> FETCH_HEAD
   0431059..be5e72f  master     -> fin-bigdata/master
Updating 0431059..be5e72f
Fast-forward
 hello.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

本地库代码已被修改

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ vim hello.txt
hello atguigu! hello git! second wirte
hello atguigu! hello git!
hello atguigu! hello git! github-web-test
hello atguigu! hello git! master test hot-fix test
hello atguigu! hello git! hot-fix commit
  1. 克隆远程仓库到本地
    命令
    git clone 远程地址
    步骤
    先拿到远程仓库地址

在这里插入图片描述

https://github.com/Fin-WuHongYu/Fin-Bigdata.git

clone到本地Git-ybq文件夹下

111@Daier-WuHongyu MINGW64 /d/Git-ybq
$ git clone https://github.com/Fin-WuHongYu/Fin-Bigdata.git
Cloning into 'Fin-Bigdata'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 21 (delta 6), reused 17 (delta 5), pack-reused 0
Receiving objects: 100% (21/21), done.
Resolving deltas: 100% (6/6), done.

在这里插入图片描述
注意
clone代码是不需要登录GitHub账号的,任何人都可以clone代码,因为我们之前创建的是public的远程库
会给克隆下来的链接起别名,叫origin

111@Daier-WuHongyu MINGW64 /d/Git-ybq/Fin-Bigdata (master)
$ git remote -v
origin  https://github.com/Fin-WuHongYu/Fin-Bigdata.git (fetch)
origin  https://github.com/Fin-WuHongYu/Fin-Bigdata.git (push)
  1. 邀请加入团队
    步骤
    选择邀请合作者
    在这里插入图片描述

复制邀请链接
在这里插入图片描述

https://github.com/Fin-WuHongYu/Fin-Bigdata/invitations

接受邀请,成为团队一员

在这里插入图片描述
6 团队类另一个成员提交代码到远程库(团队内协作)
使用第二个账号登录,如果是在同一电脑上登录,记得删除电脑中原来的凭据
在这里插入图片描述

在第二行添加

hello atguigu! hello git! second wirte
hello atguigu! hello git! lhc-commit
hello atguigu! hello git! github-web-test
hello atguigu! hello git! master test hot-fix test
hello atguigu! hello git! hot-fix commit
					lhc-commit

使用第二个账号提交并推送到远程库

111@Daier-WuHongyu MINGW64 /d/Git-lhc/Fin-Bigdata (master)
$ git add hello.txt

111@Daier-WuHongyu MINGW64 /d/Git-lhc/Fin-Bigdata (master)
$ git commit -m "lhc" hello.txt
[master 24c56b9] lhc
 1 file changed, 1 insertion(+), 1 deletion(-)

111@Daier-WuHongyu MINGW64 /d/Git-lhc/Fin-Bigdata (master)
$  git push https://github.com/Fin-WuHongYu/Fin-Bigdata.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (1/1), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 1), reused 2 (delta 1), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Fin-WuHongYu/Fin-Bigdata.git
   69602a0..24c56b9  master -> master

结果
在这里插入图片描述

第二个账号正常提交,文件已被修改

  1. 团队外的成员提交代码到远程库(跨团队协作)

    • 登录第三个账号,先通过项目链接,Fork项目,将远程库的代码复制到自己的远程库中
    • 用第三个账号修改自己的远程库代码

在这里插入图片描述
在第六行添加:hello atguigu! hello git! dongfangbubai test

  • 创建拉取请求
  • 切换为第一个账号,可以看到拉取请求
  • 审核通过后,可以将代码合并

2.3 SSH 免密登录

GitHub中有两种链接HTTPS协议和SSH协议,我们使用HTTPS协议需要绑定GitHub账号和密码,我们可以使用SSH登录,之后拉取、推送代码都无需登录

运行命令生成.ssh秘钥目录

111@Daier-WuHongyu MINGW64 ~
$ ssh-keygen -t rsa -C w13112688556@163.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/111/.ssh/id_rsa):
Created directory '/c/Users/111/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/111/.ssh/id_rsa
Your public key has been saved in /c/Users/111/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:EszNg5dA2B6cXpTDCBa4Lkt9TYkFeV9vi8NhnPEmWUE w13112688556@163.com
The key's randomart image is:
+---[RSA 3072]----+
|   .+O==..  .E.  |
|  ...+*B=. o .   |
|   . oOoO.o B    |
|  .  .o= o B =   |
| ..   + S o * .  |
|.... . o   + .   |
|.o  .       .    |
|.                |
|                 |
+----[SHA256]-----+

进入.ssh目录查看文件列表

111@Daier-WuHongyu MINGW64 ~
$ cd .ssh
111@Daier-WuHongyu MINGW64 ~ /.ssh
$ ll -a
total 21
drwxr-xr-x 1 Layne 197609    0 1125 19:27 ./
drwxr-xr-x 1 Layne 197609    0 1125 19:27 ../
-rw-r--r-- 1 Layne 197609 1679 1125 19:27 id_rsa
-rw-r--r-- 1 Layne 197609  406 1125 19:27 id_rsa.pub

复制id_rsa.pub文件内容

111@Daier-WuHongyu MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDSevXY8hYrLZodh4vfB1jCYYov4qfsqpDOpqUpOpxIrVrEuaMz/sTsZnuJ7XL7/67Qyul7b7CPt9wlGlOBFDONgoy9SyMWARJzIgU3uijyQQLiCnLPOZYMAf/ggZshPghwAy/g8Wd81PPk4lbDlzIUr1qQUwbylKBlETyeUOwimHJNInuGkQLyqHXALS8FHBQXtj7HGVzF3xYWKnluncnROTXcmoPYvl9Iigpy3GWHO+od+wySJwZBx5waRMNjRbKmL7OL9fgkhOtcwGqPaZfTx1AOn/jgxqJT11QEVM97PzumMMkZ+vTWZOta9RoWJ/2guLNWZg8iWriszONdGdiqUt1VRkAxBLlkNFUdztBCjnvzyefGCq+IfGyMvxKeJOlkQjBGv5LZjHOQZTZf08PiqoFE611pv38qZAj2EI4ufSk2FuJPoSwMXJG8why3xMK6an0kvAeIuF6KXf2r0b+hdDG2kyta7AxNesWUizMjEczIcA4GqpGgzploRhfDpgc= w13112688556@163.com

粘贴公钥内容在这里插入图片描述免密拉取文件

111@Daier-WuHongyu MINGW64 /d/Git-space/git-demo (master)
$ git pull git@github.com:Fin-WuHongYu/Fin-Bigdata.git master
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 9 (delta 2), reused 4 (delta 1), pack-reused 0
Unpacking objects: 100% (9/9), 2.19 KiB | 45.00 KiB/s, done.
From github.com:Fin-WuHongYu/Fin-Bigdata
 * branch            master     -> FETCH_HEAD
Updating be5e72f..2645b4a
Fast-forward
 hello.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

2.4 Idea 集成 GitHub

2.4.1 设置GitHub账号

在这里插入图片描述
登录方式1:添加账号密码,但很难登录
在这里插入图片描述
登录方式2:使用口令登录

起一个口令名并拉满所有权限
在这里插入图片描述
在这里插入图片描述
口令只会出现一次,记得复制出来
在这里插入图片描述

2.4.2 分享工程到GitHub

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

3、Gitee码云

3.1 码云创建远程库

3.1.1 网址

https://gitee.com/

3.1.2 创建私有仓库

在这里插入图片描述
一般使用https协议

3.2 Idea 集成 Gitee 码云

  1. 安装gitee插件,默认不带,并添加gitee账号(操作与GitHub基本一致)

  2. 向创建好的远程库push代码

在这里插入图片描述

  1. 新建gitee连接(使用https协议即可)

在这里插入图片描述

  1. 刷新网页即可

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据之负

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值