文章目录
- 1、Git本地库命令
- 2、分支操作
- 3、Gitee远程库实操
- 4、跨团队协作-Fork应用
- 5、SSH免密登录
- 6、IDEA中使用Git本地仓库
-
- 6.1、Git忽略文件
- 6.2、定位Git程序
- [6.3 初始化本地库](#6.3 初始化本地库)
- [6.4 添加到暂存区](#6.4 添加到暂存区)
- [6.5 提交到本地库](#6.5 提交到本地库)
- [6.6 创建分支](#6.6 创建分支)
- [6.7 切换分支](#6.7 切换分支)
- [6.8 合并分支](#6.8 合并分支)
- [6.9 切换版本](#6.9 切换版本)
- 7、IDEA中连接Gitee远程仓库
-
- [7.1 设置Gitee账号](#7.1 设置Gitee账号)
- [7.2 分享工程到Gitee](#7.2 分享工程到Gitee)
- [7.3 clone](#7.3 clone)
- [7.4 push](#7.4 push)
- [7.5 pull](#7.5 pull)
- Git学习链接
1、Git本地库命令
命令名称 | 作用 |
---|---|
git init | 初始化本地库 |
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m "日志信息" 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
1.1、初始化本地库
- 基本语法
bash
git init
- 案例实操

初始化效果,会生成.git文件夹

1.2、设置用户签名
| 💡 签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做。
- 基本语法
bash
git config --global user.name 用户名
git config --global user.email 邮箱
- 案例实操
bash
git config --global user.name ZHANG Yaning
git config --global user.email yaning.zhang@sunmi.com
bash
#查看签名信息
git config user.name
git config user.email
#如果你想查看当前用户的所有Git配置信息,可以使用以下命令:
git config --list
#如果你想查看某个特定仓库的用户配置信息,可以在上述命令后加上--local参数,如:
git config --local user.name
git config --local user.email
git config --local --list
也可以在文件中查看
bash
cat ~/.gitconfig

1.3、查看本地库状态
- 基本语法
bash
git status
- 案例实操
-
工作区没有任何文件
-
检测到未追踪的文件
新建一个hello.txt后查看

- 检测到有新建的文件添加到了暂存区
执行
bash
git add hello.txt

- 提交完成后查看状态
bash
git commit -m "测试"

- 检测到工作区有文件被修改
修改hello.txt
-
工作区的修改添加到了暂存区
-
产生冲突(在后面讲分支操作后演示)
bash
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (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")
1.4、将工作区的修改添加到暂存区
- 基本语法
bash
git add 文件名
1.5、将暂存区的修改提交到本地库
- 基本语法
bash
git commit -m "日志信息" 文件名
$ 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
1.6、历史版本
- 查看历史版本
- 基本语法
bash
git reflog
- 案例实操
修改两次文件内容,分别add、commit
- 版本穿梭
- 基本语法
bash
git reset --hard 版本号
- 案例实操
2、分支操作
在版本控制过程中,同时推进多个任务

- 分支的好处
同时并行推进多个功能开发,提高开发效率;
各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。
- 分支操作常用命令
命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
2.1、查看分支
- 基本语法
bash
git branch -v

(*代表当前所在的分区)
2.2、创建分支
- 基本语法
bash
git branch 分支名
- 案例实操
bash
git branch hot-fix
git branch -v

2.3、分支合并时产生冲突
在两个不同分支分别做不同修改操作
- 在master分支修改内容
bash
--在maste分支上做修改
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ vim hello.txt
--提交到暂存区
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git add hello.txt
--提交到本地库
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git commit -m "forth main"
[master e427b0c] forth main
1 file changed, 1 insertion(+)
--查看分支
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git branch -v
hot-fix 2b5e6fe first modified
* master e427b0c forth main
--查看master分支上的文件内容
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ cat hello.txt
123
4567
- 切换分支到hot-fix分支并进行修改
- 基本语法
bash
git checkout 分支名称
- 案例实操
bash
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ git branch -v
* hot-fix 2b5e6fe first modified
master e427b0c forth main
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ cat hello.txt
123
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ vim hello.txt
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ git add hello.txt
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ git commit -m "forth hot-fix"
[hot-fix bca0a5f] forth hot-fix
1 file changed, 1 insertion(+)
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ cat hello.txt
123
4568
- 合并分支
- 基本语法
bash
git merge 分支名
- 案例实操
bash
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (hot-fix)
$ git checkout master
Switched to branch 'master'
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (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.
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (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")
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master|MERGING)
$ cat hello.txt
123
<<<<<<< HEAD
4567
=======
4568
>>>>>>> hot-fix
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master|MERGING)
$ vim hello.txt
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master|MERGING)
$ git add hello.txt
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master|MERGING)
$ git commit -m "merge hot-fix"
[master 290d289] merge hot-fix
--发现后面MERGING消失,变为正常
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ cat hello.txt
123
4567
4568
冲突产生的原因:
如果一个分支的内容是在另一个分支创建时生成的,那么在只修改一个分支相同文件相同位置的内容情况下,合并时将不会产出冲突。因为Git会自动合并简单的冲突。
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git无法替我们决定使用哪一个。必须人为决定新代码内容。
冲突的解决:
1)编辑有冲突的文件,删除特殊符号,决定要使用的内容
特殊符号:<<<<<<< HEAD=======>>>>>>> hot-fix
2)添加到暂存区
3)执行提交(注意:使用git commit命令时不能带文件名)
| 💡 master、hot-fix其实都是指向具体版本记录的指针。当前所在的分支,其实是由HEAD决定的。所以创建分支的本质就是多创建一个指针。 HEAD如果指向master,那么我们现在就在master分支上。 HEAD如果执行hotfix,那么我们现在就在hotfix分支上。 所以切换分支的本质就是移动HEAD指针。
3、Gitee远程库实操
在Gitee创建Gitee-Test仓库
远程仓库操作基本命令
命令名称 | 作用 |
---|---|
git remote -v | 查看当前所有远程地址别名 |
git remote add 别名 远程地址 | 起别名 |
git push 别名 分支 | 推送本地分支上的内容到远程仓库 |
git clone 远程地址 | 将远程仓库的内容克隆到本地 |
git pull 远程库地址别名 远程分支名 | 将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并 |
3.1、克隆远程仓库
- 基本语法
bash
git clone 远程地址
- 案例实操
bash
git clone https://gitee.com/zhangyaning666/gitee-test.git
clone会做如下操作:
1、拉取代码。2、初始化本地仓库。3、创建别名
3.2、创建远程仓库别名
- 基本语法
bash
#查看当前所有远程地址别名
git remote -v
#为远程仓库创建别名
git remote add 别名 远程地址
- 案例实操
bash
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git remote -v
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git remote add ori https://gitee.com/zhangyaning666/gitee-test.git
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹 (master)
$ git remote -v
ori https://gitee.com/zhangyaning666/gitee-test.git (fetch)
ori https://gitee.com/zhangyaning666/gitee-test.git (push)

3.3、推送本地分支上的内容到远程仓库
- 基本语法
bash
git push 别名 分支
- 案例实操
bash
SM2881@SMSHA1PF3DZPEC MINGW64 /f/测试文件夹/gitee-test (master)
$ git push ori
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 316 bytes | 316.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/zhangyaning666/gitee-test.git
5ab2247..1df7b38 master -> master

在git中,"push -f"的意思是"强制更新",是"push -force"的缩写,该命令的作用是将自己本地仓库的代码直接推送至仓库,完全以该命令提交为准,之前提交都会被覆盖。远程仓库中之前的版本记录也会被清除,慎用。
3.4、拉取远程库内容
- 基本语法
bash
git pull 远程库地址别名 远程分支名
- 案例实操
bash
git pull ori master
4、跨团队协作-Fork应用
1)将远程仓库的地址复制发给邀请跨团队协作的人。
2)在zhang real的Gitee账号里的地址栏复制收到的链接,然后点击Fork将项目叉到自己的本地仓库。
fork成功后可以看到当前仓库信息。

3)zhang real就可以在线编辑叉取过来的文件。

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

5)接下来点击上方的Pull请求,并创建一个新的请求。
6)回到"时光"Gitee账号可以看到有一个Pull request请求。
进入到聊天室,可以讨论代码相关内容。。。
7)如果代码没有问题,可以合并代码。
5、SSH免密登录
我们可以看到远程仓库中还有一个SSH的地址,因此我们也可以使用SSH进行访问。
具体操作如下:
powershell
$ git init
Initialized empty Git repository in F:/笔记/14.Git/1.笔记/Git-SSH/.git/
$ git config user.name
zyn
$ git config user.email
zyn@qq.com
$ cd .git/
HEAD description info/ refs/
config hooks/ objects/
--进入当前用户的家目录
$ cd
$ pwd
/c/Users/10420
--删除.ssh目录
$ rm -rvf .ssh
removed '.ssh/id_rsa'
removed '.ssh/id_rsa.pub'
removed '.ssh/known_hosts'
removed '.ssh/known_hosts.old'
removed directory '.ssh'
--运行命令生成.ssh秘钥目录[注意:这里-C这个参数是大写的C]
$ ssh-keygen -t rsa -C zhangyaning666
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/10420/.ssh/id_rsa):
Created directory '/c/Users/10420/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/10420/.ssh/id_rsa
Your public key has been saved in /c/Users/10420/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:BBb98gotyTriimE4bkBRtYe6esC4blmiWjRa+eNZWxY zhangyaning666
The key's randomart image is:
+---[RSA 3072]----+
| ....+o |
| . .o.. |
| . o ... |
| . .. ... . |
|+ =. . oEo |
|+B +. = ... |
|Bo*.o..oo. |
|*Booo+ +. |
|O*o.o.. |
+----[SHA256]-----+
--进入.ssh目录查看文件列表
$ cd .ssh
$ ll -a
total 37
drwxr-xr-x 1 10420 197609 0 Oct 19 22:17 ./
drwxr-xr-x 1 10420 197609 0 Oct 19 22:17 ../
-rw-r--r-- 1 10420 197609 2602 Oct 19 22:17 id_rsa
-rw-r--r-- 1 10420 197609 568 Oct 19 22:17 id_rsa.pub
--查看id_rsa.pub文件内容
$ cat id_rsa.pub
ssh-rsa *****
复制id_rsa.pub文件内容,登录Gitee,点击用户头像→设置→SSH公钥
接下来再往远程仓库push东西的时候使用SSH连接就不需要登录了。
6、IDEA中使用Git本地仓库
6.1、Git忽略文件
1)Eclipse特定文件

2)IDEA特定文件

3)Maven工程的target目录

问题1:为什么要忽略他们?
与项目的实际功能无关,不参与服务器上部署运行。把它们忽略掉能够屏蔽IDE工具之间的差异。
问题2:怎么忽略?
- 创建忽略规则文件xxxx.ignore(前缀名随便起)
这个文件的存放位置原则上在哪里都可以,为了便于让~/.gitconfig文件引用,建议也放在用户家目录下
xxxx.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 = zyn
email = zyn@qq.com
[core]
excludesfile = C:/Users/10420/hh.ignore
注意:这里要使用"正斜线(/)",不要使用"反斜线(\)"
6.2、定位Git程序

6.3 初始化本地库

选择要创建Git本地仓库的工程。

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

6.5 提交到本地库


6.6 创建分支



6.7 切换分支
在IDEA窗口的右下角,切换到master分支。

6.8 合并分支
在IDEA窗口的右下角,将hot-fix分支合并到当前master分支。

6.9 切换版本
在IDEA的左下角,点击Version Control,然后点击Log查看版本

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

7、IDEA中连接Gitee远程仓库
7.1 设置Gitee账号
安装Gitee插件

点击登录。

7.2 分享工程到Gitee


来到Gitee中发现已经帮我们创建好了gitTest的远程仓库。

7.3 clone


为clone下来的项目创建一个工程,一路Next。


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



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