为了账号安全,请及时绑定邮箱和手机立即绑定

查看文件状态和提交历史

标签:
Git

查看文件状态

git status

1、如果所有已跟踪文件都未被更改过:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

该命令显示了当前分支 main,并且显示当前分支同远程仓库的分支没有偏离,无需提交任何内容,工作目录是干净的状态。

2、如果新建文件:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.js

nothing added to commit but untracked files present (use "git add" to track)

新建文件 hello.js 出现在 Untracked files 下面,说明 hello.js 未被纳入 Git 跟踪的范围。
如果想要跟踪这个文件,可以使用指令 git add hello.js, 此时再运行 git status ,会看到 hello.js 已被跟踪,处于暂存区(在 Changes to be committed 下面的文件)。

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   hello.js

处于暂存区的文件,就意味着该文件会出现在下一次的提交中( git commit )。

3、如果修改文件:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

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:   hello.js

no changes added to commit (use "git add" and/or "git commit -a")

修改文件 hello.js 出现在 Changes not staged for commit 下面,说明 hello.js 内容发生了变化,但是没有放到暂存区。
同样执行 git add hello.js,将 hello.js 的修改内容放置暂存区,此时再运行 git status

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.js

文件状态简单预览

通过 git status -sgit status --short 命令,可以得到文件状态的简单预览信息。

$ git status -s
MM hello.js # 左边的 M 表示文件被修改而且已放入暂存区;右边的 M 表示文件被修改却未放入暂存区。两个 M 则表示在工作区被修改并放入暂存区后,又在工作区中被修改,所以暂存区和工作区都有该文件被修改了的记录
M  test.js # 左边的 M 表示文件被修改而且已放入暂存区
A  test1.js # 新创建而且已跟踪的文件
?? test2.js # 新创建却未跟踪的文件

查看提交历史

$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: zhangl <zhangl@meituan.com>
Date:   Fri Jan 26 17:11:39 2024 +0800

    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: zhangl <zhangl@meituan.com>
Date:   Thu Jan 25 19:52:13 2024 +0800

    removed unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: zhangl <zhangl@meituan.com>
Date:   Wed Jan 24 15:22:26 2024 +0800

    first commit

执行该指令,会按提交时间列出所有的提交历史,最后一次的提交历史排在最上面。
信息的内容分别为提交哈希值,作者的名字和电子邮件,提交时间以及提交说明。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
1.4万
获赞与收藏
860

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消