ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Git Essentials Part 3

Updated on March 28, 2015

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:

  1. 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

See results

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.

Does Git command is useful?

See results

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)