Friday 21 September 2018

GIT: Add and Track changes to Staging

In the previous tutorial of Create a Git Repository, we learned to create a new git repository for the project. Now that we have a Git project, it is the time to start working with it too. A Git project can be thought of as having three parts:
  • Working Directory: Local Repository, where you’ll be doing all the work like creating, editing, deleting and organizing project files
  • Staging Area: Tracked Local Repository, where you’ll list changes you make to the working directory
  • Repository: Remote Repository, where Git permanently stores changes as different versions of the project
This tutorial is all about working with local repository and tracked/untracked changes. We will be covering following git commands:
  • Git Add Command
  • Git Status Command
  • Git Remove Command
Before using the above commands, we should have some files in the git repository project. Download the following files and copy those in to the project folder. Each file contains the data of only two customers(Name, Email etc.)
Once done, the git repository will look like the below image:

Git Status Command

The git status is another must-know command that returns information about the current state of the repository. For e.g. list of files changed, list of tracked changes on staging, untracked changes on local and information about current branch & commits.
  • Staging Area: Git has the concept of a staging area, where all the updates/changes are tracked. This holds the changes which you would like to commit. Changes can be a single lines, files or parts of files.
  • Untracked Changes: Changes/files on local directory and which are not pushed to staging are known as untracked changes.
  • Tracked Changes: Changes/files on staging are known as tracked changes.

Lot of talk right! let’s move forward and use the command in command prompt.
1) Now, open the command prompt and navigate to the git repository, in our case it is at C:/Git Tutorial


2) Just type git status in the command prompt. This will now list which files are stagedunstaged, and untracked.

Notice the Output:
  1. branch master: This says that the working tree is master branch. We will learn more on branching in later tutorials.
  2. no commits yet: This gives information on the commits, as of now there is no commits yet.
  3. Untracked files: This says that Git sees the files but has not started tracking changes yet and marked these as red.
  4. Status messages: This gives relevant instructions for staging/unstaging files.


Git Add Command

The git add command adds a change in the working directory to the Staging Area. It tells Git that there are few updates in the project, which user wants to commit next. Thing here to note is that git add doesn’t affect the remote repository, as changes are not actually recorded until you perform git commit.

1) Let’s just start with adding a single file to stating. To use the git add command, just type git add filename. The word filename here refers to the name of the file you edited, such as CustomerData_IND.txt. Also use git status command to see what git has now to tell us about the state of the repository.

Notice the Output:
  • Changes to be comitted: This shows the information of tracked files kept on staging. This is the one, which we added using git add command.
  • Untracked files: This shows the information of untracked files. These are the ones, which brought to the project earlier but still not pushed to staging.


Git Remove Command

The git rm command removes tracked changes from the Staging Area. It tells Git that the updates which were pushed to staging earlier with git add command, are not ready to commit. So on running this command, git just removes the changes from the staging. But the changes still exists in Local Repository. If you look carefully the output of the above image section 1. In this git gives suggestion to the user that the tracked file on staging can be removed by using git rm –cached <file>.
To practice the command, let’s try removing the same CustomerData_IND.txt file, which was added earlier.

1) To remove the desired file, type git rm –cached CustomerData_IND.txt. Also use git status command to check the state of the repository.
Notice the Output
  • Untracked files: CustomerData_IND.txt file is back to untracked changes list. As git remove this file from staging tracked list.

Add different changed files to Staging

Above we just added a single file to staging, what if there will be multiple file to add. This can be easily achieved by using git add <file> <file>.
To add multiple file, type git add CustomerData_IND.txt CustomerData_UK.txt

Output: Mentioned two files are now added to staging.

Remove different files from Staging

The way you can add multiple files to staging, the same way multiple files can be removed from staging too. the command to remove the multiple files are git rm –cached <file> <file>.
To remove multiple file, type git rm –cached CustomerData_IND.txt CustomerData_UK.txt

Output: Mentioned two files in the command are now removed from staging and back to untracked list.

Add all the changed files to Staging

Above we added multiple files to staging, what if there will be many multiple files to add. This can be easily achieved by using git add *.
To add all the changed files, type git add *

Output: All the changed files are now moved to staging.

The same way all the files can be removed from staging and moved back to untracked list.

0 comments:

Post a Comment