Git概述和安装
Git 是目前最主流的 分布式版本控制系统
(Version Control System),是团队协作开发不可或缺的工具。它可以保存和管理文件的所有更新记录、并且使用 版本号 进行区分。从而支持将编辑后的文档恢复到修改前的状态(历史版本)、对比不同版本的文件差异、防止旧版本覆盖新版本等功能。
资源
⭐️ Git 官方下载:https://git-scm.com/downloads(opens new window)
Git 可视化管理软件:
一般情况下不用额外安装软件,用开发工具自带的 Git 可视化功能即可(比如 IDEA、VS Code 等)
- ⭐️ GitKraken:https://www.gitkraken.com/(炫酷且友好)
- Sourcetree:https://www.sourcetreeapp.com/(opens new window)
- TortoiseGit:https://tortoisegit.org/(opens new window)
- GitHub Desktop:https://desktop.github.com/(opens new window)
- Git GUI(安装 Git 后自带)
在线文档:
- ⭐️ 猴子都能懂的 Git 入门:https://backlog.com/git-tutorial/cn/(强烈推荐)
- 菜鸟教程:https://www.runoob.com/git/git-tutorial.html(opens new window)
- 廖雪峰的教程:https://www.liaoxuefeng.com/wiki/896043488029600/(opens new window)
- Git 简明指南:http://rogerdudler.github.io/git-guide/index.zh.html (opens new window)(几分钟看一遍就好,可以用来复习)
视频教程:
Git 相关的入门视频太多了,B 站随便一搜一大把
- 一节课入门:https://www.bilibili.com/video/BV1s3411g7PS (opens new window)(18 分钟)
- 一节课入门:https://www.bilibili.com/video/BV1KD4y1S7FL (opens new window)(10 分钟)
- 5h 打通 Git 全套教程:https://www.bilibili.com/video/BV1vy4y1s7k6 (opens new window)(较完整,时间多的话可以看)
在线游戏:
- ⭐️ Learning Git Branching:https://learngitbranching.js.org/?locale=zh_CN (opens new window)(帮助你学习 Git 分支的用法)
工具:
- ⭐️ Git 命令大全:https://backlog.com/git-tutorial/cn/reference/ (opens new window)(适合收藏)
- Git 参考手册:http://gitref.justjavac.com/ (opens new window)(只列举了常用的命令,不是很全)
Git的常用命令
git的首次安装必须先设置一下用户签名,否则将无法提交代码
设置用户签名
作用:区分不同操作者身份
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop
$ git config --global user.name mufeng
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop
$ git config --global user.mail mufeng@qq.com
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop
$ git config user.name
mufeng
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop
$ git config user.mail
mufeng@qq.com
初始化本地库—git init
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-
$ git init
Initialized empty Git repository in C:/Users/沐枫/Desktop/Java基础/Git/Git-Spaces/git_demo/.git/
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ ll -a
total 4
drwxr-xr-x 1 沐枫 197121 0 Feb 5 17:18 ./
drwxr-xr-x 1 沐枫 197121 0 Feb 5 17:17 ../
drwxr-xr-x 1 沐枫 197121 0 Feb 5 17:18 .git/
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
查看本地库状态—git status
首次查看(工作区没有任何文件)
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
新增文件之后查看
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ vim hello.txt
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ ll
total 1
-rw-r--r-- 1 沐枫 197121 140 Feb 5 17:22 hello.txt
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/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)
再次查看–文件已经提交到暂存区的状态
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/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
添加文件到暂存区—git add <file>
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
从暂存区删除文件—git rm --cached <file>
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git rm --cached hello.txt
rm 'hello.txt'
提交本地库–git commit -m “message” <file>
将暂存区的文件提交到本地库
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git commit -m "first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) f2ab7aa] first commit
1 file changed, 10 insertions(+)
create mode 100644 hello.txt
查看状态(没有文件需要提交)
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git status
On branch master
nothing to commit, working tree clean
git查看日志命令—git log
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git reflog
f7f1a3f (HEAD -> master) HEAD@{0}: commit: third commit
72c54eb HEAD@{1}: commit: second commit
f2ab7aa HEAD@{2}: commit (initial): first commit
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git log
commit f7f1a3fd3655a7c86b462a1c5eb23f46183e0784 (HEAD -> master)
Author: mufeng <mufeng@qq.com>
Date: Mon Feb 5 21:38:56 2024 +0800
third commit
commit 72c54ebc35279f094ea262b5dc6d9abe1e6c66db
Author: mufeng <mufeng@qq.com>
Date: Mon Feb 5 21:35:00 2024 +0800
second commit
commit f2ab7aa252760e1d31a970548f43b577d6776fc3
Author: mufeng <mufeng@qq.com>
Date: Mon Feb 5 21:07:57 2024 +0800
first commit
修改文件 hello.txt
修改文件之后再次查看状态
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ vim hello.txt
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/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")
继续提交到暂存区,继续查看状态
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git add .
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
提交到本地库,查看状态
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git commit -m "second commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master 72c54eb] second commit
1 file changed, 2 insertions(+), 2 deletions(-)
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git status
On branch master
nothing to commit, working tree clean
历史版本—git reset --hard <7位版本号>
git reflog 查看版本信息
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git reflog
f7f1a3f (HEAD -> master) HEAD@{0}: commit: third commit
72c54eb HEAD@{1}: commit: second commit
f2ab7aa HEAD@{2}: commit (initial): first commit
git log 查看版本详细信息
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git log
commit f7f1a3fd3655a7c86b462a1c5eb23f46183e0784 (HEAD -> master)
Author: mufeng <mufeng@qq.com>
Date: Mon Feb 5 21:38:56 2024 +0800
third commit
commit 72c54ebc35279f094ea262b5dc6d9abe1e6c66db
Author: mufeng <mufeng@qq.com>
Date: Mon Feb 5 21:35:00 2024 +0800
second commit
commit f2ab7aa252760e1d31a970548f43b577d6776fc3
Author: mufeng <mufeng@qq.com>
Date: Mon Feb 5 21:07:57 2024 +0800
first commit
版本穿梭(回滚)---- git reset --hard <7位版本号>
注意:git进行版本切换,底层其实是移动head的指针
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git reset --hard f7f1a3f
HEAD is now at f7f1a3f third commit
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git reset --hard f2ab7aa
HEAD is now at f2ab7aa first commit
Git的分支操作
什么是分支?
查看分支—git branch -v
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git branch -v
* master f7f1a3f third commit # (*代表当前所在的分区)
创建分支—git branch <分支名>
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git branch hot-fix
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git branch -v
hot-fix f7f1a3f third commit
* master f7f1a3f third commit
切换分支—git checkout <分支名>
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (hot-fix)
$ git branch -v
* hot-fix f7f1a3f third commit # (*代表当前所在的分区)
master f7f1a3f third commit
删除分支—git branch -d <分支名>
不能删除当前分支,只能删除其他分支 git branch -d b1 删除分支时,需要做各种检查 git branch -D b1 不做任何检查,强制删除
把指定的分支合并到当前分支上—git merge <分支名>
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git merge hot-fix
Updating f7f1a3f..e717d8a
Fast-forward
hello.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
代码冲突合并
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/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.
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master|MERGING)
$ git status # 出现MEMRGING,合并有冲突
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
解决措施
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master|MERGING)
$ vim hello.txt #手动修改冲突的代码
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master|MERGING)
$ git add hello.txt #继续提交代码到暂存区
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master|MERGING)
$ git commit -m "merge test" # git commit命令之后不能带文件名
[master 1daf12a] merge test
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
# 后面的MERGING消失,代表合并成功
合并只会修改master分支的内容,hot-fix分支中的文件不变
GIt团队协作机制
GitHub操作
远程仓库操作
创造远程库别名—git remote add <远程库http地址> <别名>
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git remote add git_demo https://gitee.com/mf-study/git_demo.git
查看远程库别名—git remote -v
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git remote -v
git_demo https://gitee.com/mf-study/git_demo.git (fetch)
git_demo https://gitee.com/mf-study/git_demo.git (push)
推送本地分支到远程仓库—git push <别名> <分支名>
如果没有设置别名,需要写远程仓库的http地址
需要填写远程代码托管平台的账户和密码
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git push git_demo master
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 8 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (21/21), 1.55 KiB | 529.00 KiB/s, done.
Total 21 (delta 6), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/mf-study/git_demo.git
* [new branch] master -> master
拉取远程库到本地库—git pull <别名> <分支名>
如果没有设置别名,需要写远程仓库的http地址
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)
$ git pull git_demo 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), 973 bytes | 64.00 KiB/s, done.
From https://gitee.com/mf-study/git_demo
* branch master -> FETCH_HEAD
1daf12a..cb9e217 master -> git_demo/master
Updating 1daf12a..cb9e217
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
克隆远程库到本地库—git clone <远程仓库的http地址>
克隆代码是不需要登录账号的
git clone 会做如下操作:1.拉取代码 2.初始化本地仓库 3.创建别名 别名是origin
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_test
$ git clone https://gitee.com/mf-study/git_demo.git
Cloning into 'git_demo'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 24 (delta 7), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (7/7), done.
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_test
$ ll
total 0
drwxr-xr-x 1 沐枫 197121 0 Feb 6 00:27 git_demo/
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_test
$ cd git_demo/
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_test/git_demo (master)
$ ll
total 1
-rw-r--r-- 1 沐枫 197121 234 Feb 6 00:27 hello.txt
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_test/git_demo (master)
$ cat hello.txt
hello mufeng; hahhaha
hello mufeng; hahahaha
hello mufeng; 12345689
hello mufeng; qwert
hello mufeng; asdfg
hello mufeng; master test
hello mufeng; hot-fix test
hello mufeng;
hello mufeng;
hello mufeng;
你好,我是大神
SSH免密登录
IDEA集成Git
配置Git忽略文件
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
hs_err_pid*
.classpath
.project
.settings
target
.idea
*.iml
初始化本地库
VCS–> Import into Version Control --> Create Git Repository…
Git常见问题解决
问题一:Git Bash命令行中文乱码解决方案
乱码情景一:
使用 git log 时出现乱码,执行以下命令:
git config --global gui.encoding utf-8
git config --global i18n.commitencoding utf-8
git config --global svn.pathnameencoding utf-8
乱码情景二:
使用 git status 时出现乱码,执行以下命令:
git config --global core.quotepath false
问题二:设置本机绑定ssh公钥,实现免密登录(重要)
https://gitee.com/help/articles/4181
进入 C:\Users\liufeng.ssh 目录
liufeng@LAPTOP-2JMKA4G8 MINGW64 ~/.ssh
$ ssh-keygen -t rsa
####### ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/liufeng/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/liufeng/.ssh/id_rsa
Your public key has been saved in /c/Users/liufeng/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:U+eixmTrSq7zy8Zctlzx8A9Au1m0RJlkwzj93MirFIo liufeng@LAPTOP-2JMKA4G8
The key's randomart image is:
+---[RSA 3072]----+
| *=o |
| +.B. |
| ..=.= o |
| .=o+ = .|
| S..@.. . |
| +E+=.= . |
| o.+=o . + |
| .++oo . . |
| .=*o. |
+----[SHA256]-----+
liufeng@LAPTOP-2JMKA4G8 MINGW64 ~/.ssh
文件夹中就会产生对应的两个文件
将id_rsa.pub文件中的信息添加到码云的SSH公钥!!
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDfAqVl6gpdHLsX4nsehVyM4T0k0WKcqxvB/wkDbahdSPQkSSEzOkeTbhgDazpAbusth7KPqqtzu/E5UAQh3dCFdMQAbteWpEJRkxSe1NJNr9cycb6Vnf9kc2YE4XEzhb6fcVuQuQab7RXVmcb6Sb0dZF/QlfzbuqIVLfNvf4OXXNWB0S69zlIwKs3J1B5M1u9hptQePd3mjNQeuG+O91CzsUafugYD/rIoS0Qo91h+F7kdAWucR7Z5lQAasrZdfwPbzZXunaH5thxMl0yAg0CX14NguxvqVT4J2ZaogXSJnW3pZSuTTJ+NwfxL3KrEaAisExmXF8KQMp3/YiNKLfrB7owSV61FOQLBFJRpiIDUHl3OgsNcY5pjSuIhWJffqR/8JIPAM1aoFhoU1ROdoLPjKwiuNb/CPrUQbYNWpBPoknPnrxANgUvhGTpKS0mJJh93vshSuYrLPUQ//wKBWjou3W1NGacA0rTOTrVTcKQ4fqzabQq8DMhUfXLrr3BeiW8= liufeng@LAPTOP-2JMKA4G8
面试考点
理论
- 简述 Git 的原理和工作流程
- 什么是版本控制系统?为什么需要版本控制系统?
- git fetch 和 git pull 命令的区别?
- git rebase 和 git merge 命令的区别?
- 什么是 Git Flow,它有什么好处?
- 什么是暂存区?Git 为什么需要暂存区?
实践
- 分享下你在团队中使用 Git 协作开发的流程(从拉取项目到上线)
- 如何控制某些文件不被提交?
- 什么情况下提交会冲突,如何解决冲突?
- 不小心改错了代码、删除了文件,如何恢复?
- 不小心提交错了文件,如何撤销?
- 团队开发中,如何区分和管理分支?
- 如果让你负责团队,会怎么管理项目的代码?
- 如何防止错误的代码提交?
`