Git常用命令
| 设置用户签名 |
| 设置用户签名 |
| 初始化本地库 |
| 查看本地库状态 |
| 添加到暂存区 |
| 提交到本地库 |
| 查看历史纪录 |
| 版本穿梭 |
设置用户签名
Git首次安装必须设置用户签名,否则无法提交代码
桌面右键git Bash Here
git config --global user.name Winnie
git config --global user.email Winnie@atguigu.com
如何验证用户签名设置好?查看配置文件.gitconfig
在C:\Users\Administrator.DESKTOP-RFJDIJ4目录下,Administrator.DESKTOP-RFJDIJ4根据自己的用户替换,git Bash Here开头的用户即当前使用的用户,如图:
![](https://img-blog.csdnimg.cn/img_convert/a630b8fe514275f2e03ea47e2ac169d8.png)
.gitconfig文件用记事本打开就能看到配置的用户签名:
[user]
name=Winnie
email=Winnie@atguigu.com
签名的作用:区分不同操作者的身份,这里设置的用户签名和将来登录GitHub或其他代码托管中心的账号没有任何关系,只是代表本地的Git客户端
初始化本地库
git init
例如:以F:\Git-Space\git-demo为项目目录,快捷方式使客户端进入该目录:点进该目录,在目录下右键打开Git Bash Here
![](https://img-blog.csdnimg.cn/img_convert/1fda08b4e0d34804b2599b4f6f9082fd.png)
初始化本地库git init,回车,生成了.git目录
![](https://img-blog.csdnimg.cn/img_convert/ae3809a9b03a7631f7015e5311d9f5d2.png)
若看不到,点击查看->显示->勾选隐藏的项目
![](https://img-blog.csdnimg.cn/img_convert/5aa26a5a9ed982f8353748b826504216.png)
在客户端也可以查看:ll -a查看隐藏目录
ll -a
![](https://img-blog.csdnimg.cn/img_convert/c854baed4be587d9a52316a7e7749829.png)
查看本地库状态
首次查看
git status
![](https://img-blog.csdnimg.cn/img_convert/18bbdb066f452e292a4f04f2aa12cd74.png)
新增文件
vim hello.txt
点击i进入插入模式最后一行显示-- INSERT --,(Esc进入命令模式),在txt中写入
![](https://img-blog.csdnimg.cn/img_convert/1a8d539deaefe2e62b263f39e975b603.png)
写完之后点击Esc退出插入模式
将这行复制几行:
在vim中yy是复制,p是粘贴,把光标放在第一行,点击yy复制,点击p复制
写入:wq保存并退出
查看目录
ll
![](https://img-blog.csdnimg.cn/img_convert/d1fe312f882451fe2c3a091424290224.png)
查看文件内容
cat hello.txt
![](https://img-blog.csdnimg.cn/img_convert/e25ec6e9703081ab108b99530b0daa73.png)
查看文件末尾第一行
tail -n -1 hello.txt
在git Bash 客户端中Linux命令通用
再次查看本地库的状态
git status
![](https://img-blog.csdnimg.cn/img_convert/89f278e8af82c90c8e4324ba3096eb81.png)
添加暂存区
![](https://img-blog.csdnimg.cn/img_convert/9e62aa8014ab953ad5c474f78e55bd18.png)
将工作区文件添加到暂存区
git add hello.txt
![](https://img-blog.csdnimg.cn/img_convert/b20a57026a5494f1db35ca941ac179e4.png)
再次查看本地库状态git status
![](https://img-blog.csdnimg.cn/img_convert/27b0e3c4934b4d6ff3f79ee1137a5cee.png)
删去暂存区文件:
git rm --cached hello.txt
只是把暂存区里面的文件删掉,而工作区的不会删掉
验证:ll
![](https://img-blog.csdnimg.cn/img_convert/0dc5ef5c5ed9042db0377ff448b29127.png)
git-demo文件夹是工作区
再次查看本地库状态git status:
![](https://img-blog.csdnimg.cn/img_convert/31848a35665dca4e774f3c09021a4325.png)
又显示还未追踪过hello.txt文件,说明hello.txt文件已经从暂存区里面删掉了。
先把文件添加到暂存区:
git add hello.txt
再次查看本地库状态git status,已更新:
![](https://img-blog.csdnimg.cn/img_convert/ec511a3cd0425b82b112e7d59ebbd0a9.png)
提交本地库
![](https://img-blog.csdnimg.cn/img_convert/d481961fbd44c14b93da04649124a2bf.png)
git commit -m "first commit" hello.txt
![](https://img-blog.csdnimg.cn/img_convert/1fb30f31df729682572f52ed97225086.png)
再次查看本地库状态git status
No commits yet 消失
![](https://img-blog.csdnimg.cn/img_convert/fa93c9aeed2e3e27fe9aba5e22d993f9.png)
查看版本信息
查看引用日志信息
git reflog
![](https://img-blog.csdnimg.cn/img_convert/ab9ebc9fbcfb412e6981c19ba3979030.png)
查看详细日志
git log
![](https://img-blog.csdnimg.cn/img_convert/99d763b63d520336be2c53d1779ab21b.png)
修改文件
先查看文件
cat hello.txt
vim hello.txt
点击i进入插入状态在第一行后面加2222222222
点击Esc退出插入状态,:wq退出并保存
查看本地库状态git status
![](https://img-blog.csdnimg.cn/img_convert/2542666b3210928e4e1a09b8502c2bb2.png)
添加到暂存区git add hello.txt
再次查看本地库状态git status
![](https://img-blog.csdnimg.cn/img_convert/a6d567d9f950eaf2b9399bd404c5660a.png)
提交本地库
git commit -m "second commit" hello.txt
![](https://img-blog.csdnimg.cn/img_convert/8af636afc96890a06401490fad51eb0d.png)
再次查看本地库状态git status
![](https://img-blog.csdnimg.cn/img_convert/1452c2648545be65485ac92f3cb0cabd.png)
查看版本信息:
此时有两个版本了first commit ,second commit,指针指向second commit,所以cat hello.txt就是第二个版本
![](https://img-blog.csdnimg.cn/img_convert/628b283818253be6175f86d8968e852d.png)
![](https://img-blog.csdnimg.cn/img_convert/e466d9ea6a31f060991527a1f0709a2d.png)
再次提交一个版本:
![](https://img-blog.csdnimg.cn/img_convert/6275a50b018fc7bca76f1bcead4973a8.png)
历史版本
查看版本信息
git reflog
![](https://img-blog.csdnimg.cn/img_convert/8bad38dcd9d60fc6f349fae8cc2205b7.png)
查看详细版本信息日志(详细版本号,版本,日期,作者)
git log
版本穿梭
把代码回到2号版本
选择2号版本的版本号右键copy(不能ctrl+c)粘贴用右键Paste)
git reset --hard df4b024
![](https://img-blog.csdnimg.cn/img_convert/36d390eb00dfff63e9d2ff5cb77c0c1f.png)
再次查看git reflog
![](https://img-blog.csdnimg.cn/img_convert/ea73ad035808217db378c645f53946b7.png)
查看文件内容cat hello.txt已经变成第二版本
![](https://img-blog.csdnimg.cn/img_convert/86de148d8038a78ac95077a4ceae6cf0.png)
把代码回到最初的版本:
![](https://img-blog.csdnimg.cn/img_convert/82bb8709ee8267d9e0da281c78bc1b01.png)
此时F:\Git-Space\git-demo目录中的hello.txt已经回到最初版本
F:\Git-Space\git-demo\.git\refs\heads里面的master里面记得是当前版本版本号