尚硅谷Git入门到精通笔记

目录

一、课程内容

二、Git概述

2.1 工作机制

2.2 代码托管中心

 三、Git常用命令

3.1 设置用户签名

3.2 初始化本地库

3.3 查看本地库状态

3.4 添加暂存区

3.5 提交本地库

3.6 版本穿梭

四、分支

4.1 分支的操作

 4.1.1 查看分支

4.1.2 创建分支

4.1.3 切换分支

4.1.4 合并分支

4.1.5 合并分支(冲突合并)

五、Git 团队协作机制

六、Github操作 

6.1 创建远程库

6.2 远程仓库操作

6.2.1 创建远程仓库别名 

6.2.2 推送本地库到远程库

6.2.3 拉取远程库到本地库

6.2.4 团队内协作

 6.2.5 跨团队协作

6.2.6 SSH免密登录

 七、IDEA集成Git

7.1 配置 Git忽略文件

7.2 在IDEA配置Git程序

7.3 初始化本地库

7.4 添加到暂存区

7.5 提交至本地库

7.6 切换版本

7.7 创建分支

7.8 切换分支

7.9 合并分支

7.10 合并分支(冲突合并)

八、IDEA集成GitHub

8.1 设置 GitHub

8.2 分享工程到 GitHub 

8.3 推送代码到远程库

8.4 pull拉取远程库到本地库

8.5 克隆代码到本地

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

9.1 码云简介

9.2 账号注册登录

9.3 创建远程库

9.4 IDEA集成Gitee码云 

9.5 IDEA连接码云

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


一、课程内容

尚硅谷Git入门到精通笔记_第1张图片

二、Git概述

2.1 工作机制

尚硅谷Git入门到精通笔记_第2张图片

2.2 代码托管中心

 三、Git常用命令

尚硅谷Git入门到精通笔记_第3张图片

3.1 设置用户签名

git config --global user.name abc #有户名
git config --global user.email [email protected]

3.2 初始化本地库

基本语法:git init

对于新建项目,需要在根目录执行git init,建立本地仓库。

git clone拉取的项目不需要再初始化本地库,因为clone命令包含init。

3.3 查看本地库状态

基本语法:git status

案例实操:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop
$ cd HelloGit/

# 首次查看(工作区没有任何文件)
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

# 新增文件(hello.txt)
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ vim hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ cat hello.txt
hello, git!

# 再次查看(检测到未追踪的文件)
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch 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)

3.4 添加暂存区

基本语法:git add 文件名

       git add . #添加目录下所有文件

案例实操:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch 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)

#添加至暂存区
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (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

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   hello.txt

#移除暂存区的hello.txt
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git rm --cached hello.txt
rm 'hello.txt'

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch 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)

# 再次添加
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (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

3.5 提交本地库

基本语法:git commit -m "日志信息" 文件名

案例实操:

#提交本地库
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git commit -m "first commit" hello.txt
[master (root-commit) b0006bc] first commit
 1 file changed, 19 insertions(+)
 create mode 100644 hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch master
nothing to commit, working tree clean

两种查看提交记录:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git reflog
b0006bc (HEAD -> master) HEAD@{0}: commit (initial): first commit

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git log
commit b0006bc538b98b7eb77b4b4aaa17b6e333c4289e (HEAD -> master)
Author: abc 
Date:   Tue Jul 6 00:38:24 2021 +0800

    first commit

HEAD -> master表示:指针指向master分支

3.6 版本穿梭

查看历史版本

基本语法:

  • git reflog 查看版本信息

  • git log 查看版本详细信息

git log与git reflog区别

git log 命令可以显示所有提交过的版本信息,如果感觉太繁琐,可以加上参数 --pretty=oneline,只会显示版本号和提交时的备注信息。

git reflog 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)。例如,执行 git reset --hard HEAD~1,退回到上一个版本,用git log则是看不出来被删除的commitid,用git reflog则可以看到被删除的commitid,我们就可以买后悔药,恢复到被删除的那个版本。

版本穿梭

基本语法:git reset --hard 版本号

# 首先查看当前的历史记录
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git reflog
41f776b (HEAD -> master) HEAD@{0}: commit: third commit
6967bf0 HEAD@{1}: commit: second commit
b0006bc HEAD@{2}: commit (initial): first commit

# 切换到 b0006bc 版本,也就是第一次提交的版本
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git reset --hard b0006bc
HEAD is now at b0006bc first commit

# 切换完毕之后再查看历史记录,当前成功切换到了 b0006bc 版本
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git reflog
b0006bc (HEAD -> master) HEAD@{0}: reset: moving to b0006bc
41f776b HEAD@{1}: commit: third commit
6967bf0 HEAD@{2}: commit: second commit
b0006bc (HEAD -> master) HEAD@{3}: commit (initial): first commit

# 然后查看文件 hello.txt,发现文件内容回到第一版本
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ cat hello.txt
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!

Git 切换版本, 底层其实是移动的 HEAD 指针。不但可以返回之前的版本,也可以从第一个版本穿到第三个版本。

四、分支

尚硅谷Git入门到精通笔记_第4张图片

什么是分支

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

尚硅谷Git入门到精通笔记_第5张图片

分支的好处 

        同时并行推进多个功能开发,提高开发效率。

        各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。

4.1 分支的操作

尚硅谷Git入门到精通笔记_第6张图片

 4.1.1 查看分支

基本语法:git branch -v

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git branch -v
* master b0006bc first commit

4.1.2 创建分支

基本语法:git branch 分支名

#从当前分支(master)创建新分支

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git branch hot-fix

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git branch -v
  hot-fix b0006bc first commit
* master  b0006bc first commit

刚创建的新的分支,并将主分支master的内容复制了一份

4.1.3 切换分支

基本语法:git checkout 分支名

# 切换到刚创建的分支上
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git branch -v
* hot-fix b0006bc first commit
  master  b0006bc first commit

切换分支后,在新分支修改文件:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ vim hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!


abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git status
On branch hot-fix
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   hello.txt

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

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git add hello.txt

#在hot-fix提交
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git commit -m "hot-fix first commit"
[hot-fix 25f62d6] hot-fix first commit
 1 file changed, 1 insertion(+), 1 deletion(-)

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git reflog
25f62d6 (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix first commit
b0006bc (master) HEAD@{1}: checkout: moving from master to hot-fix
b0006bc (master) HEAD@{2}: reset: moving to b0006bc
5f8dbf6 HEAD@{3}: commit: forth commit
b0006bc (master) HEAD@{4}: reset: moving to b0006bc
41f776b HEAD@{5}: commit: third commit
6967bf0 HEAD@{6}: commit: second commit
b0006bc (master) HEAD@{7}: commit (initial): first commit

4.1.4 合并分支

基本语法:git merge 分支名

在 master 分支上合并 hot-fix 分支(将hot-fix的合并到master),先切换到master分支。

# 首先要切换到master分支上
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git checkout master
Switched to branch 'master'

#将hot-fix的合并到master
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git merge hot-fix
Updating b0006bc..25f62d6
Fast-forward
 hello.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

#合并后,可以在master分支上看到hot-fix上分支对文件的修改
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!

#合并后,hot-fix分支依然存在,并未消失
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git branch -v
  hot-fix 25f62d6 hot-fix first commit
* master  25f62d6 hot-fix first commit

4.1.5 合并分支(冲突合并)

冲突产生的原因

        合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git无法替我们决定使用哪一个。必须人为决定新代码内容。

产生冲突

        首先,在master修改文件hello.txt最后一行内容,并提交:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ vim hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!master test

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   hello.txt

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

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git add hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git commit -m "master test"
[master fb0e30b] master test
 1 file changed, 2 insertions(+), 2 deletions(-)

然后,在hot-fix修改文件hello.txt最后一行内容,并提交:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ vim hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!hot-fix test

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git add hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git commit -m "hot-fix test"
[hot-fix 47d2d8f] hot-fix test
 1 file changed, 1 insertion(+), 1 deletion(-)

切换到master分支,然后将hot-fix分支的合并到master,冲突产生:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (hot-fix)
$ git checkout master
Switched to branch 'master'

# 冲突产生
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (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.

# MERGING 出现,表示有冲突待解决
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (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 ..." to mark resolution)
        both modified:   hello.txt

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

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master|MERGING)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
<<<<<<< HEAD
hello, git!hot-fix test
hello, git!master test
=======
hello, git!
hello, git!hot-fix test
>>>>>>> hot-fix

解决冲突

        编辑有冲突的文件,删除特殊符号,决定要使用的内容。然后再add和commit一次

特殊符号:

<<<<<<< HEAD
当前分支的代码 
======= 
合并过来的代码 
>>>>>>> hot-fix

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

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master|MERGING)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
<<<<<<< HEAD
hello, git!hot-fix test
hello, git!master test
=======
hello, git!
hello, git!hot-fix test
>>>>>>> hot-fix

#手动删除冲突
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master|MERGING)
$ vim hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master|MERGING)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!hot-fix test
hello, git!master test

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (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 ..." to mark resolution)
        both modified:   hello.txt

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

#需要再commit一次,且不能指定文件名
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master|MERGING)
$ git add hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master|MERGING)
$ git commit -m "conflict solved"
[master 785ab46] conflict solved

# (master|MERGING)的|MERGING消失了,冲突解决
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git reflog
785ab46 (HEAD -> master) HEAD@{0}: commit (merge): conflict solved
fb0e30b HEAD@{1}: checkout: moving from hot-fix to master
47d2d8f (hot-fix) HEAD@{2}: commit: hot-fix test
25f62d6 HEAD@{3}: checkout: moving from master to hot-fix
fb0e30b HEAD@{4}: commit: master test
25f62d6 HEAD@{5}: checkout: moving from hot-fix to master
25f62d6 HEAD@{6}: checkout: moving from master to hot-fix
25f62d6 HEAD@{7}: checkout: moving from hot-fix to master
25f62d6 HEAD@{8}: checkout: moving from master to hot-fix
25f62d6 HEAD@{9}: merge hot-fix: Fast-forward
b0006bc HEAD@{10}: checkout: moving from hot-fix to master
25f62d6 HEAD@{11}: commit: hot-fix first commit
b0006bc HEAD@{12}: checkout: moving from master to hot-fix
b0006bc HEAD@{13}: reset: moving to b0006bc
5f8dbf6 HEAD@{14}: commit: forth commit
b0006bc HEAD@{15}: reset: moving to b0006bc
41f776b HEAD@{16}: commit: third commit
6967bf0 HEAD@{17}: commit: second commit
b0006bc HEAD@{18}: commit (initial): first commit

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$

创建分支和切换分支

master、 hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD决定的。所以创建分支的本质就是多创建一个指针。

        HEAD 如果指向 master,那么我们现在就在 master 分支上。
        HEAD 如果执行 hot-fix,那么我们现在就在 hot-fix 分支上。
所以切换分支的本质就是移动 HEAD 指针。

五、Git 团队协作机制

团队内协作

尚硅谷Git入门到精通笔记_第7张图片

 跨团队协作

尚硅谷Git入门到精通笔记_第8张图片

六、Github操作 

6.1 创建远程库

尚硅谷Git入门到精通笔记_第9张图片

 尚硅谷Git入门到精通笔记_第10张图片

6.2 远程仓库操作

尚硅谷Git入门到精通笔记_第11张图片

6.2.1 创建远程仓库别名 

基本语法

  • git remote -v 查看当前所有远程地址别名
  • git remote add 别名 远程地址
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git remote -v

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git remote add hellogit https://github.com/abc/HelloGit.git

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git remote -v
hellogit        https://github.com/abc/HelloGit.git (fetch)
hellogit        https://github.com/abc/HelloGit.git (push)

(fetch)和(push)表示clone、pull和push

6.2.2 推送本地库到远程库

基本语法:git push 别名 分支

# 将master分支推送到别名为hellogit远程地址,
# 也就推送到https://github.com/abc/HelloGit.git(具体看前一节)
# 这里需要授权认证操作(输入账号密码)
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git push hellogit master
fatal: unable to access 'https://github.com/abc/HelloGit.git/': OpenSSL SSL_read: Connection was reset, errno 10054

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ git push hellogit master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (13/13), 1.02 KiB | 523.00 KiB/s, done.
Total 13 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/abc/HelloGit.git
 * [new branch]      master -> master

6.2.3 拉取远程库到本地库

基本语法:git pull 别名 分支

在远程库获取地址URL

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit (master)
$ cd ..

# 创建一个新文件夹
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop
$ mkdir HelloGit-clone

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop
$ cd HelloGit-clone/

# 在新的文件夹内,克隆远程库到本地
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone
$ git clone https://github.com/abc/HelloGit.git
Cloning into 'HelloGit'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 16 (delta 5), reused 12 (delta 4), pack-reused 0
Receiving objects: 100% (16/16), done.
Resolving deltas: 100% (5/5), done.
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone

$ git remote -v
fatal: not a git repository (or any of the parent directories): .git

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone
$ ls
HelloGit/

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone
$ cd HelloGit/

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ git remote -v
origin  https://github.com/abc/HelloGit.git (fetch)
origin  https://github.com/abc/HelloGit.git (push)

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ git reflog
47e257f (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: clone: from https://github.com/JallenKwong/HelloGit.git

clone 会做如下操作:

  1. 拉取代码。
  2. 初始化本地仓库。
  3. 创建别名(origin)。

6.2.4 团队内协作

一、选择邀请合作者。(在仓库设置里操作)

尚硅谷Git入门到精通笔记_第12张图片

二、填入目标合作者。

尚硅谷Git入门到精通笔记_第13张图片

 三、复制网址发送给你目标合作者 , 复制内容如下:https://github.com/atguiguyueyue/git-shTest/invitations。

 四、目标合作者接收到网址,用浏览器打开它,点击接受邀请。

尚硅谷Git入门到精通笔记_第14张图片

 五、接受邀请成功之后,可以在目标合作者Github账号上看到将来共同开发远程仓库。

六、目标合作者可以修改内容并 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 url 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

 七、回到发送合作邀请者的 GitHub 远程仓库中可以看到,最后一次是目标合作者提交的。

尚硅谷Git入门到精通笔记_第15张图片

 6.2.5 跨团队协作

尚硅谷Git入门到精通笔记_第16张图片

 一、将远程仓库的地址复制发给邀请跨团队协作的人,比如东方不败。

二、在东方不败的 GitHub 账号里的地址栏复制收到的链接,然后点击 网页右上方的Fork按钮,将项目叉到自己的本地仓库。

叉成功后可以看到当前仓库信息。

尚硅谷Git入门到精通笔记_第17张图片

 三、东方不败就可以在线编辑或git clone叉取过来的文件。

尚硅谷Git入门到精通笔记_第18张图片

 四、编辑完毕后,填写描述信息并点击左下角绿色按钮提交。

尚硅谷Git入门到精通笔记_第19张图片

五、接下来点击上方的 Pull 请求,并创建一个新的请求。

尚硅谷Git入门到精通笔记_第20张图片

六、回到岳岳 GitHub 账号可以看到有一个 Pull request 请求。

尚硅谷Git入门到精通笔记_第21张图片进入到聊天室,可以讨论代码相关内容。 

 尚硅谷Git入门到精通笔记_第22张图片

七、如果代码没有问题,可以点击 Merge pull reque 合并代码。

尚硅谷Git入门到精通笔记_第23张图片

6.2.6 SSH免密登录

 我们可以看到远程仓库中还有一个 SSH 的地址,因此我们也可以使用 SSH 进行访问。

尚硅谷Git入门到精通笔记_第24张图片

 先到用户的主页目录,删除.ssh文件夹(如果没有.ssh文件夹,忽略此步):

abc@DESKTOP-R85C9HV MINGW64 ~
$ cd ~

abc@DESKTOP-R85C9HV MINGW64 ~
$ pwd
/c/Users/abc

abc@DESKTOP-R85C9HV MINGW64 ~
$ ls -a .ssh
./  ../  id_rsa  id_rsa.pub

abc@DESKTOP-R85C9HV MINGW64 ~
$ rm -rf .ssh

abc@DESKTOP-R85C9HV MINGW64 ~
$ ls -a .ssh
ls: cannot access '.ssh': No such file or directory

 运行命令ssh-keygen生成.ssh目录:

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ ssh-keygen -t rsa -C [email protected]
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/abc/.ssh/id_rsa):
Created directory '/c/Users/abc/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/abc/.ssh/id_rsa
Your public key has been saved in /c/Users/abc/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:aeNMB/hP2yiH/Dka2jK9BJciSgA8yKKLlKXX8oei7J0 [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|=                |
|++ .   .         |
|+ = . . .        |
|.= o . . +       |
|o.o + + S o      |
|o. o + @ * +     |
|. o . ..O = .    |
| o. . o+.=..     |
|.. E  .o+oo.     |
+----[SHA256]-----+

abc@DESKTOP-R85C9HV MINGW64 ~
$ ls -a .ssh
./  ../  id_rsa  id_rsa.pub

# 生成公钥
abc@DESKTOP-R85C9HV MINGW64 ~
$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQChXy8I20br9nu4GCNeZSDkozfHvlRFpXiImYnVlHVvyvFgjct1/zMeJgot1J6+yArSJbA4TMlS9nG8owCE6C9yqhPceDlKtQbARKS2pW7IyP5OhIbcqVmWmvvd+IMmsWrWgK9S6jqp0xSqv3Z3mlcHWOAK18oOe6wF6b3SyGgCP/EcwwUGX4NG7jukhK+In9joSuAxchEg/Ba2/LVjqtfBn3hXZx/SEt+rJ0UVPIT/eEe32HflrzokNcO7l0IgyLntv5QEAsSC2hiGxrM6vF5tQpb12MVZnt1/01ytP0ruQn2TVTI96vsOAa3Cj98dAH2Z0JdqZUSVBw+o3AqXP5oeF1JWkDHZzHQjLgu741wnUZn+vVXFBu1xQyApbvH7y7cNbq8PaxU+SyZbVXbq3RwTywJsyFQvsIOM5l0tG7jUD0QAd6dP3rcNODjFTaafJaBsR9aMwvKQd/d7H+BdwFPYOFp8HB2JAzhRpvlS4Av9MCIe0474wZ0T2QOJmcs7mns= [email protected]

然后,将生成的公钥添加至Github账号SSH设置

尚硅谷Git入门到精通笔记_第25张图片

 尚硅谷Git入门到精通笔记_第26张图片

 尚硅谷Git入门到精通笔记_第27张图片

尚硅谷Git入门到精通笔记_第28张图片

尚硅谷Git入门到精通笔记_第29张图片

添加公钥后,可不用输入Github账号密码便可推送。

接下来通过SSH方式提交hello.txt。

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!modify from Github editor
hello, git!hot-fix test
hello, git!master test

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ vim hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ cat hello.txt
hello, git!hot-fix
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!ssh test
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!
hello, git!modify from Github editor
hello, git!hot-fix test
hello, git!master test

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ git add .

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   hello.txt

abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ git commit -m "ssh test"
[master 9602a37] ssh test
 1 file changed, 1 insertion(+), 1 deletion(-)

# 通过SSH推送
abc@DESKTOP-R85C9HV MINGW64 ~/Desktop/HelloGit-clone/HelloGit (master)
$ git push [email protected]:abc/HelloGit.git master
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:JallenKwong/HelloGit.git
   47e257f..9602a37  master -> master

 七、IDEA集成Git

7.1 配置 Git忽略文件

        与项目的实际功能无关,不参与服务器上部署运行。把它们忽略掉能够屏蔽 IDE 工具之
间的差异。例如,Maven工程根据src生成的target。

        创建忽略规则文件 xxxx.ignore(前缀名随便起,建议是 git.ignore),这个文件的存放位置原则上在哪里都可以,为了便于让~/.gitconfig 文件引用,建议也放在用户家目录下。

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 文件中引用忽略配置文件(此文件在 Windows 的家目录中)

[user]
    name = Layne
    email = [email protected]
[core]
	excludesfile = C:/Users/asus/git.ignore

注意:这里要使用“正斜线(/)”,不要使用“反斜线(\)”

7.2 在IDEA配置Git程序

在菜单栏File->Setting->搜索栏搜Git,配置Git的安装路径。

尚硅谷Git入门到精通笔记_第30张图片

7.3 初始化本地库

先创建一个名叫HelloGit的Maven工程。

在菜单栏VCS -> Import into Version Control -> Create Git Repository

尚硅谷Git入门到精通笔记_第31张图片

 选择要创建 Git 本地仓库的工程,也就是HelloGit工程,然后添加OK。

尚硅谷Git入门到精通笔记_第32张图片

7.4 添加到暂存区

创建一个HelloGit类,将其添加Git暂存区。

右键点击HelloGit类,选择Git->Add。可以右键点击HelloGit,更大范围地添加文件到暂存区。

尚硅谷Git入门到精通笔记_第33张图片

 添加成功后,文件名会从红色变成绿色。

7.5 提交至本地库

右键点击HelloGit,选择Git->Commit Directory。

尚硅谷Git入门到精通笔记_第34张图片

尚硅谷Git入门到精通笔记_第35张图片 添加成功后,后台打印相关信息。

7.6 切换版本

在 IDEA 的左下角,点击 Git,然后点击 Log 查看版本

右键选择要切换的版本,然后在菜单里点击 Checkout Revision。

7.7 创建分支

右键点击HelloGit,Git -> Repository -> Branches,或者点击IDEA的右下角,如图红圈所示部位:

选择点击New Branch:

创建新分支:

7.8 切换分支

创建分支步骤相似,如点击IDEA的右下角(它显示项目正处在那条分支),如图红圈所示部位,选择你想要切换的分支,然后checkout:

  

或者在log窗口,右键点击分支,选择checkout:

7.9 合并分支

先在hot-fix分支修改HelloGit类,并将其提交:

然后切换到master分支,右下角的hot-fix会变为master:

然后,点击IDEA 窗口的右下角的master,将 hot-fix 分支合并到当前 master 分支。选择hot-fix->Merge into Current

如果代码没有冲突, 分支直接合并成功,分支合并成功以后,代码自动提交,无需手动提交本地库。

 

7.10 合并分支(冲突合并)

分别在master,hot-fix分支修改HelloGit类同一行,并提交,故意制作冲突:

切换到master分支,将hot-fix的合并到master分支: 

 

冲突产生,需要人工解决:

尚硅谷Git入门到精通笔记_第36张图片

看代码抬头,“Changes from master”,中间是结果.。“×”取消,“》”会输入到Result

尚硅谷Git入门到精通笔记_第37张图片

手动合并完代码以后,点击右下角的 Apply按钮。 

代码冲突解决,将代码提交本地库后,如图所示:

八、IDEA集成GitHub

8.1 设置 GitHub

在菜单栏File->Setting->搜索栏搜GitHub,添加GitHub账号:

由于网络问题,会时常登陆不了:

解决方法:可通过Token登陆。

登陆Github网站,获取Token,操作步骤看下图:

 

将生成的token用来IDEA登录。 

8.2 分享工程到 GitHub 

 

8.3 推送代码到远程库

右键点击项目,可以将当前分支的内容push到 GitHub的远程仓库中 。

尚硅谷Git入门到精通笔记_第38张图片

 尚硅谷Git入门到精通笔记_第39张图片

 尚硅谷Git入门到精通笔记_第40张图片

尚硅谷Git入门到精通笔记_第41张图片

尚硅谷Git入门到精通笔记_第42张图片

 注意: push 是将本地库代码推送到远程库,如果本地库代码跟远程库代码版本不一致,
push 的操作是会被拒绝的。也就是说, 要想 push 成功,一定要保证本地库的版本要比远程库的版本高! 因此一个成熟的程序员在动手改本地代码之前,一定会先检查下远程库跟本地代码的区别!如果本地的代码版本已经落后,切记要先 pull 拉取一下远程库的代码,将本地代码更新到最新以后,然后再修改,提交,推送!

8.4 pull拉取远程库到本地库

右键点击项目,可以将远程仓库的内容 pull 到本地仓库。

尚硅谷Git入门到精通笔记_第43张图片

注意: pull 是拉取远端仓库代码到本地,如果远程库代码和本地库代码不一致,会自动合并,如果自动合并失败,还会涉及到手动解决冲突的问题。 

8.5 克隆代码到本地

在菜单栏的File->Close Project->Get from Version Control。

或者在菜单栏VCS->Get from Version Control。

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

9.1 码云简介

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

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


9.2 账号注册登录

9.3 创建远程库

跟Github的类似。

另外,可以从GitHub与GitLab中导入仓库。

9.4 IDEA集成Gitee码云 

 首先,要在IDEA安装Gitee插件

在菜单栏选File->Settings->Plugins,搜Gitee。

安装插件成功后,重启IDEA。

功能跟在IDEA的Github插件,功能类似,如添加Gitee账号等,可参考前文IDEA的Github插件,触类旁通

 

9.5 IDEA连接码云

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

将本地代码 push到码云远程库

尚硅谷Git入门到精通笔记_第44张图片

自定义远程库链接。

尚硅谷Git入门到精通笔记_第45张图片

 给远程库链接定义个name,然后再 URL里面填入码云远程库的 HTTPS链接即可 。 码云服务器器在国内,用在国内,用HTTPS链接即可,没必要用SSH免密链接。

尚硅谷Git入门到精通笔记_第46张图片

 然后选择定义好的远程链接,点击 Push即可。

尚硅谷Git入门到精通笔记_第47张图片

 看到提示就说明Push远程库成功。

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

 如何搭建参考尚硅谷视频41_尚硅谷_Git_GitLab_简介和安装环境准备_哔哩哔哩_bilibili

十一、经验

11.1 追加最近一次的commit

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。

此时,可以运行带有 --amend 选项的提交命令尝试重新提交:

如果有一个新的文件修改,也想提交到上一个commit中(不改动描述信息)

这里的提交就是直接将忘记的文件合在上一次提交中编辑信息还是上次的

  1. git add .

  2. git commit --amend --no-editgit 修改已经提交到git远程仓库的commit信息

11.2 跳过使用暂存区域

git commit -a -m '信息'   ==>>(等价于) git add .  &&  git commit -m '信息'

11.3 修改commit信息

11.3.1 git 如何修改最近一次的commit信息

        1. git commit --amend
       2. 进入vim操作界面之后, 点击字母键 i 然后进入INSERT模式,然后对commit信息进行修改,然后ESC 然后 :wq 保存退出
       3. 然后执行 git log 会发现最近的一次commit信息被修改成功了

11.3.2 git修改已经提交到git远程仓库的commit信息(没试过)

       1. 首先回撤到上一次提交,不修改本次提交的改动 git reset --soft HEAD~1

       2. 重新做一次提交 git commit -m “重新提交commit信息”

       3. 强推本地分支到远程仓库 git push -f origin master

       4. 查看远程仓库中的是否已经修改commit信息

11.3.3 push后出错,回滚代码

在所选分支的版本,右键回滚

soft和Mixed回滚后仍会保存写的代码,只不过soft会存在暂存区,而mixed不会。hard会彻底回退上一个版本

尚硅谷Git入门到精通笔记_第48张图片

然后git push -f

11.3.4 IDEA中看不到新建的远程分支

git fetch

11.3.5 代码提交到错误的分支解决

前提是尚未将修改提交到错误的分支

> git stash //将修改的代码暂存到stash

> git checkout rightbranch //切换到正确的分支

> git stash pop //从stashz中去除暂存的代码修改

> git add .

> git commit -m xxx

————————————————
版权声明:本文引用了CSDN博主「巨輪」的原创文章。
引用原文链接:https://blog.csdn.net/u011863024/article/details/118562748

你可能感兴趣的:(#,Git,Java,笔记)