一、认识Git
1.1 Git 工作机制
二、安装
第四章 Git常用命令
4.1 命令
| 名称 | 作用 |
|---|---|
| git init | 初始化本地库 |
| git config --global user.name 用户名 | 设置用户签名 |
| git config --global user.email 邮箱 | 设置用户签名 |
| git status | 查看本地库状态 |
| git add 文件名 | 添加到暂存区 |
| git commit -m "日志信息" 文件名 | 提交到本地库 |
| git reflog | 查看历史记录 |
| git reset --hard 版本号 | 版本穿梭 |
4.2 初始化本地库
1)基本语法
git init
2)案例实操
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720
$ git init
Initialized empty Git repository in D:/MyWork/Git-Space/SH0720/.git/
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ ll -a
total 4
drwxr-xr-x 1 ZhangJY 197609 0 11月 25 14:07 ./
drwxr-xr-x 1 ZhangJY 197609 0 11月 25 14:07 ../
drwxr-xr-x 1 ZhangJY 197609 0 11月 25 14:07 .git/ (.git 初始化的效果,生成git)
4.3 设置用户签名
1)基本语法
git config --global user.name 用户名
git config --global user.email 邮箱
2)案例实操
全局范围的签名设置:
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git config --global user.name ZhangJY
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git config --global user.email ZhangJY@atguigu.com
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ cat ~/.gitconfig
[user]
name = ZhangJY
email = ZhangJY@atguigu.com
说明:
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。
※注意:这里设置用户签名和将来登录GitHub(或其他代码托管中心)的账号没有任何关系。
4.3 查看本地库状态
1)基本语法
git status
2)案例实操
4.3.1 工作区没有任何文件
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
4.3.2 检测到未追踪的文件
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
4.3.3 检测到有新建的文件添加到了暂存区
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
4.3.4 提交完成后查看状态
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git status
On branch master
nothing to commit, working tree clean
4.3.5 检测到工作区有文件被修改
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
4.3.6 工作区的修改添加到了暂存区
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
4.3.7 产生冲突(在后面讲分支操作后演示)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
4.4 将工作区的修改添加到暂存区
1)基本语法
git add 文件名
2)案例实操
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
4.5 将暂存区的修改提交到本地库
1)基本语法
git commit -m "日志信息" 文件名
2)案例实操
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git commit -m "my first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
[master (root-commit) 86366fa] my first commit
1 file changed, 16 insertions(+)
create mode 100644 hello.txt
4.6 历史版本
1)基本语法
git reflog
2)案例实操
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
4.6.2 版本穿梭
1)基本语法
git reset --hard 版本号
2)案例实操
--首先查看当前的历史记录,可以看到当前是在087a1a7这个版本
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
--切换到86366fa版本,也就是我们第一次提交的版本
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git reset --hard 86366fa
HEAD is now at 86366fa my first commit
--切换完毕之后再查看历史记录,当前成功切换到了86366fa版本
ZhangJY@MSI MINGW64 /d/MyWork/Git-Space/SH0720 (master)
$ git reflog
86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa
087a1a7 HEAD@{1}: commit: my third commit
ca8ded6 HEAD@{2}: commit: my second commit
86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit