Three States of Git (Git Workflow)
What Will We Learn?
The three states of Git — modified, staged and committed with the help of Git general workflow.
Git General Workflow
It’s good to know what is the general workflow while we are perfoming version control using git before we go into the details of git states. This will be a short summary.
Step 1: Initialize Git Repository
First step is to initialize your local project directory as Git repository. Go into the root of your project and hit:
git initThis is a way of telling Git — “Hey, I will be using you (git) for Version Control from now on.”
Step 2: Stage your Changes
Assuming the project is python project with app.py and README.md. Once you reach the significant level in your app development, you can add it to index using:
git add app.pyThis is a way of telling Git — “Hey, I want to include this version of app.py in my next commit.”
Step 3: Commit Your Changes
The final step for local repo is to create Git database or local Git Repository by actually commiting the changes you just have staged.
git commit -m 'Initial Version of app'This is a way of telling Git — “Please, commit my changes and save in your database.”
And obviously, this general workflow hides the nitty-gritty details and gotchas and best practices of using git. But the motto here is just to make yourself familiar with the general workflow to make you ready to learn the three states of Git.
The Three States of Git
Generally, we can say, Git has three main states that your files can be in: modified, staged, and committed. In the very start, we can also refer modified as newfile but it’s okay to call it modified just to remember things easily.
Modified
You have make changes to your file in working directory but not yet staged for changes to include in next commit. You editing your files are responsible for this.
Tip
No matter what, Git will not touch your working directory. You and you only are responsible for modifying your working directory.
Staged
You have included your modified file to include in next commit. git add is responsibe for this.
Committed
You have successfully saved your file in the local git database (repository). git commit is responsible for this. Once you have committed the file you staged, it will automatically be removed from the staged area.
Visualizing the States of Git
sequenceDiagram
participant WorkDir as Working Directory
participant Staging as Staging Area
participant LocalRepo as Git Repositiory
Note over WorkDir: app.py (modified)<br/>README.md (modified)
Note over Staging: (nothing yet)
Note over LocalRepo: (nothing yet)
WorkDir ->> Staging: git add app.py
Note over Staging: app.py (staged)
Note over WorkDir: README.md (modified)
Note over LocalRepo: (nothing yet)
Staging ->> LocalRepo: git commit -m 'Initial Version of App'
Note over LocalRepo: Commit XYZ (app.py)<br>Initial Version of App
Note over Staging: (removed after committed)
Note over WorkDir: README.md (modified)
Initially both
app.pyandRAEDME.mdare in modified state. Noting yet staged or committed.Since only
app.pyis staged (git add app.py),README.mdis still in modified state. No commit yet means nothing in committed state.After commit has been applied to staged file (
app.py) usinggit commit, commit appears in committed state alongwith commit message and file (app.py) and Staging Area is now cleared. Till this point,README.mdhas not been added to Staging Area, so no commit shows it and hence is still in modified state.