How to master the Git status command
If distributed version control is your thing, you need to master the intricacies of the files on your working tree, and that means you must learn the Git status commands and understand the information it can provide.
The goal of the Git status command is to provide you information about the state of the various files in your working directory, also known as the working tree. The six different states the Git status command can inform you of are:
- There is no commit history
- There are untracked files
- There are changes to be committed
- The working tree is clean
- Files have been modified
- Files have been deleted
How to use the Git status command
To really learn how to use the Git status command, follow these steps:
- Issue a git init command to create a new repository
- Issue the git status command and note the results
- Add a new file named index.html to the repository
- Issue the git status command and inspect the results
- Issue a git add . command
- Run the git status command and note the results
- Issue a Git commit
- Run the git status command and inspect the results
- Modify the index.html file
- View the results of the git status command
- Delete the index.html file
- Inspect the results of the git status command
Going through this cycle of git status commands will demonstrate each of the states the tool will report upon.
Nothing to commit status
When a new repository is created, the git status command reports there is nothing to commit:
$ git init Initialized empty Git repository in C:/_bart/.git/ $ git status On branch feature No commits yet nothing to commit (create/copy files and use "git add" to track)
Untracked git status
When new files are added to a repository, git status reports untracked files
$ touch index.html $ git status On branch feature No commits yet Untracked files: index.html nothing added to commit but untracked files present (use "git add" to track)
Changes to be committed git status
When new files are added to the index, the git status reports changes to be committed:
$ git add index.html $ git status On branch feature No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.html
Clean working tree status
After a commit, the git status will report a clean working tree. This can also be triggered by running a git clean command:
$ git commit -m "Git status and working tree commit" [feature (root-commit) 1159b26] Git status and working tree commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 134221 index.html $ git status On branch feature nothing to commit, working tree clean
Modified git status
If a file is edited the status reported will be modified:
$ echo "Hello World" >> index.html $ git status On branch feature 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: index.html no changes added to commit (use "git add" and/or "git commit -a")
And finally, if you remove a file, the git status will report that a file has been deleted:
$ rm index.html $ git status On branch feature Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: index.html
And those are the ‘ins and outs’ of the git status command. Learn these various different statuses, and you will be a master of the command.