目录
一、基本配置
-
桌面上右键Click 选择Git Bash打开命令终端
-
设置用户信息名称和邮箱(设置用户信息,以便知道谁对 github上的项目文件进行了commit 操作)
git config --global user.name 'wangpeng'
git config --global user.email 'webberwang@foxmail.com'
- 查看配置的所有信息
git config --list
![](https://img-blog.csdnimg.cn/img_convert/b67e222e7b97ea5ad3f8c75e83fa7f98.png)
- 查看刚刚配置的用户信息
git config --global user.name
git config --global user.email
![](https://img-blog.csdnimg.cn/img_convert/6e60dcd923734751c0b7f67e26037ffd.png)
二、git完整流程
![](https://img-blog.csdnimg.cn/5c34dc0a6702496f81728a8b07b2635f.png)
三、创建本地仓库
- 创建空目录local_rep,使用指令
mkdir local_rep
- 切换目录到local_rep,
cd local_rep
- 执行命令
git init
来初始化本地仓库 - 此时进入master仓库,执行
ls -al
命令可以看到.git
文件
![](https://img-blog.csdnimg.cn/img_convert/5a83891be91f7c52673c3fb571d9a97a.png)
![](https://img-blog.csdnimg.cn/img_convert/a021fce77b5eaedefa1615413ae4c610.png)
四、基础操作指令
4.1 git的add、commit 以及log的使用
- 新建文件demo.txt 查看git状态 其为未跟踪文件(untracked file)
![](https://img-blog.csdnimg.cn/img_convert/4a7cd2dc17dc3cae58be49cc42bdac34.png)
- 使用
git add demo.txt
将文件添加到暂存区
![](https://img-blog.csdnimg.cn/img_convert/147466e6429e95d1be1cf6ca34bdc6f2.png)
- 使用
git commit -m 'create a file named as demo.txt'
将文件提交到本地仓库
![](https://img-blog.csdnimg.cn/img_convert/7a5f694ede4965e823dad42a39bd14f9.png)
- 使用
git log
输出提交日志
![](https://img-blog.csdnimg.cn/img_convert/f6fc85b7b69cbc9a0c55d99cc11a9a23.png)
- 修改文件demo.txt 指令为
echo "版本一" > demo.txt
![](https://img-blog.csdnimg.cn/img_convert/d50d9ef4809626fa597d7e2eeab53947.png)
- 继续查看git status 发现目前文件处于 not staged 状态 重新执行 add 以及 commit 操作提交到本地仓库 执行 git log 查看有两条提交日志
![](https://img-blog.csdnimg.cn/img_convert/76585db89b8c032c8614df26c0b99b09.png)
-
git log 的选项描述
git log --pretty=oneline
将提交信息表示为一行
![](https://img-blog.csdnimg.cn/img_convert/83d967b66080f5cc5b971a37edcf47de.png)
git log --pretty=oneline --abbrev-commit
使得commitid更简短
![](https://img-blog.csdnimg.cn/img_convert/9224780168b5064f4fac177f514e6a6c.png)
![](https://img-blog.csdnimg.cn/img_convert/c75f5a10e248fed56b2dc4d89dc9fbd0.png)
- 当前修改版本回退到创建版本,指令为
git reset --hard commit_id
![](https://img-blog.csdnimg.cn/img_convert/26852c516a241803ef15c7b3983eb4dc.png)
![](https://img-blog.csdnimg.cn/img_convert/75f3f05bcd8d62b672b0e944fd4ad514.png)
- 再从创建版本恢复到修改版本
![](https://img-blog.csdnimg.cn/img_convert/d1168b46771aaf0c3ac573f37ba92708.png)
我们可以通过给长命令取别名的方式来简化输入命令难度。
- 在~目录下创建 .bashrc文件
![](https://img-blog.csdnimg.cn/img_convert/9b7f6d72fb0a81bd15c55fbfdb0de649.png)
- 通过vim打开 ./bashrc 文件 输入
alias gitlog='git log --pretty=oneline --abbrev-commit --all --graph'
![](https://img-blog.csdnimg.cn/e0e4b1521be845dcae21a7f7b18e31d2.png)
- 执行
source ~/.bashrc
命令
4.4 忽略文件 .gitignore
- 创建文件.gitignore 指令为
touch .gitignore
![](https://img-blog.csdnimg.cn/img_convert/13fbef2b29d81476bd08f18c769ea5ba.png)
![](https://img-blog.csdnimg.cn/img_convert/36ebf86cbfc7ab8ef6fc4d4c4477e92d.png)
五、分支
5.1 创建分支
命令:git branch dev
5.2 切换到dev分支
命令:git checkout dev
![](https://img-blog.csdnimg.cn/img_convert/d277fa1ada56f4ea27b2d19231d6157d.png)
命令:git checkout -b test
![](https://img-blog.csdnimg.cn/img_convert/4d5169b407e695123ba6795fc3796c10.png)
5.4 合并分支(merge)
- 在 test 分支上提交 test.txt 文件
![](https://img-blog.csdnimg.cn/img_convert/51bab287b908e577619f178f2a0f5572.png)
- add、commit 文件
![](https://img-blog.csdnimg.cn/img_convert/37f875f3c3e136918ab1825daf297b8b.png)
- 在dev分支上进行相同的操作。
![](https://img-blog.csdnimg.cn/img_convert/822ea34e15feef9625d73b487724ec33.png)
![](https://img-blog.csdnimg.cn/img_convert/7668636220d240430aadf5be95af3c43.png)
- gitlog一下
![](https://img-blog.csdnimg.cn/img_convert/3b3ae076375902559295021a36840a79.png)
- 在test分支上执行 merge操作
git merge dev
![](https://img-blog.csdnimg.cn/img_convert/ed66263fed843c8be318cdd7b40c0700.png)
- 删除分支
git branch -D 分支名称
![](https://img-blog.csdnimg.cn/img_convert/b07d9ea4a7eff3b1e6aff7cbb8a517c9.png)
5.5 解决冲突
当两个分支上对文件的修改可能会存在冲突,例如同时修改了同一个文件的同一行,这时就需要手动解决冲突。解决冲突步骤如下:
- 处理文件中冲突的地方
- 将解决完冲突的文件加入到暂存区(add)
- 提交到仓库(commit)
![](https://img-blog.csdnimg.cn/img_convert/c8996a7a0744b0b1208df3e2e6b046aa.png)
六、本地仓库 > 远程仓库
6.1 gitee上新建远程仓库
![](https://img-blog.csdnimg.cn/img_convert/603f9d008253cfef7eb770defb1f4f93.png)
![](https://img-blog.csdnimg.cn/img_convert/b403c00565b5be361e4c6175fbd68e68.png)
6.2 配置SSH公钥
- 输入
ssh-keygen -t ed25519 -C "Gitee SSH Key"
来生成SSH公钥-t
为SSH 密钥类型(算法采用ed25519 )-C为注释
![](https://img-blog.csdnimg.cn/img_convert/deb4797e91ab99e4646f44c3abd7c36b.png)
-
Gitee添加生成好的公钥
- 输入
cat gitee_ssh.pub
查看内容
- 输入
![](https://img-blog.csdnimg.cn/img_convert/e78a4917cc6fc1fe6565be7b2c50e031.png)
- 复制该内容粘贴至gitee中
![](https://img-blog.csdnimg.cn/img_convert/a391c786ab46bdc1d5ca1459b735faf0.png)
- 验证是否配置成功 输入
ssh -T git@gitee.com
![](https://img-blog.csdnimg.cn/img_convert/65e8ff99927867eec3595db6e1bf1c6e.png)
6.3 本地仓库与远程仓库建立连接
指令:
git remote add <远程名称> <仓库路径>
远程名称默认为origin
- git remote add origin https://gitee.com/peng-wang98/git_test.git
![](https://img-blog.csdnimg.cn/img_convert/4a6a63758c4d8db52db4e45520d3d2f1.png)
6.4 查看远程仓库
指令:
git remote
![](https://img-blog.csdnimg.cn/img_convert/3e50bf16ea56a20e0e4648d297a5e205.png)
6.5 推送到远程仓库
指令:git push [-f] [--set-upstream] [远端名称] [本地分支名] [:远端分支名]
- 如果远程分支名和本地分支名名称相同(都为master),则可以只写本地分支名 命令为
git push origin master
-f
表示强制覆盖--set-upstream
建立分支联系,如果当前分支已经和远端分支建立了联系,则可以忽略分支名和远端名。直接 git push
![](https://img-blog.csdnimg.cn/img_convert/f82b602aa60ee9066300867032df9ac8.png)
![](https://img-blog.csdnimg.cn/img_convert/4dc1f16d9bc982429580639404dbf2cf.png)
七、远程仓库>本地仓库
7.1 克隆仓库
我们可以将远程仓库clone到本地。指令:
git clone <仓库路径> <本地目录[可省略]>
![](https://img-blog.csdnimg.cn/img_convert/f4839fa4adabe6e1ecb9223fc8d9a5aa.png)
7.2 抓取和拉取
- 抓取命令
git fetch [remote name] [branch name]
不会合并
![](https://img-blog.csdnimg.cn/img_convert/dc7745151f7db43479891bba90722bca.png)
- 拉取命令
git pull [remote name] [branch name]
自动合并