Sitemap
amirreza lotfi

amirreza lotfi

Android Developer

Basic Git Work Flow

About the basic git mechanism in the simplest way

4 min readAug 11, 2021

--

Press enter or click to view image in full size

In this article, I will say:

  1. What is git Directory, Working Tree and Staging Area.
  2. What is the process for committing a snap shot of project.
  3. How does file state change.
  4. States of file (Untracked, committed, modified, staged)

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Suppose that you have a project that makes up of various files and you want manage them. Git is the best choose to do it.

Getting Start

For example, we our project has 4 file:

Press enter or click to view image in full size
Fig1. Creating projects files.

With command git init , we can easily create a git repository and add it to our project:

Press enter or click to view image in full size
Fig2. Initializing an empty git repository.

In general, git has consisted with 3 component :

  1. Git Directory
  2. Staging Area (or Index)
  3. Working Tree

Git Directory is where our commit is stored.
Staging Area is a place that contains files that are ready to be committed, and Working Tree consists of files that you are currently working on. It shows files which are modified, unstated and untracked and files that staged but no committed yet. For seeing what file already is in your Work Tree, Your can use this command :

git status

Press enter or click to view image in full size
Fig3. Seeing working tree by git status command.

Git status shows files that already are in your Working Tree.

Get amirreza lotfi’s stories in your inbox

Join Medium for free to get updates from this writer.

In general, every file in git, can be these states

  1. UnTracked
  2. Staged
  3. Modified
  4. Committed(Unmodified)

Untracked files, any files in your working directory that were not in your last snapshot and are not in your staging area anything with untracked file. To adding them to staging area, we can do it with this command:

git add

Press enter or click to view image in full size
Fig4. Tracking files and adding them to Staging Area

Now, our state of files converted to Staged and they add to Staging Area.

Another important concept in Git, is Commit. Commits can be thought of as snapshots or milestones of your project. To put it more simply, Commit command, save a version of your project.

What files does committed by commit command?

As I said before, a Staging Area is a place that contains files that are ready to be committed. So, a files that stored in Staging Area, committed with commit command. In a more understandable way, by default, Commit commands only look at Staging Area. For committing files, we can use this command :

git commit –m <message>

Press enter or click to view image in full size
Fig5. Commit files

When we run this command, the states of Staging Area files change to Committed or Unmodified. Also, content of Staging Area will remove. Our file committed, so it is clear that our Working Tree will be empty. Because no file to working on:

Press enter or click to view image in full size
Fig6. After committing, our working tree will be empty

Now we change the contents of the main.py and adding a line in it. If we run Git Status, we can see that state of main.py changed from Committed to Modified, and it displayed in Working Tree because we are working on:

Press enter or click to view image in full size
Fig7. Adding line to main.py
Press enter or click to view image in full size
Fig8. Changing state of main.py from Committed to Modified

When you track file with add command, Staging Area compares files that is in git repository and a file that is in Working Tree. So, all changes be founded in Working Tree. When you change content of a file, Staging Area know it as Modifier, not staged. If we want to commit it, we should add it to a Staging Area. To do this, we use Git Add command again:

Press enter or click to view image in full size
Fig9. Adding to staging area

Now we can commit our change:

Press enter or click to view image in full size
Fig10. Committing changes and seeing all commit with git.log

You may think Commit command take a copy of your project and save it in git Directory, but it didn't happen. Instead, Git stores each file under its SHA1. What that means is when you commit; Git stores a pointer to your project state. That means, if a file have not changed, it didn’t duplicate. Git just save a file that changed.

--

--