Git最新版---个人总结精简版笔记

开源 0

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.namemufeng沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop$ git config user.mailmufeng@qq.com

初始化本地库—git init

沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-$ git initInitialized 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 -atotal 4drwxr-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 statusOn branch masterNo commits yetnothing 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)$ lltotal 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 statusOn branch masterNo commits yetUntracked files:  (use "git add <file>..." to include in what will be committed)        hello.txtnothing added to commit but untracked files present (use "git add" to track)
再次查看–文件已经提交到暂存区的状态
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git statusOn branch masterNo commits yetChanges 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.txtwarning: 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.txtrm 'hello.txt'

提交本地库–git commit -m “message” <file>

将暂存区的文件提交到本地库
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git commit -m "first commit" hello.txtwarning: 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 statusOn branch masternothing to commit, working tree clean

git查看日志命令—git log

沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git reflogf7f1a3f (HEAD -> master) HEAD@{0}: commit: third commit72c54eb HEAD@{1}: commit: second commitf2ab7aa HEAD@{2}: commit (initial): first commit沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git logcommit f7f1a3fd3655a7c86b462a1c5eb23f46183e0784 (HEAD -> master)Author: mufeng <mufeng@qq.com>Date:   Mon Feb 5 21:38:56 2024 +0800    third commitcommit 72c54ebc35279f094ea262b5dc6d9abe1e6c66dbAuthor: mufeng <mufeng@qq.com>Date:   Mon Feb 5 21:35:00 2024 +0800    second commitcommit f2ab7aa252760e1d31a970548f43b577d6776fc3Author: 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 statusOn branch masterChanges 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.txtno 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 statusOn branch masterChanges 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.txtwarning: 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 statusOn branch masternothing to commit, working tree clean

历史版本—git reset --hard <7位版本号>

git reflog 查看版本信息
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git reflogf7f1a3f (HEAD -> master) HEAD@{0}: commit: third commit72c54eb HEAD@{1}: commit: second commitf2ab7aa HEAD@{2}: commit (initial): first commit
git log 查看版本详细信息
沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git logcommit f7f1a3fd3655a7c86b462a1c5eb23f46183e0784 (HEAD -> master)Author: mufeng <mufeng@qq.com>Date:   Mon Feb 5 21:38:56 2024 +0800    third commitcommit 72c54ebc35279f094ea262b5dc6d9abe1e6c66dbAuthor: mufeng <mufeng@qq.com>Date:   Mon Feb 5 21:35:00 2024 +0800    second commitcommit f2ab7aa252760e1d31a970548f43b577d6776fc3Author: 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 f7f1a3fHEAD is now at f7f1a3f third commit沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_demo (master)$ git reset --hard f2ab7aaHEAD 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-fixSwitched 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-fixUpdating f7f1a3f..e717d8aFast-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-fixAuto-merging hello.txtCONFLICT (content): Merge conflict in hello.txtAutomatic 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 masterYou 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 -vgit_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 masterEnumerating objects: 21, done.Counting objects: 100% (21/21), done.Delta compression using up to 8 threadsCompressing 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 0remote: 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 masterremote: 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 0Unpacking 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/masterUpdating 1daf12a..cb9e217Fast-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.gitCloning 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 0Receiving objects: 100% (24/24), done.Resolving deltas: 100% (7/7), done.沐枫@DESKTOP-PC165B4 MINGW64 ~/Desktop/Java基础/Git/Git-Spaces/git_test$ lltotal 0drwxr-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)$ lltotal 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.txthello mufeng; hahhahahello mufeng; hahahahahello mufeng; 12345689hello mufeng; qwerthello mufeng; asdfghello mufeng; master testhello mufeng; hot-fix testhello 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*.rarhs_err_pid*.classpath.project.settingstarget.idea*.iml

初始化本地库

VCS–> Import into Version Control --> Create Git Repository…

Git常见问题解决

问题一:Git Bash命令行中文乱码解决方案

乱码情景一:

​ 使用 git log 时出现乱码,执行以下命令:

git config --global gui.encoding utf-8git config --global i18n.commitencoding utf-8git config --global svn.pathnameencoding utf-8

乱码情景二:

​ 使用 git status 时出现乱码,执行以下命令:

git config --global core.quotepath false

问题二:设置本机绑定ssh公钥,实现免密登录(重要)

生成/添加SSH公钥 - Gitee.com

https://gitee.com/help/articles/4181

进入 C:/Users/liufeng.ssh 目录

liufeng@LAPTOP-2JMKA4G8 MINGW64 ~/.ssh$ ssh-keygen -t rsa#######	ssh-keygen -t rsaGenerating 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_rsaYour public key has been saved in /c/Users/liufeng/.ssh/id_rsa.pubThe key fingerprint is:SHA256:U+eixmTrSq7zy8Zctlzx8A9Au1m0RJlkwzj93MirFIo liufeng@LAPTOP-2JMKA4G8The 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

面试考点

理论
  1. 简述 Git 的原理和工作流程
  2. 什么是版本控制系统?为什么需要版本控制系统?
  3. git fetch 和 git pull 命令的区别?
  4. git rebase 和 git merge 命令的区别?
  5. 什么是 Git Flow,它有什么好处?
  6. 什么是暂存区?Git 为什么需要暂存区?
实践
  1. 分享下你在团队中使用 Git 协作开发的流程(从拉取项目到上线)
  2. 如何控制某些文件不被提交?
  3. 什么情况下提交会冲突,如何解决冲突?
  4. 不小心改错了代码、删除了文件,如何恢复?
  5. 不小心提交错了文件,如何撤销?
  6. 团队开发中,如何区分和管理分支?
  7. 如果让你负责团队,会怎么管理项目的代码?
  8. 如何防止错误的代码提交?

`

也许您对下面的内容还感兴趣: