image.png

3.1 工作目录、暂存区、版本库概念

①. 版本库(本地仓库):前面看到的.git隐藏文件夹就是版本库,版本库中存储了很多配置信息、日志信息和文件版本信息等

②. 工作目录(工作区):包含.git文件夹的目录就是工作目录,主要用于存放开发的代码

③. 暂存区:.git文件夹中有很多文件,其中有一个index文件就是暂存区,也可以叫做stage。暂存区是一个临时保存修改文件的地方
[

](https://blog.csdn.net/TZ845195485/article/details/116904162)
image.png

3.2 设置用户签名

①. 基本语法
git config —global user.name 用户名
git config —global user.email 邮箱

  1. (1).设置用户信息
  2. git config --global user.name xiaozhi
  3. git config --global user.email 845195485@qq.com
  4. (2).查看不同级别的配置信息
  5. # 查看系统config
  6. git config --system --list  
  7. # 查看当前用户(global)配置
  8. git config --global --list
  9. 通过上面的命令设置的信息会保存在C:\Users\Administrator\.gitconfig文件中

②. 签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码

③. 注意: 这里设置用户签名和将来登录GitHub(其他代码托管中心)的账号没有任何关系

3.3 初始化坏境配置 init

必须先把目录初始化后才可以进行git的操作

①. 在电脑的任意位置创建一个空目录(例如repo1)作为我们的本地Git仓库
②. 进入这个目录中,点击右键打开Git bash窗口
③. 执行命令git init
如果在当前目录中看到.git文件夹(此文件夹为隐藏文件夹)则说明Git仓库创建成功

image.png

3.4 查看文件状态 status

①. git status 查看文件状态
②. 也可以使用git status –s 使输出信息更加简洁
③. Git工作目录下文件的两种状态

  • untracked 未跟踪(未被纳入版本控制)
  • tracked 已跟(被纳入版本控制)
    Unmodified 未修改状态
    Modified 已修改状态
    Staged 已暂存状态
    这些文件的状态会随着我们执行Git的命令发生变化 ```git Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master) $ vim a.txt Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master) $ vim a.txt Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master) $ git status On branch master No commits yet Untracked files: (use “git add …” to include in what will be committed)
    1. a.txt
    nothing added to commit but untracked files present (use “git add” to track) Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master) $ git status -s ?? a.txt
  1. <a name="BOmB5"></a>
  2. ## 3.5 添加到暂存区、撤回到工作目录 add|reset
  3. ①. git add 将未跟踪的文件加入暂存区(将新创建的文件加入暂存区后查看文件状态)<br />②. git add .(.代表所有新增、修改)<br />③. git add -A( -A 新增、修改、删除)推荐使用<br />④. git reset 将暂存区的文件取消暂存(将文件取消暂存后查看文件状态)<br />[
  4. ](https://blog.csdn.net/TZ845195485/article/details/116904162)
  5. ```git
  6. $ git add a.txt
  7. warning: LF will be replaced by CRLF in a.txt.
  8. The file will have its original line endings in your working directory
  9. Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
  10. $ git status
  11. On branch master
  12. No commits yet
  13. Changes to be committed:
  14. (use "git rm --cached <file>..." to unstage)
  15. new file: a.txt
  16. Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
  17. $ git reset a.txt
  18. Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
  19. $ git status
  20. On branch master
  21. No commits yet
  22. Untracked files:
  23. (use "git add <file>..." to include in what will be committed)
  24. a.txt
  25. nothing added to commit but untracked files present (use "git add" to track)
  26. Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)

3.6 删除文件 rm

①. git rm 删除文件(删除的是提交到本地库的文件)
注意:上面删除的只是工作区的文件,需要提交到本地仓库

②. 如下代码删除后,我们需要重新进行commit操作

$ git add b.txt
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git commit -m "b.txt" b.txt
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory
[master 4ae6a0c] b.txt
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git rm b.txt
rm 'b.txt'
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    b.txt


Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    b.txt
# 注意这里需要重新提交操作
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git commit -m "delete b.txt"
[master d39bee4] delete b.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 b.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
nothing to commit, working tree clean

3.7 提交到版本库、形成历史版本 commit

①. git commit -m “日志信息” 文件名(272a9cd这个是版本号)
②. 从工作目录一步提交到版本库:git commit -am ‘new project’(加入缓存区并提交[一步到位])

$ git add -A
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git commit -m "第一次提交 a.txt" a.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
[master (root-commit) 272a9cd] 第一次提交 a.txt
 1 file changed, 9 insertions(+)
 create mode 100644 a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
nothing to commit, working tree clean

3.8 历史版本 reflog log

查看历史版本(git reflog 查看版本信息 | git log 查看版本详细信息)

$ git reflog
272a9cd (HEAD -> master) HEAD@{0}: commit (initial): 第一次提交 a.txt

Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git log
commit 272a9cde6cd70ec607687b6ce161fc46f8b9de2b (HEAD -> master)
Author: o-tangzhi <o-tangzhi@ghac.cn>
Date:   Sun May 16 17:10:35 2021 +0800

    第一次提交 a.txt

3.9 版本穿梭 reset —hard

版本穿梭(git reset —hard 版本号)

Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git add -A
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory

Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git commit -m "第二次提交了" a.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
[master c5d3d8a] 第二次提交了
 1 file changed, 1 insertion(+)
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git log
commit c5d3d8a380a444650db481f6ea2b84a0a0371716 (HEAD -> master)
Author: o-tangzhi <o-tangzhi@ghac.cn>
Date:   Sun May 16 17:16:19 2021 +0800
    第二次提交了
commit 272a9cde6cd70ec607687b6ce161fc46f8b9de2b
Author: o-tangzhi <o-tangzhi@ghac.cn>
Date:   Sun May 16 17:10:35 2021 +0800
    第一次提交 a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git reflog
c5d3d8a (HEAD -> master) HEAD@{0}: commit: 第二次提交了
272a9cd HEAD@{1}: commit (initial): 第一次提交 a.txt

Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git reset --hard 272a9cd  # 注意这里切换到了第一次版本提交
HEAD is now at 272a9cd 第一次提交 a.txt

Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)

$ git reflog
272a9cd (HEAD -> master) HEAD@{0}: reset: moving to 272a9cd
c5d3d8a HEAD@{1}: commit: 第二次提交了
272a9cd (HEAD -> master) HEAD@{2}: commit (initial): 第一次提交 a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
# 注意这里切换到第二次
$ git reset --hard c5d3d8a
HEAD is now at c5d3d8a 第二次提交了
$ cat a.txt
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
第二次版本迭代

[

](https://blog.csdn.net/TZ845195485/article/details/116904162)