背景
这两天学习了git相关操作,记录下常见的操作命令。
基本理论
git本地有三个工作区:工作目录(Working Directory)、暂存区(stage\index)、资源区(Repository)或Git Directory
操作流程
![](https://file.jishuzhan.net/article/1709728463008567298/17f56a58ef4ce8785f4eb33bc8d926e2.webp)
![](https://file.jishuzhan.net/article/1709728463008567298/7dbb37c537b9859441890d4f21286684.webp)
目录结构
版本库:/.git隐藏文件夹
工作区:/.git所在的目录
暂存区:/.git/index 保存临时修改文件的地方
用户配置文件 :gitconfig
文件地址:.gitconfig
![](https://file.jishuzhan.net/article/1709728463008567298/45b1568637e5346fff90b6510dc65c8a.webp)
常见命令
通用指令
查看配置:查看配置git config -l
![](https://file.jishuzhan.net/article/1709728463008567298/5cd3659346c2d5b6c68cd7a277aefe98.webp)
查看当前用户配置git config --global --list
![](https://file.jishuzhan.net/article/1709728463008567298/7e7cf14d26dca0025ff492b34b8403da.webp)
修改用户配置
![](https://file.jishuzhan.net/article/1709728463008567298/2da3cb7b09c941e9040c75d98b98c1e9.webp)
查看文件状态:git status [filename]
![](https://file.jishuzhan.net/article/1709728463008567298/024ad9ebea4cf0199922770625884890.webp)
查看操作日志: git log
提示的文字在commit时书写
![](https://file.jishuzhan.net/article/1709728463008567298/c053727f92315a3e08473c14f5469714.webp)
切换指定版本: git reset
git reset--hard 995e0688f2554e1e557d86f82de3e5c92db817c8 ,可以切换到指定版本的文件
![](https://file.jishuzhan.net/article/1709728463008567298/46da3ca20dcf9701bb10a135bd3f3c6d.webp)
分类
本地仓库操作
设置本地git仓库:git init
![](https://file.jishuzhan.net/article/1709728463008567298/eae89707bc84bd84512aec464849e6b6.webp)
添加到暂存区: git add
可以看到add后文件的颜色由红变绿
![](https://file.jishuzhan.net/article/1709728463008567298/6124e297c72356e57a6d3ff6c2e174eb.webp)
取消暂存: git reset
add的逆操作,取消存入暂存区
![](https://file.jishuzhan.net/article/1709728463008567298/56af11d3aac4783fb04c417075387cd9.webp)
提交:git commit
-m 以及后面的文字为此次更新的注释说明
本地流程总结
新建文件(红色)->add 文件(绿色)-> commit 文件(白色)
![](https://file.jishuzhan.net/article/1709728463008567298/c75c38cfce320d40f576125233df9036.webp)
远程仓库操作
设置远程仓库:git clone
设置远程git仓库: git clone git@gitee.com:gitee-enterprise_2_0/hellogit.git
![](https://file.jishuzhan.net/article/1709728463008567298/e1915eda25b3be047837c8874f70cc6d.webp)
查看远程仓库:git remote -v
![](https://file.jishuzhan.net/article/1709728463008567298/1dda991b2c5ccb71b3539cb7c08d7d6f.webp)
添加远程仓库:git remote add
可观察添加前后远程仓库数量(0->2)
![](https://file.jishuzhan.net/article/1709728463008567298/7224df2ea446f24fd224bd3ae7db3a43.webp)
推送到远程仓库:git pull origin master
推送到远程流程:新建文件->添加到缓存区(add)->提交(commit,可写备注)->推送远程仓库(pull)
![](https://file.jishuzhan.net/article/1709728463008567298/c099caf8f8532954f40777efc1a80359.webp)
合并冲突:pull后加上参数 --allow-unrelated-histories
![](https://file.jishuzhan.net/article/1709728463008567298/5f34fa74a7e3efdc154be0f0766129b0.webp)
分支相关操作
查看分支: git branch
参数说明:
不写,本地分支
-r远程分支
-a 所有分支(远程+本地)
![](https://file.jishuzhan.net/article/1709728463008567298/363f914e3f465873845a1bdc809636b3.webp)
创建分支: git branch [name]
![](https://file.jishuzhan.net/article/1709728463008567298/52d0c7464cf0ae145c26c73cf1332b5c.webp)
切换分支:git checkout [name]
![](https://file.jishuzhan.net/article/1709728463008567298/5b53e10104312f110deb731774efc528.webp)
推送分支至远程仓库:git push [shortName] [name]
注意与上文的区别:git pull origin master
推送谁,最后一个参数就写谁,上文默认是主线master,这是分支b1
![](https://file.jishuzhan.net/article/1709728463008567298/644b792fa593c683411029fc691b53be.webp)
合并分支: git merge [name]
![](https://file.jishuzhan.net/article/1709728463008567298/47fb0e1d6a1034104dc0b41bb8f35737.webp)
合并前切换到主线,在主线master合并分支b1
![](https://file.jishuzhan.net/article/1709728463008567298/fa6a131645e91cae1eec8495c435a939.webp)
分支合并冲突处理 : commit后加 -i
注意与上文区别:--allow-unrelated-histories
上文是在一条分支内处理,横向的
这是处理两条分支冲突,纵向的
![](https://file.jishuzhan.net/article/1709728463008567298/47fb0e1d6a1034104dc0b41bb8f35737.webp)
标签相关操作
显示标签:git tag
创建标签: git tag [name]
![](https://file.jishuzhan.net/article/1709728463008567298/4fc085f02f872581992759091c42a79b.webp)
将标签推送至远程仓库:git push [shortName][name]
![](https://file.jishuzhan.net/article/1709728463008567298/b12ef46011ce1712ffeb6443085da383.webp)
检出标签:git checkout -b [branch][name]
标签相关操作与分支基本相同
分支可看作日常协调工作,时不时会改
标签是较为稳定的一个版本,类似 v1.0,v2.0 作为一个发布版本
![](https://file.jishuzhan.net/article/1709728463008567298/8b48cc0debad8ebcf3ab591e5fdc57a2.webp)
补充:基本的Linux的命令
pwd:显示当前所在的目录路径
ls(ll):都是列出当前目录的所有文件,之不多ll列出的内容更加详细
touch:创建一个文件,如touch index.js 就会在当前文件下创建一个文件
rm:删除一个文件
mkdir:新建一个目录,就是新建一个文件夹
rm-r:删除一个文件夹,rm -r src删除src目录
mv:移动文件 mv index.html src,index为要移动的文件,src为目标文件夹
reset:重新初始化屏幕
clear:清屏
history:查看历史命令
help:帮助
#表示注释