本教程主要是Git的安装及使用。
一、Git简介

其他参考资料:
| 【Git工作流和核心原理 | GitHub基本操作 | VS Code里使用Git和关联GitHub-哔哩哔哩】 |
https://b23.tv/ziyw5jd
https://www.runoob.com/git/git-tutorial.html
二、Git实操
1.环境准备
-
操作系统: Linux,Windows,MacOS(本文以Linux为教程)
-
安装Git
1
2
3
sudo apt-get update
sudo apt-get install git
git --version

2.常用指令介绍
-
Linux创建目录&&并进入目录
1
2
mkdir -p hellogit
cd hellogit

-
设置git的用户与邮箱(必须设置!!!)
1
2
git config --global user.name "tungchiahui"
git config --global user.email tungchiahui@gmail.com
-
git初始化(使该文件夹被git控制)
1
git init

-
创建文件&&查看当前文件夹内的所有文件
1
2
touch test.md
ls

-
编辑文件文本内容
1
2
3
sudo apt-get update
sudo apt-get install vim
vim ./test.md

接下来按键盘上的insert按键进入编辑模式&&并输入一段文本

按ESC退出编辑模式

按shift+冒号按键,并输入wq回车进行保存退出

-
查询当前文件的状态
1
git status

On branch 分支名
Untracked是指文件在工作区
-
将文件提交到暂存区
1
git add test.md

-
将文件提交至本地仓库
1
2
git commit -m "version1"
#git commit -m "提交的信息(可以理解为是注释)"

-
查看已经提交的项目版本
1
git log

这几行的意思是tungchiahui(邮箱为tungchiahui@gmail.com)用户于2023年12月30日周六00:42:47往master分支提交了注解为”version1”的项目版本
-
可以尝试对文件内容进行修改,并再次进行提交
1
2
3
4
vim test.md
git add test.md
git commit -m "version2"
git log


-
如何忽略掉不想提交的文件
-
首先创建一个文件.gitignore
-
再创建一个文件 绝密文件不想开源.py 并修改里面的内容
- 查看git状态
1
2
3
4
touch .gitignore
touch vinci_secret.py
vim vinci_secret.py
git status


- 修改.gitignore的内容,将不想被提交的文件名写入该文件
1
2
vim .gitignore
git status


-
分支基本操作
-
分支的作用:开辟项目的一个新仓库分支,与master等其他分支是独立的,可以进行独立的add、commit和diff、clone。
- 创建分支
1
2
git branch route2
#git branch 分支名
- 进入分支
1
2
git checkout route2
#git checkout 分支名
- 查询有多少分支,并查询目前为哪一个分支
1
git branch

当前分支文件会完美继承master分支的文件
- 删除掉该分支下的文件
1
2
3
4
ls
rm -rf test.md
rm -rf vinci_secret.py
ls

- 将该分支往本地仓库进行提交
1
2
git commit -a -m "删库跑路"
#该指令是把git add和git commit指令一起写

- 查看分支是否对其他分支有影响(切换到其他分支查看影响)
1
2
git checkout master
ls

可以发现,在route2分支中被删掉的test.md文件回来了,但是同样被删掉的vinci_sercet.py文件没回来,是因为该文件在提交时被.gitignore文件屏蔽了,不会被提交到本地仓库
- 删除分支
1
2
git branch -D route2
#git branch -D 分支名

- 创建分支并立马切换到分支
1
git checkout -b route3

-
分支的合并
- 在route3分支中修改文件内容并提交
1
2
3
vim test.md
git commit -am "route3 version3"
#-a -m也可以被省略成-am


- 切换到master分支
1
2
git checkout master
vim test.md

- 将route3分支合并到当前分支上
1
2
3
git merge route3
#git merge 被合并的分支名
vim test.md


-
分支合并的冲突
- 为master添加一行内容,并提交
1
2
vim test.md
git commit -am "master version4"


- 切换到route3分支,并添加内容并提交
1
2
3
git checkout route3
vim test.md
git commit -am "route3 version4"


- 合并分支
1
git merge master

发生了内容冲突,需要手动修改文本冲突才可以!或者其他办法也可以解决,可以查询百度。
-
远程仓库Github
-
打开github官网:https://github.com(如果你打不开,请自己找办法打开)
- 注册并登录账号

- 新建远程仓库


这个界面建议详细设置一下,但这里是演示,所以只设置了仓库名称,其他选项为默认操作。

如果上图出现,则说明已经创建成功了
- 进行创建文件并提交


填写好文件名,文件内容,就可以点绿色按钮进行提交了。然后也可以填写提交的注释信息。


- 克隆远程仓库到本地

1
2
3
4
cd
mkdir repo_floder
cd repo_floder
git clone https://github.com/tungchiahui/test_repo.git

如果出错,请检查是否能正常访问github!


- 对文件内容进行修改
1
2
cd test_repo
vim test1.md



- 查看该本地仓库与哪些远程仓库有联系
1
git remote -v

- 将本地仓库push到远程仓库
1
git push

这里需要你输入github的用户名与密码,用户名则为github用户名,但是密码则为github用户token,生成token的步骤如下:




Note随便填,有效日期选择没有截止时间。
下面能勾的勾全勾上,这是权限。

复制token并填入password中(token在终端中输入会被隐藏,输完直接回车即可)

- 查看远程仓库

发现文件已经被修改了!
- 对远程仓库内容进行修改

- 将远程仓库内容拉到本地仓库
1
git fetch

- 查看本地与远程仓库区别
1
2
git diff origin/main
#git diff 远程仓库名/分支名

- 如果发现内容没问题,就可以将远程仓库内容同步到工作区
1
git pull

- 查看项目修改历史
1
git log

三、VScode + Git
-
VScode是什么?
VScode是一个文本编辑器,类似于Vim等编辑器,但是他有图形界面,有非常多的好用的插件,而且可以集成编译器环境,最终也可以被用户自定义配置成Visual Studio那样的IDE。
-
VScode官网:
https://code.visualstudio.com/
-
Linux环境下配置C++环境教程:
-
创建文件夹并在该文件夹下打开VScode

-
新建文件


-
初始化Git(类似于git init命令)


U是指未追踪状态,也就是文件还在工作区
-
将文件提交到暂存区(类似git add)

-
取消提交到暂存区点减号(这里不取消)

A是added表示在暂存区
-
进行提交(类似于commit)

输入提交信息,并点对号进行commit
-
创建新分支,并进入新分支


-
修改内容

显示M为修改状态
-
查看历史版本对比(点击文件)

-
提交

-
切换回main分支


-
合并分支



-
Push到Github远程仓库
- 输入仓库名


- 打开github仓库查看


-
高级工具
-
vcs批量导入仓库
- 安装
1
2
3
4
5
6
7
8
# debian系
sudo apt install python3-vcstool
# rhel系
sudo dnf install python3-vcstool
# pip3安装
pip3 install vcstool
- 文件格式
文件扩展名为.repos或者.yaml,必须满足yaml格式,否则会报错。
如下,
repositories:是总标签
ros_ws是克隆完这个仓库,要把仓库里的文件放在哪一个文件夹的文件夹的名字。
type是仓库管理的类型,一般为git.
url是仓库地址
version是分支名
repositories:
ros_ws:
type: git
url: https://github.com/tungchiahui/ROS_WS.git
version: main
oepncv_projects:
type: git
url: https://github.com/tungchiahui/OpenCV_Projects.git
version: main
以下是一个总示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
repositories:
tungchiahui:
type: git
url: https://github.com/tungchiahui/tungchiahui.git
version: main
ros_ws:
type: git
url: https://github.com/tungchiahui/ROS_WS.git
version: main
oepncv_projects:
type: git
url: https://github.com/tungchiahui/OpenCV_Projects.git
version: main
stm32_projetcts:
type: git
url: https://github.com/tungchiahui/STM32_Projects.git
version: main
mdk6_template:
type: git
url: https://github.com/tungchiahui/CubeMX_MDK5to6_Template.git
version: master
serial_pack:
type: git
url: https://github.com/tungchiahui/Serial_Pack.git
version: main
ros-docker:
type: git
url: https://github.com/tungchiahui/ros-docker.git
version: main
CyberRobotROS:
type: git
url: https://github.com/CyberNaviRobot/CyberRobot_ROS2_Jazzy_WS.git
version: main
CyberRobotMCU:
type: git
url: https://github.com/CyberNaviRobot/STM32_FreeRTOS_MainController.git
version: main
- 如何使用?
把yaml文件放在某个你要存放大量仓库的文件夹下,敲入下方命令
1
vcs import < myrepos.yaml

如下图,成功

-
Github代理
https://ghproxy.link/
-
搭建博客
使用github搭建自己的博客。
https://www.bilibili.com/video/BV1g68TzPEkh
-
常见问题
-
commit到本地的想直接取消
# 设置默认编辑器
git config --global core.editor vim
# 使用交互式rebase
git rebase -i HEAD~2
若我不想提交add bishe了

可以将上图修改为:
drop <hash1> add bishe
pick <hash2> update
然后退出编辑器后就成功了。