2778 views

A picture is worth a thousand words! The Animated Git series introduces core Git concepts with pictures and animations.

The three Git states

Your work can be in three main states with Git:

  • Modified: changes made to your project working directory
  • Staged: changes prepared in the staging area for your next commit
  • Committed: changes stored in your git repository

The intermediate staged state lets you create fine-grained commits, each focused on one specific modification.

Modified

git status displays the current state of your work:

$ git status
nothing to commit, working tree clean

There is no new work yet on your project! Let’s modify files A, B, C and E, and create a new File D. We’ll leave File C unmodified:

animated-git-staging-area-modified

Four of your files are now in the modified state, in the “changes not staged for commit” section. The new File D isn’t tracked yet by Git:

$ git status
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:   File A
    modified:   File B
    modified:   File C
    modified:   File E

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

	File D

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

For our example, the changes in File A, D, E are related (thus you want to keep File B and F changes for a different commit). Add the related work to the staging area with git add.

$ git add "File A" "File D" "File E"

P.S. Have a look at the git add -i and git add -p interactive modes and the various advanced options available.

Staged

animated-git-staging-area-staged

Your related work for those files is now in the staged state in the “changes to be committed” section:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


    modified:   File A
    new file:   File D
    modified:   File E


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:   File B
    modified:   File F

Committed

You can now commit the work on File A, D and E and safely store this project snapshot in your local repository:

$ git commit -m "add feature X"

The staging area is now empty:

animated-git-staging-area-committed

You have the two remaining files in the modified state:

$ 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:   File B
    modified:   File F


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

We recommend the following articles to learn more about managing changes from your working directory to your repository:

This Animated Git course is just the beginning. Follow us on @Wakandasoft to catch the next one!