命令名称 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m “日志信息” 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
git config --global user.name 用户名
git config --global user.email 邮箱
全局范围的签名设置:
Aiw@DESKTOP-2K268H6 MINGW64 ~/Desktop
$ git config --global user.name Aiw
Aiw@DESKTOP-2K268H6 MINGW64 ~/Desktop
$ git config --global user.email aiw@1314.com
Aiw@DESKTOP-2K268H6 MINGW64 ~/Desktop
$ cat ~/.gitconfig
[user]
name = Aiw
email = aiw@1314.com
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看 到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码。
※注意:这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任何关系。
git init
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo
$ git init
Initialized empty Git repository in E:/Git-Space/git-demo/.git/
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ ll -a
total 4
drwxr-xr-x 1 Aiw 197121 0 Aug 6 14:26 ./
drwxr-xr-x 1 Aiw 197121 0 Aug 6 14:23 ../
drwxr-xr-x 1 Aiw 197121 0 Aug 6 14:26 .git/
查看.git文件夹下文件,由于.git是隐藏文件,所以需要指定 -a 进行查看;ll 是 ls -l 的别名
git status
① 首次查看(工作区没有任何文件)
Aiw@DESKTOP-2K268H6 MINGW64 /e/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)
② 新增文件(hello.txt)
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ vim hello.txt
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ ll
total 1
-rw-r--r-- 1 Aiw 197121 42 Aug 6 14:46 hello.txt
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ cat hello.txt
hello, git!!!
hello, git!!!
hello, git!!!
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ tail -n 1 hello.txt
hello, git!!!
输入
vim hello.txt
后,按“i”键进入编辑模式,输入一行“hello, git!!!”若退出编辑模式,按“esc”键,最后敲":wq"保存退出
退出编辑模式后,敲“yy”是复制当前行,“p”是粘贴到下一行
③ 再次查看(检测到未追踪的文件)
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
git add 文件名
可以指定文件名,也可以直接写“.”,代表所有文件
执行该命令,则指定文件处于跟踪状态
① 将工作区的文件添加到暂存区
Aiw@DESKTOP-2K268H6 MINGW64 /e/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
此处警告,是Git默认转换行末换行符
② 查看状态(检测到暂存区有新文件)
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: hello.txt
使用
git rm --cached 文件名
命令可以清除暂存区的文件
git commit -m “日志信息” 文件名
可以省略文件名,表示所有文件
未跟踪状态的文件无法提交,需要先git add 文件名
若提交过到工作区的日志信息已经存在(
git commit -m “日志信息” 文件名
执行过),修改文件后,执行git commit -am "日志信息" 文件名
可以省略add这一步,因为git commit -am可以提交跟踪过的文件
① 将暂存区的文件提交到本地库
Aiw@DESKTOP-2K268H6 MINGW64 /e/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) 7f64f7b] first commit!
1 file changed, 3 insertions(+)
create mode 100644 hello.txt
② 查看状态(没有文件需要提交)
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
使用
git reflog
命令查看引用日志信息;使用git log
命令查看详细日志信息Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master) $ git reflog 7f64f7b (HEAD -> master) HEAD@{0}: commit (initial): first commit! Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master) $ git log commit 7f64f7b455b074b703f0965cbaefa71f858c461d (HEAD -> master) Author: Aiw
Date: Sat Aug 6 15:29:05 2022 +0800 first commit!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
修改hello.txt内容为:
hello, git!!!666
hello, git!!!
hello, git!!!
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
输出说明此时修改的hello.txt未被添加到暂存区,也就是未被跟踪状态
Aiw@DESKTOP-2K268H6 MINGW64 /e/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
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: hello.txt
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git commit -m "second commit!" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master bd40f0e] second commit!
1 file changed, 1 insertion(+), 1 deletion(-)
对应已跟踪的文件,修改后,可以跳过add命令,直接使用
git commit -am "日志信息" 文件名
命令
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git reflog
bd40f0e (HEAD -> master) HEAD@{0}: commit: second commit!
7f64f7b HEAD@{1}: commit (initial): first commit!
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git log
commit bd40f0e2dbe4d010d38105796753e489630dc5e8 (HEAD -> master)
Author: Aiw
Date: Sat Aug 6 15:50:12 2022 +0800
second commit!
commit 7f64f7b455b074b703f0965cbaefa71f858c461d
Author: Aiw
Date: Sat Aug 6 15:29:05 2022 +0800
first commit!
Head指向第二次提交的版本,再次查看文件就会显示所指向的版本:
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master) $ cat hello.txt hello, git!!!666 hello, git!!! hello, git!!!
- 1
- 2
- 3
- 4
- 5
git reflog:查看版本信息
git log:查看版本详细信息
不再赘述。
git reset --hard 版本号
首先查看当前的历史记录,可以看到当前是在 bd40f0e 这个版本
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git reflog
bd40f0e (HEAD -> master) HEAD@{0}: commit: second commit!
7f64f7b HEAD@{1}: commit (initial): first commit!
切换到 7f64f7b 版本,也就是第一次提交的版本
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git reset --hard 7f64f7b
HEAD is now at 7f64f7b first commit!
切换完毕之后再查看历史记录,当前成功切换到了 86366fa 版本
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ git reflog
7f64f7b (HEAD -> master) HEAD@{0}: reset: moving to 7f64f7b
bd40f0e HEAD@{1}: commit: second commit!
7f64f7b (HEAD -> master) HEAD@{2}: commit (initial): first commit!
然后查看文件 hello.txt,发现文件内容已经变化
Aiw@DESKTOP-2K268H6 MINGW64 /e/Git-Space/git-demo (master)
$ cat hello.txt
hello, git!!!
hello, git!!!
hello, git!!!
vim小技巧:鼠标左键双击即复制,鼠标中键单击即粘贴;或者左键选中,直接中键即粘贴
Git 切换版本,底层其实是移动的 HEAD 指针,具体原理如下图所示。