git一些常用命令
git checkout + 文件名
上次说过,git reset head + 文件名 是将暂存区的文件取消暂存,这个地方不是很好理解,再举个例子:
1、我们有一个文件在工作区并提交到版本区叫test.txt;
2、这时候删除工作区文件并将工作区删除这一改变提交到暂存区,此时工作区没有test.txt文件,暂存区有test.txt文件删除记录;
3、现在想恢复test.txt文件,该怎么做呢?
➜ gitTest git:(master) ls
test.txt
#工作区有 test.txt
➜ gitTest git:(master) rm test.txt
➜ gitTest git:(master) ✗ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test.txt
#删除工作区内容
no changes added to commit (use "git add" and/or "git commit -a")
➜ gitTest git:(master) ✗ git add .
➜ gitTest git:(master) ✗ ls
➜ gitTest git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.txt
#提交到暂存区
此时工作区没有test.txt文件,而暂存区有,将暂存区的文件恢复到工作区我们用
git rest head + 文件名
➜ gitTest git:(master) ✗ ls #此时工作区没有内容
➜ gitTest git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.txt
#暂存区删除文件,如果提交,那么提交区也会删除test.txt文件
➜ gitTest git:(master) ✗ git reset head test.txt
#将暂存区文件改变转移到工作区
Unstaged changes after reset:
D test.txt
➜ gitTest git:(master) ✗ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
#此时没有追踪删除文件这一个改变,此时状态和在工作区删除test.txt文件状态一致
进行以上操作后,仅有工作区删除了 test.txt 文件,我们继续用 git checkout 文件名,来恢复 test.txt
➜ gitTest git:(master) ✗ git checkout test.txt
Updated 1 path from the index
➜ gitTest git:(master) ls
test.txt
➜ gitTest git:(master) git status;
On branch master
nothing to commit, working tree clean
➜ gitTest git:(master)
可以看到工作区有文件,此时状态是工作区干净,表明工作区文件内容和版本库中内容一致。
git checkout + 文件名
功能:
1、若暂存区没有修改,则将工作区修改丢弃,使文件与版本库保持一致。
2、若暂存区有修改,则丢弃掉工作区相对于暂存区的修改,是文件与暂存区内容保持一致。
下面演示功能1:
(1) test.txt 文件内有一行提交到版本库
(2)在工作区向test.txt中添加一行
(3)用git checkout 命令
(4)查看test.txt文件内容
➜ gitTest git:(master) ls
#工作区是空的
➜ gitTest git:(master) echo 'the first line' >> test.txt
➜ gitTest git:(master) ✗ cat test.txt
the first line
#新建并存储一行
➜ gitTest git:(master) ✗ git add .
➜ gitTest git:(master) ✗ git commit -m 'one line'
[master d4b3982] one line
1 file changed, 1 insertion(+)
create mode 100644 test.txt
#提交到版本库,此时版本库内test.txt只有一行
➜ gitTest git:(master) echo 'the second line' >> test.txt
➜ gitTest git:(master) ✗ cat test.txt
the first line
the second line
#修改工作区内容,此时工作区和版本库test.txt内容差一行
➜ gitTest git:(master) ✗ git checkout test.txt
Updated 1 path from the index
➜ gitTest git:(master) cat test.txt
the first line
#可以看到此时工作区内容和版本库一致
下面演示功能2:
(1) test.txt 文件内有一行提交到版本库
(2)在工作区向test.txt中添加一行,并提交到暂存区
(3)在工作区向test.txt中再添加一行
(3)用git checkout 命令
(4)查看test.txt文件内容
➜ gitTest git:(master) cat test.txt
the first line
#此时只有一行
➜ gitTest git:(master) echo 'the second line' >> test.txt
➜ gitTest git:(master) ✗ cat test.txt
the first line
the second line
#增加一行
➜ gitTest git:(master) ✗ git add .
➜ gitTest git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
#添加的暂存区
➜ gitTest git:(master) ✗ echo 'the third line' >> test.txt
➜ gitTest git:(master) ✗ echo test.txt
test.txt
➜ gitTest git:(master) ✗ cat test.txt
the first line
the second line
the third line
#添加到暂存区
➜ gitTest git:(master) ✗ git checkout test.txt
Updated 1 path from the index
➜ gitTest git:(master) ✗ cat test.txt
the first line
the second line
#用完chekcout 命令,此时内容和暂存区相同
➜ gitTest git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
#此时暂存区有内容尚未提交
git rm + 文件名
git rm
在工作区和暂存区 都干净 的情况下可以用git rm + 文件名命令,相当于先删除一个文件,然后提交删除;
如果工作区或暂存区有内容,可以先删除文件,然后git add . 达到相同效果
git rm 演示
#此时工作区和暂存区都是干净的
➜ gitTest git:(master) ls
test.txt
➜ gitTest git:(master) git rm test.txt
rm 'test.txt'
➜ gitTest git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.txt
#暂存区删除
➜ gitTest git:(master) ✗ ls
#工作区也没有了
工作区不干净的情况
➜ gitTest git:(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: test.txt
#工作区有修改
no changes added to commit (use "git add" and/or "git commit -a")
➜ gitTest git:(master) ✗ git rm test.txt
error: the following file has local modifications:
#报错
test.txt
(use --cached to keep the file, or -f to force removal)
此时可以用先用rm 删除工作区文件,然后用git add . 可以达到相同的效果。
git mv 重命名文件
git mv test1 test2 是将工作区的文件test1重命名为test2,并将重命名提交到暂存区,相当于三步操作
1、将test1 复制一份并命名为test2
2、删除test1
3、将更改提交
用git mv实现重命名
➜ gitTest git:(master) ls
test1.txt
➜ gitTest git:(master) git status
On branch master
nothing to commit, working tree clean
➜ gitTest git:(master) git mv test1.txt test2.txt
➜ gitTest git:(master) ✗ ls
test2.txt
➜ gitTest git:(master) ✗ git status;
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: test1.txt -> test2.txt
回到之前状态
➜ gitTest git:(master) ✗ git reset head test1.txt
Unstaged changes after reset:
D test1.txt
➜ gitTest git:(master) ✗ git status;
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: test2.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test1.txt
# 此时将test1的改变从暂存区移回工作区,看到提示是暂存区存在新文件test2,工作区删除了test1待暂存。
➜ gitTest git:(master) ✗ git reset head test2.txt
Unstaged changes after reset:
D test1.txt
➜ gitTest git:(master) ✗ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
#将暂存区的test2移回工作区,看到test2未被追踪,表明新建文件test2,此时工作区有 test1文件和test2文件
➜ gitTest git:(master) ✗ git checkout test1.txt
Updated 1 path from the index
#将test1文件和版本库内保持一致
➜ gitTest git:(master) ✗ ls
test1.txt test2.txt
#此时工作区有test1 和test2文件
➜ gitTest git:(master) ✗ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test2.txt
nothing added to commit but untracked files present (use "git add" to track)
➜ gitTest git:(master) ✗ rm test2.txt
#删除test2文件,此时工作区是干净的
➜ gitTest git:(master) ls
test1.txt
用cp和rm实现
➜ gitTest git:(master) ls
test1.txt
➜ gitTest git:(master) cp test1.txt test2.txt
➜ gitTest git:(master) ✗ ls
test1.txt test2.txt
➜ gitTest git:(master) ✗ rm test1.txt
➜ gitTest git:(master) ✗ git add .
➜ gitTest git:(master) ✗ ls
test2.txt
➜ gitTest git:(master) ✗ git status;
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: test1.txt -> test2.txt
效果相同
git log 命令
git log可以查看历史提交信息
➜ gitTest git:(master) git log
commit b0a49e6a0db744422b53866f5ab2729d258a709b (HEAD -> master)
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 21:30:37 2020 +0800
rename
commit cca3c7998b7c13b7a0804e8a377c5acf50d3397a
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 21:16:43 2020 +0800
test1.txt
commit 1f0645ec5ba070a8bb877da1fc8f7f5ecc232a8f
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 18:18:29 2020 +0800
clear
commit a90c1e8f86e1e111ea2eb13c090e252e089c31ae
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 18:04:19 2020 +0800
2line
commit b36edf51e939e611da06fbe0007600454a6f25dc
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 18:02:43 2020 +0800
first commit
commit 4d9aa7ae06b98a41bd540710ee5fd8892209b17a
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 17:58:44 2020 +0800
delete test
commit fb5c027cc986a36b101b14365445302ada243455
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 17:56:32 2020 +0800
有一个commit id,一个作者,和提交日期以及提交信息,就是在提交时编辑的那个
git log -n 显示最近n次的提交信息
➜ gitTest git:(master) git log -3
commit b0a49e6a0db744422b53866f5ab2729d258a709b (HEAD -> master)
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 21:30:37 2020 +0800
rename
commit cca3c7998b7c13b7a0804e8a377c5acf50d3397a
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 21:16:43 2020 +0800
test1.txt
commit 1f0645ec5ba070a8bb877da1fc8f7f5ecc232a8f
Author: yibin.yang <zhangsan@git.com>
Date: Wed Jan 22 18:18:29 2020 +0800
clear
(END)
git log --pretty=oneline
以一行显示提交信息, 只显示提交id和提交信息
➜ gitTest git:(master) git log --pretty=oneline
b0a49e6a0db744422b53866f5ab2729d258a709b (HEAD -> master) rename
cca3c7998b7c13b7a0804e8a377c5acf50d3397a test1.txt
1f0645ec5ba070a8bb877da1fc8f7f5ecc232a8f clear
a90c1e8f86e1e111ea2eb13c090e252e089c31ae 2line
b36edf51e939e611da06fbe0007600454a6f25dc first commit
4d9aa7ae06b98a41bd540710ee5fd8892209b17a delete test
fb5c027cc986a36b101b14365445302ada243455 add
d4b3982ba0b727d57c1182cc2dc3c4779d4bfaf3 one line
694497bcf72c56aea1cbe69055c0ffc76ed4c9b5 rm
2b93b4342a18e730e10d86b0c3edac020a29ada8 add a line
6276b5f9a2fc65ba5391233f6712013efa46314b init commit
git log --pretty=format:"%h-%an,%ar:%s";
可以更该显示格式,有兴趣的童鞋可以试试。
更改提交信息
git commit --amend -m
如果我们在commit的时候,提交信息需要更改,这个时候我们通常用 git commit --amend -m
例如我们最新一次提交信息:
➜ gitTest git:(master) git log --pretty=oneline -1
b0a49e6a0db744422b53866f5ab2729d258a709b (HEAD -> master) rename
➜ gitTest git:(master) git commit --amend -m 'successfully renamed'
[master 1813a29] successfully renamed
Date: Wed Jan 22 21:30:37 2020 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
rename test1.txt => test2.txt (100%)
➜ gitTest git:(master) git log --pretty=oneline -1
1813a290f127d138bf5177b20c77cf39a45a34b1 (HEAD -> master) successfully renamed
我们看到虽然提交信息更改了,提交id也更改了。
总结
我们学习了几个git本地常用的操作命令,讲解的算式比较详细了:
1、git checkout
2、git rm
3、git mv
4、git log
5、git commit --amend -m ‘ 内容’
大家都回忆一下每个命令的功能和用法吧。