Git Local Repository Basic Concepts

Git local repository basic concepts

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have installed Git and created our 1st local repository using git init command.

https://cloudaffaire.com/how-to-install-git-in-aws-ec2-instance/

In this blog post, we will discuss some basic concepts of git local repository with examples.

Git Repository Basic Concepts Part 1

Git local repository basic concepts:

Git local repository can be divided into three areas, working tree, staging area (also known as index) and local repository.

Working Tree: Working tree is the area where your files reside and you do modifications to your files. The directory under which you initialize your git local repo by issuing git init command is your working tree.

Staging Area: Staging area in the intermediate state between your working tree and local repository. Once you do changes in your working tree, you add those changes to the staging area by issuing git add command.

Local Repository: Local repository is the area where your changes are committed. Once you stage your changes to the staging area, you need to commit those changes using git commit command which add your changes to your local repository.

Note: Git uses .git file to track your changes and commits.

Demo:

#login to the system where you have installed git and created your 1st repo 
#go inside mygit directory
cd mygit

#create a file named file1 (file will be created in working tree)
echo "name: debjeet" > file1 && echo "fav color: blue" >> file1 

#check git status > one untracked file found
git status

#add the file to git index (file will be added to staging area)
git add file1

#check git status > file is added to git index and is ready for commite
git status

#before commit you can add your user name and email to git config
git config --local user.name "Debjeet" && \
git config --local user.email "debjeet@cloudaffaire.com"

#view git config
git config --list #git config is stored in config file inside .git directory 
cat .git/config
#you can edit your git config file using git config --local --edit or vi .git/config

#commit the file (file will be added to local repository)
git commit -m "file1 added"

#check git log
git log

#check git status > file is consistent across working tree, staging area and local repo
git status

#edit file1 and chnage fav color to green(will be edited in working tree)
vi file1
------------
name: debjeet
fav color: green
------------
:wq

#check git status > changes are not added to staging area yet
git status

#get difference between staging area in working tree (blue is changed to green)
git diff 

#get difference between local repository and staging (no difference as we have not staged the changes yet)
git diff --staged

#stage the changes (now our working tree and staging area has same data)
git add file1

#check git status > changes are ready to get committed
git status

#difference between working tree and staging (since we already staged by git add, no diffrence)
git diff

#difference between staging area and local repository (not same as we have not commited yet)
git diff --staged

#commit the file (file will be added to local repository)
git commit -m "file1 edited"

#file will be consistent in all three space
git diff
git diff --staged
git status

#check the log
git log --oneline

#if you want to check what changed in local repository
git log -p

Hope you have enjoyed this article. In the next blog post, we will discuss how to roll back changes in git local repository.

To get more details on Git, please refer below git documentation

https://git-scm.com/doc

 

Leave a Reply

Close Menu