1、常用命令
2、实例
1、设置用户和邮箱
对于user.name和user.email来说有三个地方可以设置:
- /etc/gitconfig:整个计算机的范围(几乎不会使用),计算机上的所有用户都会使用这个
- ~/.gitconfig:针对于特定用户(很常用),创建几个仓库栋笃会用这个值
- .git/config:针对于特定项目的
2、可以进入.git目录查看相关文件
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit/.git (GIT_DIR!)
$ ls -al
total 11
drwxr-xr-x 1 16140 197609 0 May 31 23:34 ./
drwxr-xr-x 1 16140 197609 0 May 31 23:34 ../
-rw-r--r-- 1 16140 197609 23 May 31 23:34 HEAD
-rw-r--r-- 1 16140 197609 130 May 31 23:34 config
-rw-r--r-- 1 16140 197609 73 May 31 23:34 description
drwxr-xr-x 1 16140 197609 0 May 31 23:34 hooks/
drwxr-xr-x 1 16140 197609 0 May 31 23:34 info/
drwxr-xr-x 1 16140 197609 0 May 31 23:34 objects/
drwxr-xr-x 1 16140 197609 0 May 31 23:34 refs/
3、删除.git文件,再添加回来
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ rm -rf .git/
#可以看出,这里删除掉以后就没有了master分支
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit
$ ls -al
total 0
drwxr-xr-x 1 16140 197609 0 May 31 23:54 ./
drwxr-xr-x 1 16140 197609 0 May 31 23:32 ../
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit
$ git init
Initialized empty Git repository in D:/笔记和课程/code review/mygit/.git/
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls -al
total 4
drwxr-xr-x 1 16140 197609 0 May 31 23:55 ./
drwxr-xr-x 1 16140 197609 0 May 31 23:32 ../
drwxr-xr-x 1 16140 197609 0 May 31 23:55 .git/
也就是说,.git文件可以通过git init来恢复。
4、创建test.txt文件,添加到本地版本库
创建文件,并查看状态:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
touch test.txt
#在文件中写上hello world
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt #红色
可以看出test.txt还没加入暂存区。
#提交到暂存区
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt #绿色
#从暂存区移除
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git rm --cached test.txt
rm 'test.txt'
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt #红色
nothing added to commit but untracked files present (use "git add" to track)
真实提交:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit
Aborting commit due to empty commit message.
#什么都不写直接放弃此次提交
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit
[master (root-commit) afd6099] initial commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
#使用了本地文本工具在行首添加了commit的注释
查看第一次提交的日志:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git log
commit afd6099bfb903668619684edb0c36cd48f0df033 (HEAD -> master)
Author: Oliver <16140@qq.com>
Date: Wed Jun 1 07:59:06 2022 +0800
initial commit
其实git的提交id是一个摘要值,这个摘要值实际上是sha1计算出来的。
5、创建test2.txt文件,并添加
#创建文件
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ echo hello world > test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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)
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add test2.txt
warning: LF will be replaced by CRLF in test2.txt.
The file will have its original line endings in your working directory
#提交
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit -m 'the second commit'
[master b1ecd6e] the second commit
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
现在想把the second commit的提交信息修改为second commit,可以使用下面的方式:
#这个指令可以
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit --amend
[master 12af739] the second commit
Date: Thu Jun 2 08:07:04 2022 +0800
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git log
commit 12af739eda8283dfad3675c63efc68fcd1dbaecc (HEAD -> master)
Author: Oliver <16140@qq.com>
Date: Thu Jun 2 08:07:04 2022 +0800
the second commit
commit afd6099bfb903668619684edb0c36cd48f0df033
Author: Oliver <16140@qq.com>
Date: Wed Jun 1 07:59:06 2022 +0800
initial commit
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit --amend
[master 5ebb33a] second commit
Date: Thu Jun 2 08:07:04 2022 +0800
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
6、使用git rm删除掉已经提交的test2.txt文件,再恢复回来
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git rm test2.txt
rm 'test2.txt'
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test2.txt #绿色
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls
test.txt
#如果真的想删除就执行git commit -m '提交信息'把修改提交到暂存区
#如果不需要删除想要恢复,则需要使用git reset...
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git reset HEAD test2.txt
Unstaged changes after reset:
D test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -- test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
nothing to commit, working tree clean
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls
test.txt test2.txt
7、使用rm命令删除掉已经提交的test2.txt文件,再恢复回来
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ rm test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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: test2.txt #红色
#删除后未交到暂存区,可以立刻恢复回来
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -- test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
nothing to commit, working tree clean
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls
test.txt test2.txt
#真的想要删除可以这样执行
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ rm test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit -m 'delete test2.txt'
[master 3ce0482] delete test2.txt
1 file changed, 1 deletion(-)
delete mode 100644 test2.txt
区别:
- git rm:是删除了文件,并提交到暂存区,要想恢复要先用reset从暂存区恢复到工作区,然后再使用checkout移除
- rm:就是简单的rm,还没有提交到暂存区,这个直接使用checkout去恢复即可
8、使用git mv把test.txt移动到test2.txt(重命名)
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git mv test.txt test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: test.txt -> test2.txt #绿色
想要恢复可以这样操作:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git reset HEAD test.txt
Unstaged changes after reset:
D test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls
test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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: test.txt #红色
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git reset HEAD test2.txt
Unstaged changes after reset:
D test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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 #红色
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")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -- test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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)
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls
test.txt test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ rm test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
nothing to commit, working tree clean
9、使用mv把test.txt移动到test2.txt(重命名)
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ mv test.txt test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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
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")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -- test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (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)
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls
test.txt test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ rm test2.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
nothing to commit, working tree clean
10、给log设置格式
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git log --pretty=format:"%h - %an, %ar : %s"
3ce0482 - Oliver, 26 minutes ago : delete test2.txt
5ebb33a - Oliver, 40 minutes ago : second commit
afd6099 - Oliver, 25 hours ago : initial commit
\