[译]git status

git status

git status命令能展示工作目录和stage区的状态. 使用他你能看到那些修改被staged到了, 哪些没有, 哪些文件没有被Git tracked到. git status不显示已经commit到项目历史中去的信息. 看项目历史的信息要使用git log.

用法

git status

列出哪些文件被staged了, 哪些文件没有被staged到, 哪些文件没有被tracked到.

讨论

git status相对来说是一个简单的命令. 他简单的展示状态信息. 输出的内容分为3个分类/组

# On branch master
# Changes to be committed:  (已经在stage区, 等待添加到HEAD中的文件)
# (use "git reset HEAD <file>..." to unstage)
#
#modified: hello.py
#
# Changes not staged for commit: (有修改, 但是没有被添加到stage区的文件)
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#modified: main.py
#
# Untracked files:(没有tracked过的文件, 即从没有add过的文件)
# (use "git add <file>..." to include in what will be committed)
#
#hello.pyc

Ignoring Files

没有tracked的文件分为两类. 一是已经被放在工作目录下但是还没有add的, 另一类是一些编译了的程序文件(如 .pyc .obj .exe等). 一旦这些我们不想add的文件一多起来, git status的输出简直没法看, 一大堆的状态信息怎么看? 

基于这个原因. Git让我们能在一个特殊的文件.gitignore中把我们要忽略的文件放在其中. 每一个想忽略的文件应该独占一行, *这个符号可以作为通配符使用. 例如在项目根目录下的.gitignore文件中加入下面内容能阻止.pyc文件出现在git status中:

*.pyc

例子

在每次commit之前先使用git status检查文件状态是一个很好的习惯, 这样能防止你不小心commit了你不想commit的东西. 下面的例子展示stage 前后的状态, 并最后commit一个快照.

# Edit hello.py
git status
# hello.py is listed under "Changes not staged for commit"
git add hello.py
git status
# hello.py is listed under "Changes to be committed"
git commit
git status
# nothing to commit (working directory clean)

第一个状态输出显示了这个文件没有staged. git add将影响第二个git status的输出, 最后一个git status告诉我们没有什么能commit了—工作目录已经和最近的commit相匹配了. 有些命令 (如, git merge) 要求工作目录是clean状态, 这样就不会不小心覆盖更新了.

原文地址:https://www.cnblogs.com/irocker/p/git-status.html