Pages

Monday, March 4, 2019

GIt - Working Tree, Staged ,Committed

Our original repository exists in a separate machine. A mirror of that repository exists in our local machine we call it as local repository. When ever we have completed our code we generally push the code from our local repository to the main repository. There are multiple things that happen before pushing our code from local to main repository.

Working Tree - Whenever we want to contribute to a project, we download the source code which is checking out the source code from the main repository. The source code once checked out to our local machine is called working tree. We modify the code that is available in this local repository.

Staging - Staging is an intermediate layer between working tree and local repository. Staging is a step before the commit process in git. We make changes to the source code and add them to this staging area. As long as the modified files are in staging area

git allows you to edit it as you like (replace staged files with other versions of staged files, remove changes from staging, etc.).

Why do we need stage?
The practical purpose of the staging is a logical separation of file commits. Let's say i have to make changes to 2 files. Now by the end of day, i was able to make the code changes to the first file and did a half change to the second file. If i did not have stage location and i commit changes directly to the local repository and push to main repo, i'm breaking the code because the second change is only half made.

In this case , i can push the changes to the stage location and then to the local repo. Once i push the local repo to main repo, it is not breaked as we pushed completed changes only.

Where does the staging area located?
The staging area is located inside the .git location. The index file in this location is a single, binary file which lists all files in the current branch including their checksums, time stamps and file names.

Commit - this is final step in our local repository. Whatever the code changes that we did we push it to the local repository and this operation is called as “commit”.

Where is the code pushed to local repository located?
The local repository is a hidden directory under the .git location which includes a objects directory. This contains all versions of every single file in the repo ( local branches and copies of the remote branches ) and are compressed as blob files.

The below image represents the process,
Local repository or working tree is where we make the source code changes. A file in the working tree of a git repository can have 4 states. These states are

Untracked - this file is not tracked by the git repository. This means the file is never staged or committed
Tracked - committed to the local repository
Staged - code is available in stage location
dirty/modified - the file is modified but the changes are not staged.
jagadishsample$Mon Feb 18@ git init
Initialized empty Git repository in /Volumes/Available/sample/.git/
jagadishsample$Mon Feb 18@ touch one two three
jagadishsample$Mon Feb 18@ git status
On branch master

No commits yet

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

    one
    three
    two

nothing added to commit but untracked files present (use "git add" to track)
jagadishsample$Mon Feb 18@

We can see that all files one, two , three are in untracked states. Now do a “git add one” command and we can see the file one moves to the tracked status showed under the status “changes to be committed”
jagadishsample$Mon Feb 18@ git status
On branch master

No commits yet

Changes to be committed:
 (use "git rm --cached ..." to unstage)

    new file:   one

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

    three
    two


Now lets edit the file one and see the output. The file one will be in both stage mode and in dirty mode as we made the changes.
jagadishsample$Mon Feb 18@ git status
On branch master

No commits yet

Changes to be committed:
 (use "git rm --cached ..." to unstage)

    new file:   one

Changes not staged for commit:
 (use "git add ..." to update what will be committed)
 (use "git checkout -- ..." to discard changes in working directory)

    modified:   one

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

    three
    two

Now once we do a git commit, we can see the file one that was staged before making changes is committed and changes that we did is not yet staged.
jagadishsample$Mon Feb 18@ git status
On branch master
Changes not staged for commit:
 (use "git add ..." to update what will be committed)
 (use "git checkout -- ..." to discard changes in working directory)

    modified:   one

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

    three
    two

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

Similarly as we use the git status to we have a short status available using “git status -s”. The out put of the git short status is different

File that are not tracked as shown using “??”. New files that have been added to the staging area have an “A”. modified file have an “M”. There are some times 2 column output - the left hand column indicates the status of the staging area and right column indicate the status of the working tree.
jagadishsample$Mon Feb 18@ git status -s
?? one
?? three
?? two

jagadishsample$Mon Feb 18@ git status -s
A  one
?? three
?? two

jagadishsample$Mon Feb 18@ git status -s
AM one
?? three
?? two

jagadishsample$Mon Feb 18@ git status -s
M one
MM three
?? two

If you see the one file is given with “AM” which means that file is in staging mode and also edited ( dirty ). In the last image, we see only “M” for one file which means that file is in working tree but not staged. The “MM” means that file is modified and staged.

No comments :

Post a Comment