Git常用其它命令

Git常用其它命令

1、git archive

从命名树创建文件的存档。

# 查看支持的归档格式
$ git archive -l
tar
tgz
tar.gz
zip
# 生成一个可供发布的压缩包
$ git archive -o latest.zip HEAD

$ ls
a.txt  c.txt  d.txt  e.txt  f.txt  latest.zip  new.txt

2、git shortlog

汇总 git 日志输出。

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn
    10  zsx242030

3、git blame

追溯文件的历史修改记录。

# 显示指定文件是什么人在什么时间修改过
$ git blame a.txt
ddbfc0b8 (zsx242030 2023-05-27 09:53:59 +0800 1) branch_a
8cb57f66 (zsx242030 2023-05-27 13:31:13 +0800 2) branch_b
d735ee3c (zsx242030 2023-05-27 17:04:00 +0800 3) branch_c
d735ee3c (zsx242030 2023-05-27 17:04:00 +0800 4) branch_d

4、git reflog

查看引用日志。

# 显示当前分支的最近几次提交
# 所有分支的所有操作记录,包括已经删除的commit记录和reset记录
$ git reflog
d735ee3 (HEAD -> master, origin/master, origin/branch_d, origin/HEAD) HEAD@{0}: clone: from https://gitee.com/zsx242030/sm.git

当错误使用 git reset --hard HEAD~3 回退的时候可以使用 git reflog 查看日志信息,进行正确的回退。

显示的是一个 HEAD 指向发生改变的时间列表,在你切换分支、用 git commit 进行提交、以及用 git reset 撤销

commit 时,HEAD 指向会改变,但当你进行 git checkout – filename 撤销或者 git stash 存储文件等操作时,

HEAD 并不会改变,这些修改从来没有被提交过,因此 reflog 也无法帮助我们恢复它们。

git reflog 不会永远保持,Git 会定期清理那些用不到的对象,不要指望几个月前的提交还一直在那里。

5、git ls-files

列出 git index 包含的文件。

$ git ls-files
a.txt
c.txt
d.txt
e.txt
f.txt
new.txt

6、git restore

撤销文件。

在工作区修改代码时,如果发现修改错误了,想回到修改之前的状态,就可以使用该命令。

撤消工作区的修改返回到最近一次add(缓存区)的版本或者最近一次commit(当前版本库)的版本。

# 新版本git提示用该命令进行撤销
$ git restore file
# 如果已经add进暂存区
$ git restore --staged file

7、git switch

创建和切换分支。

创建分支和撤销都用checkout容易分不清,因此新版本创建分支推荐用。

# 创建并切换到dev
$ git switch -c branch
# 直接切换到已有的dev分支
$ git switch branch

8、git show-branch

图示当前分支历史。

# 图示当前分支历史
$ git show-branch
[master] branch_d | update a.txt | update b.txt | update e.txt
# 图示所有分支历史
$ git show-branch --all
* [master] branch_d | update a.txt | update b.txt | update e.txt
 ! [origin/HEAD] branch_d | update a.txt | update b.txt | update e.txt
  ! [origin/branch_a] branch_a | update a.txt | add new.txt
   ! [origin/branch_b] branch_b | update a.txt | add new.txt
    ! [origin/branch_c] branch_c | update a.txt | delete e.txt
     ! [origin/branch_d] branch_d | update a.txt | update b.txt | update e.txt
      ! [origin/master] branch_d | update a.txt | update b.txt | update e.txt
-------
*+   ++ [master] branch_d | update a.txt | update b.txt | update e.txt
*+  +++ [origin/branch_c] branch_c | update a.txt | delete e.txt
*+ ++++ [origin/branch_b] branch_b | update a.txt | add new.txt
*++++++ [origin/branch_a] branch_a | update a.txt | add new.txt

9、git whatchanged

显示提交历史对应的文件修改。

$ git whatchanged
commit d735ee3c30c49d70e731df8922649ce40f4c5507 (HEAD -> master, origin/master, origin/branch_d, origin/HEAD)
Author: zsx242030 <2420309401@qq.com>
Date:   Sat May 27 17:04:00 2023 +0800

    branch_d | update a.txt | update b.txt | update e.txt

:100644 100644 8515a07... 3d9f4cd... M  a.txt
:100644 000000 e69de29... 0000000... D  b.txt
:000000 100644 0000000... c8616fd... A  e.txt

commit 8cb57f667e7681be447d98051da7a3b5123f8c33 (origin/branch_c)
Author: zsx242030 <2420309401@qq.com>
Date:   Sat May 27 13:31:13 2023 +0800

    branch_c | update a.txt | delete e.txt

:100644 100644 bfc4d22... 8515a07... M  a.txt
:100644 000000 e69de29... 0000000... D  e.txt

commit 5b05cb640c164e86db3f14c9e3af4529969ba534 (origin/branch_b)
Author: zsx242030 <2420309401@qq.com>
Date:   Sat May 27 13:14:01 2023 +0800

    branch_b | update a.txt | add new.txt

:100644 100644 dd18a66... bfc4d22... M  a.txt

10、git ls-tree

显示某个git对象。

$ git ls-tree HEAD~9
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    a.txt

$ git ls-tree HEAD
100644 blob 3d9f4cdcd79fcdb93e8fd327cd1df92cdc69648d    a.txt
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    c.txt
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    d.txt
100644 blob c8616fde5dfdac2ba2750e5e651039d287221dba    e.txt
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    f.txt
100644 blob 3510812807e8a7055b1082bce8dab17ab478666d    new.txt

11、git rev-parse

显示某个ref对于的SHA1 HASH。

$ git rev-parse master
d735ee3c30c49d70e731df8922649ce40f4c5507

$ git log --oneline
d735ee3 (HEAD -> master, origin/master, origin/branch_d, origin/HEAD) branch_d | update a.txt | update b.txt | update e.txt
8cb57f6 (origin/branch_c) branch_c | update a.txt | delete e.txt
5b05cb6 (origin/branch_b) branch_b | update a.txt | add new.txt
ddbfc0b (origin/branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt

12、git gc

清理Git暂存区。

$ git gc
Counting objects: 28, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (28/28), done.
Total 28 (delta 7), reused 0 (delta 0)

13、git grep

搜索文件中的文本内容。

# 文件中搜索文本b
$ git grep "b"
a.txt:branch_a
a.txt:branch_b
a.txt:branch_c
a.txt:branch_d
e.txt:branch_d
new.txt:branch_a_new
new.txt:branch_b_new

# 文件中搜索文本branch_b
$ git grep "branch_b"
a.txt:branch_b
new.txt:branch_b_new

14、git fsck

恢复误删 stash 贮藏的数据。

$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (12/12), done.

15、gitk

内建的图形化。

$ gitk
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值