Git Basic Concepts
Repository
Repositories in Git contain a collection of files of various different versions of a Project. These files are imported from the repository into the local server of the user for further updations and modifications in the content of the file. A VCS or the Version Control System is used to create these versions and store them in a specific place termed as a repository. The process of copying the content from an existing Git Repository with the help of various Git Tools is termed as cloning. Once the cloning process is done, the user gets the complete repository on his local machine.
Working Area or Staging of a Git Repository
A working tree in a Git Repository is the collection of files which are originated form a certain version of the repository. It helps in tracking the changes done by a specific user on one version of the repository. Whenever an operation is committed by the user, Git will look only for the files which are present in the working area, and not all the modified files. Only the files which are present in the working area are considered for commit operation. The user of the working tree gets to change the files by modifying existing files and removing or creating files. There are a few stages of a file in the working tree of a repository:
- Untracked: In this stage, the Git repository is unable to track the file, which means that the file is never staged nor it is committed.
- Tracked: When the Git repository tracks a file, which means the file is committed but is not staged in the working directory.
- Staged: In this stage, the file is ready to be committed and is placed in the staging area waiting for the next commit.
- Modified/Dirty: When the changes are made to the file i.e. the file is modified but the change is not yet staged.
After the changes are done in the working area, the user can either update these changes in the GIT repository or revert the changes.
Working with a Repository
A Git repository allows performing various operations on it to create different versions of a project. These operations include the addition of files, committing an action, deleting a file, etc. These modifications will result in the creation of different versions of a project.
Initializaing Git Repository
To initialize a new repo, you'll use the git init
command. git init
is a one-time command you use during the initial setup of a new repo.
Executing this command will create a new .git
subdirectory in your current working directory. This will also create a branch named master
.
mkdir my-repo # Create a new directory
cd my-repo # Change working director
git init # Initialize git repo or create repo
Adding to a Repository
After performing various modifications on a file in the Working Area, Git needs to follow two more steps to save these changes in the local repository.These steps are:
- Adding the changes to the Index (Staging Area)
- Committing the indexed changes into the repository
Adding changes to the Index
This process is done by the use of git add
command. When the changes have been made in the Working Tree/Area. These changes need to be added
to the Staging Area for further modification of the file. git add
command will stages file for the commit process.
$ git add <file-path>
Below is different ways to use add command:
Git Command | Description |
---|---|
git add file-name |
Add specific file to staging area |
git add --all |
To add all files of current directory to staging area. |
git add *.txt |
To add all text files of the current directory to staging area. |
git add docs/*.txt |
To add all text files of a particular directory(docs) to staging area. |
git add docs/ |
To add all files in a particular directory(docs) to staging area. |
$ git add "*.txt" |
To add text files of entire project to staging area. |
Committing changes from the Index
Committing process is done in the staging area on the files which are added to the Index after git add command is executed. This committing
process is done by the use of git commit
command. This command commits the staged changes to the local repository.
$ git commit -m "Add existing file"
This commit command is used to add any of the tracked files to staging area and commit them by providing a message to remember. Image below highlights the basic git flow.