学海无涯回头是岸
3、Git 常用命令
命令名称 | 作用 |
---|---|
git config --global user.name | 用户名 设置用户签名 |
git config --global user.email | 邮箱 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m “日志信息” 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
3.1 设置用户签名
- 基本语法
git config --global user.name 用户名
git config --global user.email 邮箱 - 案例实操
全局范围的签名设置:
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git config --global user.name tianyu
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git config --global user.email 854116434@qq.com
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ cat ~/.gitconfig
[user]
name = tianyu
email = 854116434@qq.com
说明:
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码。
※注意:这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任何关系。
3.2 初始化本地库
- 基本语法
git init - 案例实操
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo
$ git init
Initialized empty Git repository in D:/ProgramData/Git-Space/git-demo/.git/
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ ll -a
total 4
drwxr-xr-x 1 85411 197609 0 Sep 30 10:00 ./
drwxr-xr-x 1 85411 197609 0 Sep 30 09:57 ../
drwxr-xr-x 1 85411 197609 0 Sep 30 10:00 .git/(.git 初始化的效果,生成 git)
- 结果查看
3.3 查看本地库状态
- 基本语法
git status - 案例实操
3.3.1 首次查看(工作区没有任何文件)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
3.3.2 新增文件(hello.txt)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ vim hello.txt
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
3.3.3 再次查看(检测到未追踪的文件)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (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)
3.4 添加暂存区
3.4.1 将工作区的文件添加到暂存区
- 基本语法
git add 文件名 - 案例实操
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
3.4.2 查看状态(检测到暂存区有新文件)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
3.5 提交本地库
3.5.1 将暂存区的文件提交到本地库
- 基本语法
git commit -m “日志信息” 文件名 - 案例实操
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git commit -m "first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 466668c] first commit
1 file changed, 16 insertions(+)
create mode 100644 hello.txt
3.5.2 查看状态(没有文件需要提交)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
3.6 修改文件(hello.txt)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ vim hello.txt
hello tianyu! hello git! 2222222222
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
3.6.1 查看状态(检测到工作区有文件被修改)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
3.6.2 将修改的文件再次添加暂存区
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
3.6.3 查看状态(工作区的修改添加到了暂存区)
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
3.7 历史版本
3.7.1 查看历史版本
- 基本语法
git reflog 查看版本信息
git log 查看版本详细信息 - 案例实操
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git reflog
3f21f1c (HEAD -> master) HEAD@{0}: commit: third commit
c22c179 HEAD@{1}: commit: second commit
466668c HEAD@{2}: commit (initial): first commit
3.7.2 版本穿梭
- 基本语法
git reset --hard 版本号 - 案例实操
--首先查看当前的历史记录,可以看到当前是在 3f21f1c 这个版本
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git reflog
3f21f1c (HEAD -> master) HEAD@{0}: commit: third commit
c22c179 HEAD@{1}: commit: second commit
466668c HEAD@{2}: commit (initial): first commit
--然后查看文件 hello.txt
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ cat hello.txt
hello tianyu! hello git! 2222222222
hello tianyu! hello git! 3333333333
hello tianyu! hello git! 4444444444
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
--切换到 466668c 版本,也就是我们第一次提交的版本
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git reset --hard 466668c
HEAD is now at 466668c first commit
--切换完毕之后再查看历史记录,当前成功切换到了 466668c 版本
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ git reflog
466668c (HEAD -> master) HEAD@{0}: reset: moving to 466668c
3f21f1c HEAD@{1}: commit: third commit
c22c179 HEAD@{2}: commit: second commit
466668c (HEAD -> master) HEAD@{3}: commit (initial): first commit
--然后查看文件 hello.txt,发现文件内容已经变化
85411@DESKTOP-FR01288 MINGW64 /d/ProgramData/Git-Space/git-demo (master)
$ cat hello.txt
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
hello tianyu! hello git!
Git 切换版本,底层其实是移动的 HEAD 指针,具体原理如下图所示。