签名的作用是区分不同操作者身份。
用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。
Git 首次安装必须设置一下用户签名,否则无法提交代码。
全局范围的签名设置:
# git config --global user.name 用户名
35192@babyboy MINGW64 /e/35192/Desktop
$ git config --global user.name zyx
# git config --global user.email 邮箱
35192@babyboy MINGW64 /e/35192/Desktop
$ git config --global user.email zyx@1229.com
查看设置
35192@babyboy MINGW64 /e/35192/Desktop
$ cat ~/.gitconfig
[user]
name = zyx
email = zyx@1229.com
[credential]
helper = store
[http]
sslVerify = false
postBuffer = 1048576000
本地配置文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HL31YCfr-1658821863971)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725174406060.png)]](https://1000bd.com/contentImg/2022/07/29/234841710.png)
※ 注意: 这里设置用户签名和将来登录 GitHub (或其他代码托管中心)的账号有关系(需要保持一致)。
要使用Git对我们的代码进行版本控制,首先需要获得本地仓库
1)在电脑的任意位置创建一个空目录(例如git-demo)作为我们的本地Git仓库
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-khXidAuJ-1658821863972)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725175557523.png)]](https://1000bd.com/contentImg/2022/07/29/234841986.png)
2)进入这个目录中,点击右键打开Git bash窗口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HT4mzdwQ-1658821863972)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725175613764.png)]](https://1000bd.com/contentImg/2022/07/29/234842126.png)
3)执行命令git init
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4JiU6d1i-1658821863972)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725175710122.png)]](https://1000bd.com/contentImg/2022/07/29/234842393.png)
4)如果创建成功后可在文件夹下看到隐藏的.git目录。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rKkg4ZTE-1658821996889)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725175731350.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234842604.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Sa1HNVeF-1658822018980)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725175903302.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234842809.png)
作用:查看的修改的状态(暂存区、工作区)
命令形式:git status
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y5mLl6BW-1658821863974)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725180108748.png)]](https://1000bd.com/contentImg/2022/07/29/234842997.png)
新增文件 hello.txt:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fERMW1g3-1658821863975)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725180338152.png)]](https://1000bd.com/contentImg/2022/07/29/234843615.png)
再次查看本地库状态:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CZSABj8n-1658821863975)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725180418951.png)]](https://1000bd.com/contentImg/2022/07/29/234843775.png)
此时就会检测到未追踪的新文件,需要将其进行添加操作。
作用:添加工作区一个或多个文件的修改到暂存区
命令形式:git add 单个文件名|通配符
git add![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jfi8b17l-1658822081938)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725185433822.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234843969.png)
※ 注意: 这里有个警告,就是将windows的换行符转换为Linux中的换行符,无需在意!
此时再次查看本地库状态:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mj7vyiUe-1658821863978)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725185302508.png)]](https://1000bd.com/contentImg/2022/07/29/234844245.png)
可以看到原来的红色文件现在变成了绿色,并且信息提示,更改有待被提交。
并且如果想要移除文件,可以使用git -rm cached 文件名命令,注意这里的删除是指从暂存区进行移除,本地库中的文件依然存在。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f8BxtHIA-1658821863979)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725190121520.png)]](https://1000bd.com/contentImg/2022/07/29/234844544.png)
作用:提交暂存区内容到本地仓库的当前分支
命令形式:git commit -m '注释内容' 文件名
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BOzQHmwn-1658821863980)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725194808338.png)]](https://1000bd.com/contentImg/2022/07/29/234844703.png)
其中 b0c4ceb 就是本次提交的版本号。
再次查看本地仓库状态:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pfPCoOuQ-1658822135773)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725200257785.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234844888.png)
可以看到,当前工作树是干净的。也就是提交后,当前没有任何的文件修改操作。
git reflog
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-urJtnRLO-1658821863981)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725200236137.png)]](https://1000bd.com/contentImg/2022/07/29/234845044.png)
可以看到开头代表我们的版本号,并且当前指针执向master分支,已经提交的信息是first commit。
git log
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sFhvymtM-1658822163793)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725200204479.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234845200.png)
这里我们可以看到详细的日志信息,这里的一长串是真正的版本号,不仅仅可以看到提交的版本信息,同时还能看到提交的用户信息。
这里我们对hello.txt文件进行内容的修改:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gaLQDy3A-1658821863982)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725202717789.png)]](https://1000bd.com/contentImg/2022/07/29/234845372.png)
然后再次查看本地库的状态:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jvi5DAKO-1658821863983)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725203054989.png)]](https://1000bd.com/contentImg/2022/07/29/234845560.png)
可以看到,git检测出我们的本地库文件进行了修改,需要继续追踪添加到暂存区
当我们添加完成后,原本红色的提示变成了绿色:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d11aZDWB-1658822201405)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725203210126.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234845763.png)
此时我们再次进行提交,版本信息:second commit:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TCqeaIOU-1658821863984)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725203704339.png)]](https://1000bd.com/contentImg/2022/07/29/234846028.png)
可以看到,此次提交显示我们新添加了一条数据(insertion)
当我们查看日志信息的时候,就会显示两个版本了:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TaDVq3aZ-1658822232442)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725204009123.png#pic_center)]](https://1000bd.com/contentImg/2022/07/29/234846240.png)
前面提过我们可以查看提交的版本信息:
有了历史的版本记录,我们可以在不同版本之间进行穿梭(更换),使用 git reset --hard 版本号进行版本穿梭。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yQWlToB3-1658821863985)(https://test1.jsdelivr.net/gh/Code-for-dream/Blogimages/img/Git/image-20220725205845368.png)]](https://1000bd.com/contentImg/2022/07/29/234846506.png)
然后打开我们的本地文件,直接变回了版本1:

当我们再次切换到版本2的时候,文件内容随之改变:

