- HubPages»
- Technology»
- Computers & Software»
- Computer Software
Git Essentials Part 3
Two-Tree Architecture
On the figure above, we have a repository and working files and those are what we call two (2) trees.
We call them trees because they represent a file structure.
Our working copies begin with the project directory (working) and below are some folders that has another or more folder on it and so on. If you can image to map it out, each of those folders will branch out like the branches of a tree.
On the repository, we checkout the files on the repository on our working directory and when we finish some changes, we commit those changes back to the repository.
Three-Tree Architecture
Git uses the three-tree architecture, it still has the repository and working but in between there is tree called staging index.
Staging Index means the preparation or staging of files before we commit it.
Remember on how we commit the FirstGitProject on our repository, we must add it first by using the command git add . or git add file_name, the command have added our file to the staging index and then we commit from the repository by using the command git commit -m "commit_message".
Git Workflow
Now, we will see work flow on using the three-three architecture by giving an illustrative example:
- New File - We know that Git is working with different files used to keep track of changes, for now we will just work on a single file for simpler illustration.
On the given figure above, we have the three-tree's which is repository, staging index, and working. We will add a file named file.txt and named it A as its reference.
Do you used Git
Adding Files
Now that we have a better understanding on git concept and architecture we will now we will revisit again on our first commit.
The command git status, used to report back the difference of the working directory, the staging index, and the repository.
Notice that it replay nothing to commit, because we don't add a file on the staging index.
Now lets try to add new file on our working directory.
Now we will used the git status command and see what happens
Now git told us that there are untracked files present which means if you make some changes, git can't give information about the new files unless you committed it to the staging index and to the repository.
Now we will add the file SecondFile.txt to our staging index by using the command git add SecondFile.txt then followed by the command git status.
On the Changes to be committed: that is the staging index and the green on specify the file that we add on the staging index and the Untracked files that are not added to the staging index.
Now we will try to commit the files on staging index by using the command git commit -m ""
Now that we successfully commit the file SecondFile.txt, the only difference when we use the git status command is that it doesn’t show the staging index and it only shows the untrack files.
Now used the command git log to view committed files on our repository.
Now we will add the untrack file to the repository.
The three (3) files is now added to the repository
Editing Files
Now we will edit a file and see how git works on those edits.
First, lets open our working directory and we will edit the FirstFile.txt.
Now that the updated file is updated on our working directory, let check the status on git bash.
Git have notify as that the FirstFile.txt that we edit is now classified as modify. This is different from untracked file.
Now we will add the firstfile.txt on the staging index.
Now that the file Firstfile.txt is added to our staging directory, let make some changes on hour working directory by editing the SecondFile.txt and ThridFile.txt and lets check the status.
Git have showed us the file add on the staging index and changes mage on the working directory. Now we add the SecondFile.txt to the staging index.
Now that the SecondFIle.txt is on the staging directory, we will commit the files on the staging directory on our repository and see what happen
Now that git have taken the snapshoot of the 2 files we committed on the repository, and git also shows us the modified. Now we will add the last file to the staging index and commit it to the repository.
All changes on our working directory is now committed and track on our repository.
Viewing Changes (Working Directory and Repository) with diff
In order to see the changes that will be made on the repository based from the working directory file. Git has a command that determine the difference or changes on the content of a file from the working directory to the repository.
To begin, we will edit the FirstFile.txt on out working directory
Now if we will used the command git status, git will tell us that the FirstFile.txt was modified.
In order to see the modification or changes on the content of the file, we will used the command git diff for all changes or git diff name_of_file single file changes.
The --- a/FirstFile.txt is the file at the repository, the +++ b/FirstFile.txt is the updated file on our working directory.
The Forecolor red represent the file at the repository, notice that is the file we committed on the previous discussion.
The Forecolor green represent the changes made on the file.
Viewing Changes (Staging Index and Repository) with diff
In order to see the changes that will be made on the repository based from the staging index file or added files. Git has a command that can determine the difference or changes on the content of a file.
To begin, we will add the FirstFile.txt to the staging index
Now that the FirstFile.txt is added to the directory, we will used the command git diff --staged(for new version) or git diff --cached(for old version)to check the difference of changes from the staging index to the repository.
The --- a/FirstFile.txt is the file at the repository, the +++ b/FirstFile.txt is the updated file added on our staging index.
Deleting Files
In this part, we will now delete file and track all delete files in git.
Now we will create two (2) new file named DeleteFile1.txt and DeleteFile2.txt then commit then on our repository.
To delete files on the repository, we will used the command git rm name_if_file.
After we enter the command, git return rm 'DeleteFile1.txt' this means that git have remove the file on the repository and then added it to the staging index.
Now we will commit these changes on the repository.
Note: I've mistype the "Remove Second File to be deleted" it should be "Remove First File to be deleted".
Move and Remaining Files
In order to rename and move files on git, we will used the command git mv current_file_name new_file_name.
On the figure above we rename the file FirstFile.txt to PrimaryFile.txt.
Now to move the file to other directory on our repository, we will used the same command but first we will create a new folder named FirstDirectory on our working directory.
Now we will move the file ThirdFile.txt to the FirstDirectory folder
Now we have successfully move the file ThridFile.txt on the FirstDirectory folder.
After renaming and moving files on the repository, we will now commit these changes on git.