Git全解 idea github gitee gitlab

文章目录

尚硅谷技术课程系列之 Git

课程资源 https://www.bilibili.com/video/BV1vy4y1s7k6

1、Git概述

​ Git 是一个免费的、开源的分布式版本控制系统,可以快速高效地处理从小型到大型的各种
项目。
​ Git 易于学习,占地面积小,性能极快。 它具有廉价的本地库,方便的暂存区域和多个工作
流分支等特性。其性能优于 Subversion、CVS、Perforce 和 ClearCase 等版本控制工具。

2、何为版本控制

​ 版本控制是一种记录文件内容变化,以便将来查阅特定版本修订情况的系统。
​ 版本控制其实最重要的是可以记录文件修改历史记录,从而让用户能够查看历史版本,
方便版本切换。

3、为什么需要版本控制

个人开发过渡到团队协作。
在这里插入图片描述

分布式的版本控制系统出现之后,解决了集中式版本控制系统的缺陷:

  1. 服务器断网的情况下也可以进行开发(因为版本控制是在本地进行的)
  2. 每个客户端保存的也都是整个完整的项目(包含历史记录,更加安全)

4、Git 工作机制

在这里插入图片描述

5、Git 和代码托管中心

代码托管中心是基于网络服务器的远程代码仓库,一般我们简单称为远程库。
➢ 局域网
✓ GitLab
➢ 互联网
✓ GitHub(外网)
✓ Gitee 码云(国内网站)

6、Git常用命令

在这里插入图片描述

6.1 设置用户签名

基本语法

git config --global user.name 用户名
git config --global user.email 邮箱

说明:
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看
到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码
※注意:这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任
何关系。

6.2 初始化本地库

基本语法

git init
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720
$ git init
Initialized empty Git repository in D:/Git-Space/SH0720/.git/

结果查看

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

6.3 查看本地库状态

基本语法

git status

首次查看

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

新增文件(hello.txt)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ vim hello.txt
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!

再次查看(检测到未追踪的文件

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (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)
6.4 添加暂存区

基本语法

git add 文件名

实例

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (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.

查看状态(检测到暂存区有新文件)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git status
On branch master
No commits yet
Changes to be committed:
 (use "git rm --cached <file>..." to unstage)
 new file: hello.txt
6.5 提交到本地库

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

基本语法

git commit -m "日志信息" 文件名

实例

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git commit -m "my first 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 (root-commit) 86366fa] my first commit
1 file changed, 16 insertions(+)
create mode 100644 hello.txt

查看状态(没有文件需要提交)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git status

On branch master
nothing to commit, working tree clean
6.6 修改文件

退出 按esc 输入 :wq

插入 按 i

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ vim hello.txt

hello git! hello atguigu! 2222222222222
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
6.6.1 查看状态(检测到工作区有文件被修改)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git status
On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working 
directory)
 modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
6.6.2 将修改的文件再次添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (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.
6.6.3 查看状态(工作区的修改添加到了暂存区)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: hello.txt
6.7 历史版本
6.7.1 查看历史版本

基本语法

git reflog 查看版本信息
git log 查看版本详细信息
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reflog

087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit (head指向头文件)
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
6.7.2 版本穿梭

基本语法

git reset --hard 版本号
--首先查看当前的历史记录,可以看到当前是在 087a1a7 这个版本
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reflog

087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit


--切换到 86366fa 版本,也就是我们第一次提交的版本
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reset --hard 86366fa

HEAD is now at 86366fa my first commit


--切换完毕之后再查看历史记录,当前成功切换到了 86366fa 版本
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reflog

86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa
087a1a7 HEAD@{1}: commit: my third commit
ca8ded6 HEAD@{2}: commit: my second commit
86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit


--然后查看文件 hello.txt,发现文件内容已经变化
$ cat hello.txt

hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!

Git 切换版本,底层其实是移动的 HEAD 指针,具体原理如下图所示
在这里插入图片描述

6.8 Git标签

-a 选项意为"创建一个带注解的标签"

$ git tag -a v1.0 

Git有commit,为什么还要引入tag?

“请把上周一的那个版本打包发布,commit号是6a5819e…”

“一串乱七八糟的数字不好找!”

如果换一个办法:

“请把上周一的那个版本打包发布,版本号是v1.2”

“好的,按照tag v1.2查找commit就行!”

所以,tag就是一个让人容易记住的有意义的名字,它跟某个commit绑在一起。

6.8.1 创建标签
[root@Git git]# git tag v1.0
6.8.2 查看已有标签
[root@Git git]# git tag
v1.0
[root@Git git]# git tag v1.1
[root@Git git]# git tag
v1.0
v1.1
6.8.3 删除标签
[root@Git git]# git tag -d v1.1
Deleted tag ‘v1.1’ (was 91388f0)
[root@Git git]# git tag
v1.0
6.8.4 查看此版本所修改的内容
[root@Git git]# git show v1.0
commit 91388f0883903ac9014e006611944f6688170ef4
Author: "syaving" <"819044347@qq.com">
Date: Fri Dec 16 02:32:05 2016 +0800
commit dir
diff –git a/readme b/readme
index 7a3d711..bfecb47 100644
— a/readme
+++ b/readme
@@ -1,2 +1,3 @@
text
hello git
+use commit
[root@Git git]# git log –oneline
91388f0 commit dir
e435fe8 add readme
2525062 add readme

7、Git分支操作

在这里插入图片描述


什么是分支?

在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独
分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时
候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是
一个单独的副本。(分支底层其实也是指针的引用)

分支的好处

同时并行推进多个功能开发,提高开发效率。
各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败
的分支删除重新开始即可。

分支的操作
在这里插入图片描述

7.1 查看分支

基本语法

git branch -v
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v

master 087a1a7 my third commit (*代表当前所在的分区)
7.2 创建分支

基本语法

git branch 分支名
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch hot-fix

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v

hot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支 master的内容复制了一份)
* master 087a1a7 my third commit
7.3 修改分支
--在 maste 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ vim hello.txt


--添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git add hello.txt


--提交本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git commit -m "my forth commit" hello.txt
[master f363b4c] my forth commit
1 file changed, 1 insertion(+), 1 deletion(-)


--查看分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
 hot-fix 087a1a7 my third commit (hot-fix 分支并未做任何改变)
* master f363b4c my forth commit (当前 master 分支已更新为最新一次提交
的版本)


--查看 master 分支上的文件内容
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! master test
hello git! hello atguigu!

7.4 切换分支

基本语法

git checkout 分支名
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'


--发现当先分支已由 master 改为 hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$
--查看 hot-fix 分支上的文件内容发现与 master 分支上的内容不同
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!


--在 hot-fix 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test


--添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git add hello.txt


--提交本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git commit -m "hot-fix commit" hello.txt
7.5 合并分支

基本语法

git merge 分支名

案例实操 在 master 分支上合并 hot-fix 分支

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (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.
7.6 产生冲突

冲突产生的表现:后面状态为 MERGING

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
<<<<<<< HEAD
hello git! hello atguigu! master test
hello git! hello atguigu!
=======
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
>>>>>>> hot-fix

冲突产生的原因:
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替
我们决定使用哪一个。必须人为决定新代码内容。
查看状态(检测到有文件有两处修改)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ git status
On branch master
You have unmerged paths.
 (fix conflicts and run "git commit")
 (use "git merge --abort" to abort the merge)
Unmerged paths:
 (use "git add <file>..." to mark resolution)
 both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
7.7 解决冲突

编辑有冲突的文件,删除特殊符号,决定要使用的内容
特殊符号:<<<<<<< HEAD 当前分支的代码 ======= 合并过来的代码 >>>>>>> hot-fix

hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! master test
hello git! hello atguigu! hot-fix test

添加到暂存区

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ git add hello.txt

执行提交(注意:此时使用 git commit 命令时不能带文件名)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ git commit -m "merge hot-fix"
[master 69ff88d] merge hot-fix
--发现后面 MERGING 消失,变为正常
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$
7.8 创建分支和切换分支

master、hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD
决定的。所以创建分支的本质就是多创建一个指针。
HEAD 如果指向 master,那么我们现在就在 master 分支上。
HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。

所以切换分支的本质就是移动 HEAD 指针。

7.9 git merge 和git rebase区别*****

https://segmentfault.com/a/1190000018580144

使用场景解释

https://www.jianshu.com/p/4079284dd970

8、Git 团队协作机制

8.1 团队内协作

在这里插入图片描述

8.2 跨团队协作

在这里插入图片描述

9、GitHub操作

9.1 建立远程仓库
9.2 远程仓库操作

在这里插入图片描述

9.2.1 创建远程仓库别名
基本语法
git remote -v 查看当前所有远程地址别名
git remote add 别名 远程地址
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git remote -v
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git remote add ori https://github.com/atguiguyueyue/git-shTest.git
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git remote -v    (一般会创建俩个)
ori https://github.com/atguiguyueyue/git-shTest.git (fetch)  拉取的别名
ori https://github.com/atguiguyueyue/git-shTest.git (push)   push的别名
9.2.2 推送本地分支到远程仓库

基本语法

git push 别名 分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git push ori master
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': atguiguyueyue
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/atguiguyueyue/git-shTest.git
* [new branch] master -> master
9.2.3 克隆远程仓库到本地

基本语法

git clone 远程地址
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong
$ git clone https://github.com/atguiguyueyue/git-shTest.git
Cloning into 'git-shTest'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
9.2.4 邀请加入团队

1)选择邀请合作者
在这里插入图片描述

2)填入想要合作的人
在这里插入图片描述

3)复 制 地 址 并 通 过 微 信 钉 钉 等 方 式 发 送 给 该 用 户 , 复 制 内 容 如 下 :
https://github.com/atguiguyueyue/git-shTest/invitations
在这里插入图片描述

4)在 atguigulinghuchong 这个账号中的地址栏复制收到邀请的链接,点击接受邀请
在这里插入图片描述

5)成功之后可以在 atguigulinghuchong 这个账号上看到 git-Test 的远程仓库
在这里插入图片描述

6)令狐冲可以修改内容并 push 到远程仓库。

--编辑 clone 下来的文件
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ vim hello.txt


Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 33333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! 我是最帅的,比岳不群还帅
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! master test
hello git! hello atguigu! hot-fix test


--将编辑好的文件添加到暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ git add hello.txt


--将暂存区的文件上传到本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ git commit -m "lhc commit" hello.txt
[master 5dabe6b] lhc commit
1 file changed, 1 insertion(+), 1 deletion(-)


--将本地库的内容 push 到远程仓库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ git push origin master
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': atguigulinghuchong
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 309 bytes | 309.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/atguiguyueyue/git-shTest.git
 7cb4d02..5dabe6b master -> master

7)回到 atguiguyueyue 的 GitHub 远程仓库中可以看到,最后一次是 lhc 提交的。
在这里插入图片描述

9.2.5 拉取远程库内容

基本语法

git pull 远程库地址别名 远程分支名
--将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git pull ori master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/atguiguyueyue/git-shTest
* branch master -> FETCH_HEAD
 7cb4d02..5dabe6b master -> ori/master
Updating 7cb4d02..5dabe6b
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)


Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 33333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! 我是最帅的,比岳不群还帅
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! master test
hello git! hello atguigu! hot-fix test
9.3 跨团队协作

1)将远程仓库的地址复制发给邀请跨团队协作的人,比如东方不败。
在这里插入图片描述

2)在东方不败的 GitHub 账号里的地址栏复制收到的链接,然后点击 Fork 将项目叉到自
己的本地仓库。
在这里插入图片描述

fork后
在这里插入图片描述

3)东方不败就可以在线编辑叉取过来的文件。
在这里插入图片描述

4)编辑完毕后,填写描述信息并点击左下角绿色按钮提交。
在这里插入图片描述

5)接下来点击上方的 Pull 请求,并创建一个新的请求。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

6)回到岳岳 GitHub 账号可以看到有一个 Pull request 请求。
在这里插入图片描述

进入到聊天室,可以讨论代码相关内容。
在这里插入图片描述

7)如果代码没有问题,可以点击 Merge pull reque 合并代码。
在这里插入图片描述

9.4 SSH 免密登录

在这里插入图片描述

具体操作如下:

在windos上是自己C:\Users\用户名
--进入当前用户的家目录
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cd


--删除.ssh 目录
Layne@LAPTOP-Layne MINGW64 ~
$ rm -rvf .ssh
removed '.ssh/known_hosts'
removed directory '.ssh'


--运行命令生成.ssh 秘钥目录[注意:这里-C 这个参数是大写的 C]
Layne@LAPTOP-Layne MINGW64 ~
$ ssh-keygen -t rsa -C atguiguyueyue@aliyun.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Layne/.ssh/id_rsa):
Created directory '/c/Users/Layne/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Layne/.ssh/id_rsa.
Your public key has been saved in /c/Users/Layne/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7CPfRLITKcYDhaqpEDeok7Atvwh2reRmpxxOC6dkY44 
atguiguyueyue@aliyun.com
The key's randomart image is:
+---[RSA 2048]----+
| .. |
| .. |
| . .. |
|+ + o . . |
|oO . = S . |
|X . .. + = |
|+@ * .. = . |
|X.&o+. o = |
|Eo+Oo . . |
+----[SHA256]-----+


--进入.ssh 目录查看文件列表
Layne@LAPTOP-Layne MINGW64 ~
$ cd .ssh


Layne@LAPTOP-Layne MINGW64 ~/.ssh
$ ll -a
total 21
drwxr-xr-x 1 Layne 197609 0 11 月 25 19:27 ./
drwxr-xr-x 1 Layne 197609 0 11 月 25 19:27 ../
-rw-r--r-- 1 Layne 197609 1679 11 月 25 19:27 id_rsa
-rw-r--r-- 1 Layne 197609 406 11 月 25 19:27 id_rsa.pub


--查看 id_rsa.pub 文件内容
Layne@LAPTOP-Layne MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa 
AAAAB3NzaC1yc2EAAAADAQABAAABAQDRXRsk9Ohtg1AXLltsuNRAGBsx3ypE1O1Rkdzpm
l1woa6y6G62lZri3XtCH0F7GQvnMvQtPISJFXXWo+jFHZmqYQa/6kOIMv2sszcoj2Qtwl
lGXTPn/4T2h/cHjSHfc+ks8OYP7OWOOefpOCbYY/7DWYrl89k7nQlfd+A1FV/vQmcsa1L
P5ihqjpjms2CoUUen8kZHbjwHBAHQHWRE+Vc371MG/dwINvCi8n7ibI86o2k0dW0+8SL+
svPV/Y0G9m+RAqgec8b9U6DcSSAMH5uq4UWfnAcUNagb/aJQLytrH0pLa8nMv3XdSGNNo
AGBFeW2+K81XrmkP27FrLI6lDef atguiguyueyue@aliyun.com

复制 id_rsa.pub 文件内容,登录 GitHub,点击用户头像→Settings→SSH and GPG keys
在这里插入图片描述

10、Idea集成Git

10.1 配置 Git 忽略文件(idea中其实已经自动配置了)

问题 1:为什么要忽略他们?
答:与项目的实际功能无关,不参与服务器上部署运行。把它们忽略掉能够屏蔽 IDE 工具之
间的差异。
问题 2:怎么忽略?
1)创建忽略规则文件 xxxx.ignore(前缀名随便起,建议是 git.ignore)
这个文件的存放位置原则上在哪里都可以,为了便于让~/.gitconfig 文件引用,建议也放在用
户家目录下

git.ignore 文件模版内容如下

# Compiled class file
*.class
—————————————————————————————
46
更多 Java –大数据 –前端 –python 人工智能资料下载,可访问百度:尚硅谷官网
# 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

2)在.gitconfig 文件中引用忽略配置文件(此文件在 Windows 的家目录中)

[user]
name = Layne
email = Layne@atguigu.com
[core]
excludesfile = C:/Users/asus/git.ignore
注意:这里要使用“正斜线(/)”,不要使用“反斜线(\)”
10.2 定位 Git 程序

在这里插入图片描述

10.3 初始化本地库

在这里插入图片描述

10.4 添加到暂存区

右键点击项目选择 Git -> Add 将项目添加到暂存区。

10.5 提交到本地库

右键点击项目选择 Git -> commit directory 将项目添加到本地库
在这里插入图片描述

10.6 切换版本

右下角
在这里插入图片描述

10.7 创建分支
10.8 切换分支
10.9 合并分支
10.10 解决冲突

上面四个内容都在这篇文章中可以找到 idea2020适用

https://blog.csdn.net/I_r_o_n_M_a_n/article/details/120800499

11、IDEA 集成 GitHub

11.1 设置 GitHub 账号

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

11.2 分享工程到 GitHub

在这里插入图片描述

11.3 push 推送本地库到远程库

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

在这里插入图片描述

11.4 pull 拉取远程库到本地库

在这里插入图片描述

在这里插入图片描述

11.5 clone 克隆远程库到本地

在这里插入图片描述

在这里插入图片描述

12、国内代码托管中心-码云

众所周知,GitHub 服务器在国外,使用 GitHub 作为项目托管网站,如果网速不好的话,
严重影响使用体验,甚至会出现登录不上的情况。针对这个情况,大家也可以使用国内的项
目托管网站-码云。

码云是开源中国推出的基于 Git 的代码托管服务中心,网址是 https://gitee.com/ ,使用
方式跟 GitHub 一样,而且它还是一个中文网站,如果你英文不是很好它是最好的选择。

12.1 IDEA 安装码云插件

Idea 默认不带码云插件,我们第一步要安装 Gitee 插件。
如图所示,在 Idea 插件商店搜索 Gitee,然后点击右侧的 Install 按钮
在这里插入图片描述

12.2 IDEA 连接码云

Idea 连接码云和连接 GitHub 几乎一样,首先在 Idea 里面创建一个工程,初始化 git 工
程,然后将代码添加到暂存区,提交到本地库,这些步骤上面已经讲过,此处不再赘述。

其余步骤跟上诉一样

13、自建代码托管平台-GitLab

13.1 GitLab 简介

GitLab 是由 GitLabInc.开发,使用 MIT 许可证的基于网络的 Git 仓库管理工具,且具有
wiki 和 issue 跟踪功能。使用 Git 作为代码管理工具,并在此基础上搭建起来的 web 服务。
GitLab 由乌克兰程序员 DmitriyZaporozhets 和 ValerySizov 开发,它使用 Ruby 语言写
成。后来,一些部分用 Go 语言重写。截止 2018 年 5 月,该公司约有 290 名团队成员,以
及 2000 多名开源贡献者。GitLab 被 IBM,Sony,JülichResearchCenter,NASA,Alibaba,
Invincea,O’ReillyMedia,Leibniz-Rechenzentrum(LRZ),CERN,SpaceX 等组织使用。

13.2 GitLab 官网地址

官网地址:https://about.gitlab.com/
安装说明:https://about.gitlab.com/installation/

13.3 GitLab 安装
13.3.1 服务器准备

准备一个系统为 CentOS7 以上版本的服务器,要求内存 4G,磁盘 50G。
关闭防火墙,并且配置好主机名和 IP,保证服务器可以上网。
此教程使用虚拟机:主机名:gitlab-server IP 地址:192.168.6.200

13.3.2 安装包准备

Yum 在线安装 gitlab- ce 时,需要下载几百 M 的安装文件,非常耗时,所以最好提前把
所需 RPM 包下载到本地,然后使用离线 rpm 的方式安装。
下载地址:


https://packages.gitlab.com/gitlab/gitlabce/packages/el/7/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm

注:资料里提供了此 rpm 包,直接将此包上传到服务器/opt/module 目录下即可。

13.3.3 编写安装脚本

安装 gitlab 步骤比较繁琐,因此我们可以参考官网编写 gitlab 的安装脚本。

[root@gitlab-server module]# vim gitlab-install.sh
sudo rpm -ivh /opt/module/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm
sudo yum install -y curl policycoreutils-python openssh-server cronie

sudo lokkit -s http -s ssh
sudo yum install -y postfix
sudo service postfix start
sudo chkconfig postfix on
curl https://packages.gitlab.com/install/repositories/gitlab/gitlabce/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="http://gitlab.example.com" yum -y install gitlabce

给脚本增加执行权限

[root@gitlab-server module]# chmod +x gitlab-install.sh
[root@gitlab-server module]# ll
总用量 403104
-rw-r--r--. 1 root root 412774002 4 月 7 15:47 gitlab-ce-13.10.2-
ce.0.el7.x86_64.rpm
-rwxr-xr-x. 1 root root 416 4 月 7 15:49 gitlab-install.sh

然后执行该脚本,开始安装 gitlab-ce。注意一定要保证服务器可以上网。

[root@gitlab-server module]# ./gitlab-install.sh 
警告:/opt/module/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm: 头 V4 
RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
准备中... ################################# 
[100%]
正在升级/安装...
 1:gitlab-ce-13.10.2-ce.0.el7 
################################# [100%]
13.3.4 初始化 GitLab 服务

执行以下命令初始化 GitLab 服务,过程大概需要几分钟,耐心等待…

[root@gitlab-server module]# gitlab-ctl reconfigure
。 。 。 。 。 。
Running handlers:
Running handlers complete
Chef Client finished, 425/608 resources updated in 03 minutes 08 
seconds
gitlab Reconfigured!
13.3.5 启动 GitLab 服务

执行以下命令启动 GitLab 服务,如需停止,执行 gitlab-ctl stop

[root@gitlab-server module]# gitlab-ctl start
ok: run: alertmanager: (pid 6812) 134s
ok: run: gitaly: (pid 6740) 135s
ok: run: gitlab-monitor: (pid 6765) 135s

ok: run: gitlab-workhorse: (pid 6722) 136s
ok: run: logrotate: (pid 5994) 197s
ok: run: nginx: (pid 5930) 203s
ok: run: node-exporter: (pid 6234) 185s
ok: run: postgres-exporter: (pid 6834) 133s
ok: run: postgresql: (pid 5456) 257s
ok: run: prometheus: (pid 6777) 134s
ok: run: redis: (pid 5327) 263s
ok: run: redis-exporter: (pid 6391) 173s
ok: run: sidekiq: (pid 5797) 215s
ok: run: unicorn: (pid 5728) 221s
13.3.6 使用浏览器访问 GitLab

使用主机名或者 IP 地址即可访问 GitLab 服务。需要提前配一下 windows 的 hosts 文件。
在这里插入图片描述
在这里插入图片描述

13.3.7 GitLab 创建远程库

在这里插入图片描述

13.3.8 IDEA 集成 GitLab

1)安装 GitLab 插件
在这里插入图片描述

3)push 本地代码到 GitLab 远程库

在这里插入图片描述

注意:gitlab 网页上复制过来的连接是:http://gitlab.example.com/root/git-test.git,
需要手动修改为:http://gitlab-server/root/git-test.git
选择 gitlab 远程连接,进行 push。

只要 GitLab 的远程库连接定义好以后,对 GitLab 远程库进行 pull 和 clone 的操作和
Github 和码云一致,此处不再赘述。

14、经典面试题

14.1 什么是 Git Flow,它有什么好处?

https://blog.csdn.net/ichen820/article/details/120371496

14.2 提交不小心出现误操作,如何撤销?

https://www.cnblogs.com/waiting-ying/p/13288643.html

15 资源

视频

⭐【尚硅谷】5h打通Git全套教程丨2021最新IDEA版 https://www.bilibili.com/video/BV1vy4y1s7k6

书籍

猴子都能懂的 Git 入门 https://backlog.com/git-tutorial/cn/

⭐ GitHub 漫游指南 https://github.phodal.com/

文档

GitHub 官方文档:https://docs.github.com/cn

游戏

Learning Git Branching:https://learngitbranching.js.org/?locale=zh_CN

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要同时配置gitgithubgitee,您需要按照以下步骤操作: 1. 在githubgitee上创建账户并创建仓库。 2. 在本地计算机上安装git。 3. 打开终端或命令行窗口,输入以下命令来配置gitgit config --global user.name "Your Name" git config --global user.email "[email protected]" 这将设置您的用户名和电子邮件地址,以便在提交代码时进行身份验证。 4. 生成SSH密钥并将其添加到您的githubgitee账户中。在终端或命令行窗口中输入以下命令: ssh-keygen -t rsa -C "[email protected]" 然后按照提示操作,将生成的公钥添加到您的githubgitee账户中。 5. 在本地计算机上创建一个新的git仓库,并将其与githubgitee仓库关联。在终端或命令行窗口中输入以下命令: git init git remote add origin git@github.com:yourusername/yourrepository.git git remote set-url --add origin git@gitee.com:yourusername/yourrepository.git 这将创建一个新的git仓库,并将其与您的githubgitee仓库关联。 6. 将您的代码添加到git仓库中,并将其推送到githubgitee仓库中。在终端或命令行窗口中输入以下命令: git add . git commit -m "Initial commit" git push -u origin master 这将将您的代码添加到git仓库中,并将其推送到您的githubgitee仓库中。 现在,您已经成功地配置了gitgithubgitee,并可以开始使用它们来管理您的代码了。 ### 回答2: Git是一款非常好用的版本管理系统,我们可以在其中配置多个远程仓库。这意味着我们可以在同一个本地Git仓库中,同时管理多个远程仓库,以实现不同平台之间的代码同步。 当我们使用Git时,首先需要在本地配置git的全局用户信息,如下所示: ``` $ git config --global user.name "Your Name" $ git config --global user.email "[email protected]" ``` 之后,我们需要在本地Git仓库中配置远程仓库的地址。假设我们要同时配置GitHubGitee,那么我们需要执行以下命令: ``` $ git remote add github https://github.com/yourusername/repo.git $ git remote add gitee https://gitee.com/yourusername/repo.git ``` 其中,"yourusername"是你在对应平台上的用户名,"repo.git"是你的仓库名称。执行以上命令后,Git会在本地仓库中添加两个远程仓库:githubgitee。 接下来,我们可以使用push命令将本地代码同步到远程仓库。如果我们要将代码同步到GitHubGitee,可以使用以下命令: ``` $ git push github master $ git push gitee master ``` 其中,"master"是分支名称,你也可以更改为其他分支名称。 除了push命令,我们还可以使用pull、fetch等命令从远程仓库获取代码或将远程仓库的代码合并到本地仓库中。 需要注意的是,当同一个本地仓库同时配置多个远程仓库时,你需要决定将哪一个仓库设为默认仓库,以防止误操作。你可以使用以下命令来设置默认仓库: ``` $ git push -u github master ``` 以上命令中的"-u"选项表示将github设置为默认仓库,以后在执行push命令时,就可以不用指定远程仓库的名称了。 综上所述,Git同时配置GitHubGitee非常简单,只需要执行几个简单的命令即可。只要你掌握了以上知识,就可以轻松实现多平台代码同步。 ### 回答3: Git 是一款强大的版本控制工具,可以为项目的开发和管理带来很多便利。在开发过程中,我们通常会将代码托管到代码托管平台上,比如 GithubGitee。为了更好地管理Git项目,我们需要配置 Git,以便同时连接 GithubGitee 两个平台。 首先,我们需要在GithubGitee上分别创建账号并登录。然后,我们需要生成 SSH key 并将其添加到 GithubGitee 上,以便在上传代码时进行身份验证。我们可以通过以下命令来生成 SSH key : ``` ssh-keygen -t rsa -C "[email protected]" ``` 生成成功后,我们需要在 GithubGitee 上的个人设置页面中添加 SSH key。 接下来,我们需要在本地以 Git Bash 的形式打开 Git ,在控制台中输入如下命令,来配置 GithubGitee : ``` git config --global user.name "用户名" git config --global user.email "邮箱地址" ``` 具体的用户名和邮箱地址需要根据自己的账号信息进行设置。这些信息会在用 Git 上传代码时作为身份信息,所以一定要输入正确的信息。 在 GithubGitee 的 repository 页面上,我们可以找到 Clone or download 按钮,在弹出的框中复制仓库的 SSH 地址。然后,我们可以使用如下命令克隆代码到本地: ``` git clone git@github.com:yourname/repository.git git clone git@gitee.com:yourname/repository.git ``` 其中,yourname 和 repository 分别是 GithubGitee 账号中相应的用户和仓库名。 当我们需要 Push 代码时,需要指定远程仓库的名字。我们可以使用如下命令添加远程仓库: ``` git remote add gitee git@gitee.com:yourname/repository.git git remote add github git@github.com:yourname/repository.git ``` 其中,giteegithub 分别是远程仓库的名字。我们可以使用如下命令将代码 Push 到对应的平台上: ``` git push gitee master git push github master ``` 以上就是同时配置 GithubGitee 的全部过程。在开发过程中,我们应当及时 Pull 远程仓库的代码,避免产生代码冲突。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值