Git命令大全 一 git log相关 1 2 3 git log -n4 --oneline git log --all git log --graph
二 .git 文件中相关内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 [syz@syz-x542un refs]$ cd heads/ [syz@syz-x542un heads]$ pwd /home/syz/TestGit/firstProject/.git/refs/heads [syz@syz-x542un heads]$ ls -l 总用量 12 -rw-r--r-- 1 syz syz 41 6月 21 17:05 master -rw-r--r-- 1 syz syz 41 6月 21 17:08 temp -rw-r--r-- 1 syz syz 41 6月 21 17:11 temp2 [syz@syz-x542un heads]$ cat master 068a96580597b8fff9395a1a46448f700da14fa4 [syz@syz-x542un heads]$ git cat-file -t 068a96580597b8 commit [syz@syz-x542un heads]$ git branch -v * master 068a965 mv test2.java test2.py temp 068a965 mv test2.java test2.py temp2 93eeff6 temp2 [syz@syz-x542un objects]$ ls -l 总用量 44 drwxr-xr-x 2 syz syz 4096 6月 21 17:05 06 drwxr-xr-x 2 syz syz 4096 6月 21 16:59 27 drwxr-xr-x 2 syz syz 4096 6月 21 17:11 2a drwxr-xr-x 2 syz syz 4096 6月 21 17:00 49 drwxr-xr-x 2 syz syz 4096 6月 21 16:59 53 drwxr-xr-x 2 syz syz 4096 6月 21 16:50 69 drwxr-xr-x 2 syz syz 4096 6月 21 17:11 93 drwxr-xr-x 2 syz syz 4096 6月 21 16:50 d4 drwxr-xr-x 2 syz syz 4096 6月 21 17:00 e6 drwxr-xr-x 2 syz syz 4096 6月 21 16:47 info drwxr-xr-x 2 syz syz 4096 6月 21 16:47 pack [syz@syz-x542un 53]$ git cat-file -t 538d130e0f677391965a8b90b5b11284756af6ef tree [syz@syz-x542un 53]$ git cat-file -p 538d130e0f677391965a8b90b5b11284756af6ef 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 test1.txt 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 test2.py [syz@syz-x542un 53]$ git cat-file -t e69de29bb2d1d6434b blob
三 Commit, Tree, Blog之间的关系 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 [syz@syz-x542un firstProject]$ git log commit 068a96580597b8fff9395a1a46448f700da14fa4 (HEAD -> master, temp) Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 17:05:04 2021 +0800 mv test2.java test2.py commit 4916b3b59f291596afaa62ce02f089f86cc77699 Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 17:00:36 2021 +0800 modify commit 270c9942257de016045c690b0b42b9bbe79a3403 Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 16:59:41 2021 +0800 upload-sec commit d429c42c9c0973e353e4717d9dc9cbcd98c43fab Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 16:50:33 2021 +0800 first-upload [syz@syz-x542un firstProject]$ git cat-file -t 068a96580597b8fff commit [syz@syz-x542un firstProject]$ git cat-file -p 068a96580597b8fff tree 538d130e0f677391965a8b90b5b11284756af6ef parent 4916b3b59f291596afaa62ce02f089f86cc77699 author test-zjw <test-zjw@163.com> 1624266304 +0800 committer test-zjw <test-zjw@163.com> 1624266304 +0800 mv test2.java test2.py [syz@syz-x542un firstProject]$ git cat-file -p 538d130e0f677391965a8b90b5b11284756af6ef 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 test1.txt 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 test2.py [syz@syz-x542un firstProject]$ git cat-file -p e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
四 分离头指针 分离头指针 :使用git branch + commit对象id 来创建产生,表示没有绑定任何分支,一旦切换到其他分支进行操作,那么在分离头指针分支上的一切操作将会被丢失。如果分离头指针上的操作不重要,可以使用;如果重要,则需要将分离头指针分支绑定分支,使用git branch 525e14a。
五 HEAD 用法 当前分支可以通过 cat .git/HEAD
查看
1 2 3 4 git diff HEAD HEAD~1 git diff HEAD HEAD~2 git dif HEAD HEAD^
六 删除分支 1 2 git branch -d <branch_name> git branch -D <branch_name>
1.直接修改最新的commit的提交说明
2.修改之前的commit提交声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 [syz@syz-x542un firstProject]$ git log commit e7209e40de9b50335999e2cbbcc9bf6cdc5f8f5e (HEAD -> ReadMe) Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 18:08:11 2021 +0800 你好 commit 270c9942257de016045c690b0b42b9bbe79a3403 Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 16:59:41 2021 +0800 upload-sec commit d429c42c9c0973e353e4717d9dc9cbcd98c43fab Author: test-zjw <test-zjw@163.com> Date: Mon Jun 21 16:50:33 2021 +0800 first-upload git rebase -i d429c42c9c0973e353e4717d9dc9cbcd98c43fab ---- r f7583c6 upload-sec pick 366ce80 你好 --- upload-sec修改后 --- [syz@syz-x542un firstProject]$ git rebase -i d429c42c9c0973e353e4717d9dc9cbcd98c43fab [分离头指针 2e3fd8e] upload-sec修改后 Date: Mon Jun 21 16:59:41 2021 +0800 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test2.py 成功变基并更新 refs/heads/ReadMe。
注意:变基之后对应commit的对象变了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [syz@syz-x542un firstProject]$ git log --graph * commit f557ca2e46bbd4b17d4b7a88074ca8f37242a902 (HEAD -> ReadMe) | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:08:11 2021 +0800 | | 你好 | * commit 986dec82560734e808df9c87ade6eac009064280 | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 16:59:41 2021 +0800 | | upload-sec修改后 | [syz@syz-x542un firstProject]$ git log -n3 --graph * commit 366ce80788b55b99b9f4d6cbc7b2756cb5df9588 (HEAD -> ReadMe) | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:08:11 2021 +0800 | | 你好 | * commit f7583c6e32840aebb0026aea5054c68913fd99fd | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 16:59:41 2021 +0800 | | upload-sec666
八 多个连续的commit合成1个commit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 * commit 9a7846963d226bbc60f75bc162ad04009951264c (HEAD -> ReadMe) | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:58:27 2021 +0800 | | commit 4.txt | * commit 30308d21324fc6c286768f4e8a01304eac6aadac | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:58:16 2021 +0800 | | commit 3.txt | * commit a309f87da9c4315e6911f621cca6192fd775b975 | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:58:05 2021 +0800 | | commit 2.txt | * commit cff9ea3c76588c81b3bda0997d33f57c140efb1d | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:57:52 2021 +0800 | | commit 1.txt | * commit e70b5f67e9f759e7edfaa0edbd5105c7c689f843 | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:08:11 2021 +0800 | | 你好
说明: 假设我要合并最近的四个commit,所以使用git rebase -i e70b5f67e9f759e7edfaa0edbd5105c7c689f843
,保留第一个commit,选择pick,其他的需要合并的使用squash融合为前一个提交。保存后,进入交互界面,再添加融合后的注释,保存退出即可。
结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [syz@syz-x542un firstProject]$ git rebase -i e70b5f67e9f759e7edfaa0edbd5105c7c689f843 [分离头指针 c4d4020] 四和一 commit 1.txt commit 2.txt commit 3.txt Date: Mon Jun 21 18:57:52 2021 +0800 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 1.txt create mode 100644 2.txt create mode 100644 3.txt create mode 100644 4.txt 成功变基并更新 refs/heads/ReadMe。 [syz@syz-x542un firstProject]$ git log --graph * commit c4d4020f9fb2e69d190874c7ed6aa8f874e563b1 (HEAD -> ReadMe) | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:57:52 2021 +0800 | | 四和一 | commit 1.txt | commit 2.txt | commit 3.txt | | commit 4.txt | * commit e70b5f67e9f759e7edfaa0edbd5105c7c689f843 | Author: test-zjw <test-zjw@163.com> | Date: Mon Jun 21 18:08:11 2021 +0800 | | 你好
九 比较暂存区和HEAD的文件内容的差异 1 2 git diff --cached git diff [file]
十 撤销暂存区,使之修改的内容处于工作区 1 2 3 git reset HEAD git diff --cached git diff
十一 取消工作区,恢复和暂存区一样的内容 1 git checkout [file_name]
十二 丢失最近的几次提交 1 git reset --hard 2c67af04f8
十三 紧急修复bug,git stash暂存当前工作区 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [syz@syz-x542un firstProject] $ git stash 保存工作目录和索引状态 WIP on ReadMe: 3488691 upload[syz@syz-x542un firstProject] $ git stash pop 删除 2 .txt 位于分支 ReadMe 尚未暂存以备提交的变更: (使用 "git add/rm <文件>..." 更新要提交的内容) (使用 "git restore <文件>..." 丢弃工作区的改动) 修改: 1 .txt 删除: 2 .txt 修改尚未加入提交(使用 "git add" 和/或 "git commit -a" ) 丢弃了 refs/stash@{0 }(640843 f47be8b1e7eb8db9b7f8741dd3eb68f57d)
git stash apply
和 git stash pop
的区别是:
使用git stash apply
恢复工作区并且stash 栈中还存在stash 记录, 而使用git stash pop
恢复工作区后stash栈中不再有stash记录了。