Git中如何查看当前状态?代码举例讲解

在Git中查看当前状态,可以使用git status命令。
git status命令的基本用法如下:

  1. git status显示当前分支的状态,包括有变更的文件、要提交的文件、没有跟踪的文件等信息。
  2. git status -s 以简短信息显示状态。
  3. git status -v 以详细信息显示状态。
  4. git status –ignored 展示被忽略的文件。
  5. git status –short 以简短信息显示状态,和-s相同。

举几个例子:

查看当前分支状态:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hello.py

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

    newfile.py

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

以简短信息显示状态:

$ git status -s
M  hello.py          
?? newfile.py

查看被忽略的文件:

$ git status --ignored    
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hello.py

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

    newfile.py
    .env

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

    .idea/
    *.pyc

可以看到,.env文件和.idea文件夹被忽略。