Many people usages git everyday. But very few people know how it works! Mostly coders or more specifically git users uses some command to create commit and sync with the remote repository. Although it is working for them, they soon fall into problems. As they do not know why it was working in the first place, they could not find any solution what so ever when in trouble. But to solve a problem we need to know the working procedure. That is
In the Git version control system, there are three areas where our code lives. These are called namely – Working Area, Staging Area, and Repository.
The Git version control system does not handle files that are in the Working area only. That means the Git does not track them. That is why these files are sometimes called untracked files.
Generally, programmers do not includes, ./bin folder or /
Git tracks The files that are in the staging area. Git analyse those files to check what has changed between the current commit and the last commit.
In this stage, the git version control system has all your commits and it knows what has been changed in the commit. It is the snapshot of what you changed in a commit.
How Staging area works?
The staging area is how git knows what will change between the current commit and the next commit. It has the last commits hash. So whenever new files are added or deleted or someone just edits a file, it immediately knows that something is changed. Because the new generated SHA1 (hash) is different.
Moving files In and Out of the staging area
As we are talking about staging area, some might ask how do we add or remove a file from the staging area. It’s quite simple!
- To add File in the next commit
- git add <file>
- To delete a file in the next commit
- git rm <file>
- To rename a file in the next commit
- git mv <file>
Git add -p
This is one of the tools when you’ve done too much work for one commit. It let’s you add files to the staging area interactively. Here a screenshot.
When you are removing a file from the staging area, it doesn’t mean it will get deleted. It just returned to working area. That’s all!