Showing posts with label GIT Repository. Show all posts
Showing posts with label GIT Repository. Show all posts

Thursday, 8 August 2019

HOW CREATE A LOCAL GIT REPOSITORY IN LINUX?

INTRODUCTION

Git is a version control system similar to how ‘track changes’ works in word processing programs. The main difference between git and these programs is that git can track changes to entire directories and not just a single file.
The typical workflow with a ‘track changes’ program involves emailing a draft to collaborators so that collaborators can track their changes before emailing it back to the original author. This may work well when the number of collaborators is very few but quickly becomes unstable and problematic in larger teams. Git can also be compared to cloud synchronization services like Dropbox but you get a more fine-tuned version controlled history to revert back to and the ability for collaborators to simultaneously edit the same file. Depending on how we use git, the amount of storage space is not as limited as in the case of cloud providers. Git is a distributed version control system and succeeds centralized version control systems like SVN & CVS. Centralized systems required all users to connect to a centralized server before editing a file. As a distributed system each user has all the code as well as the revision history on the computer they are working on. This not only serves as a backup layer since only one computer is needed to repopulate the backup files and revision history but also allows multiple users on multiple users on multiple computers to work independently.
In one of our previous articles, we’ve covered how to install the git version control system and also setup git initial configuration. In this article, we will demonstrate how we would create our first git repository on our system. We will be using a Centos 7 machine with git version 1.8 installed.

Step 1: Verify git is installed and check global parameters
Before we create a repository we must first check that git is actually installed on the system followed by which we will check that our initial parameters like username and email address have been populated. We’ll use the rpm command with the -q (query) option followed by the keyword git to verify that we have git installed on our system.
[sahil@linuxnix ~]$ rpm -q git
git-1.8.3.1-12.el7_4.x86_64
[sahil@linuxnix ~]$
Now that we have verified that git is indeed installed on the system that we are working with let’s now check if our global parameters have been populated. We display them using the ‘git config –global –list’ command as well as look inside the .gitconfig file in the users’ home directory.
[sahil@linuxnix ~]$ git config --global --list
user.name=Sahil Suri
user.email=sahil.suri@example.com
core.editor=vim
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ cat ~/.gitconfig
[user]
name = Sahil Suri
email = sahil.suri@example.com
[core]
editor = vim
Step 2: Create a directory named git
When we start working with git, it is a good practice to have a directory named git in your home directory. We’ll now create a directory named git within the users’ home directory.
[sahil@linuxnix ~]$ mkdir git
[sahil@linuxnix ~]$ cd git
[sahil@linuxnix git]$ pwd
/home/sahil/git
[sahil@linuxnix git]$ ls -ld .
drwxrwxr-x 2 sahil sahil 6 Mar 4 22:39 .
[sahil@linuxnix git]$
Now that we have created a directory named git, we’ll keep all our git related projects inside it.
Step 3: Create a folder for your repository
Within our git directory, we’ll create a directory named my_first_repo. This directory will be the placeholder for our first git repository.
[sahil@linuxnix git]$ mkdir my_first_repo
[sahil@linuxnix git]$ cd my_first_repo/
[sahil@linuxnix my_first_repo]$ pwd
/home/sahil/git/my_first_repo
[sahil@linuxnix my_first_repo]$ ls -ld .
drwxrwxr-x 2 sahil sahil 6 Mar 4 22:45 .
[sahil@linuxnix my_first_repo]$
Step 4: Create the git repository using ‘git init’ command
There are two ways via which we could work with git repositories. One way is to use a folder or directory that already has some content that we want to track and the other is initialize our repository within an empty folder and then proceed from there. For the purpose of this demonstration, we’ve created a new directory and will initialize or create our git repository inside it. The way we initialize or create a git repository within a directory is by executing the command ‘git init’ from within the directory.
[sahil@linuxnix my_first_repo]$ git init
Initialized empty Git repository in /home/sahil/git/my_first_repo/.git/
[sahil@linuxnix my_first_repo]$
The ‘git init’ command tells git that we want to track the changes made to the content that resides within this directory. This tracking will include any sub-directories that we create within this directory and the files that we create within those sub-directories. Therefore, we should be careful while running the ‘git init’ command and make sure that we are in the correct location before we execute the ‘git init’ command. We could run the ‘git init’ command within a sub-directory that resides within an already initialized git repository folder. This structure is referred to as a git sub-tree or git sub-module.
Executing the ‘git init’ commands creates a hidden .git directory within the current working directory.
[sahil@linuxnix my_first_repo]$ ls -a
. .. .git
[sahil@linuxnix my_first_repo]$
This .git directory will contain all the version control information that git will be using.
Step 5: Check the status of the repository
The command ‘git status’ displays the status of the git repository that we are in and is a command that git users make use of very frequently.
[sahil@linuxnix my_first_repo]$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
This gives us some information about the present state of the git repository we are in. We will be explaining the terms mentioned in the above output in greater detail in our future articles.

CONCLUSION

In this article, we provided some insight into why we should use git and how it’s more advantageous to use git over centralized version control systems. We then demonstrated a step by step procedure to create our first git repository.

Friday, 28 December 2018

GIT: The Pieces of Git

As far as the Git workflow is concerned, there are three pieces of Git that we should be aware of before moving forward with some slightly more complex (but totally doable!) explanation.
They are:
  • Repository
  • Index
  • Working Tree
Let’s cover each one in a bit more detail.

Repository

A repository is a collection of commits, and a record of what the project’s working tree looked like at one time. You can access the history of commits via the Git log.
There’s always a current starting point in a repository and that’s called the HEAD. A repository also contains tags and branches.
The job of the repository is to be the container that tracks the changes to your project files.

Working Tree

This is a directory on your file system that is associated with a repository.
You can think of this as the filesystem manifestation of the repository. It’s full of the files you edit, where you add new files, and from which you remove unneeded files. Any changes to the Working Tree are noted by the Index (see below), and show up as modified files.

Index

This is a middle area that sits between your Git repository and your Working Tree.
The Index keeps a list of the files Git is tracking and compares them to your Working Tree when you make changes. These changed files show up as modified before you bundle them up into a commit.
You might have heard this called the staging area where changes go before they are committed to the repository as commit objects.
If you ever use the -a flag when committing, then you are effectively bypassing the Index by turning your changes directly into a commit without staging them first.

Thursday, 27 December 2018

Friday, 21 September 2018

Create a Git Repository

This tutorial provides an overview of How to Set up a Repository under Git Version Control. This resource will walk you through for following topics:
  • Create a Git Repository for New Project
  • Create a Git Repository for Existing Project
But before moving forward, lets first understand about Git Repository.

What is a Git Repository?

The purpose of Git is to manage a project, or a set of files, as they change over time. Git stores this information in a data structure called a repositoryGit Repository is a virtual storage of your project where you keep all the resources/files of the project. It allows you to save versions of your code, which can be accessed, tracked and managed.

Create a Git Repository for New Project

Creating a new Git Repository is three set process:
  1. Create a New Project/Folder
  2. Browse to New Project
  3. Initialize Git Repository for the Project

Step 1 Create a New Project/Folder

1) Start the Command Prompt by typing cmd in the Windows Search.
Note: Same task can be done on GitBash, so if you wish to use the it, open the Git Bash.

2) Navigate to desired folder where you would like to create a new project. cd space folder_path is the command which is used to navigate to the folder. In this tutorial, I am creating a new project in C drive, so I used cd c:/
Note: If you wish to create a new project at deeper in C drive, command will be cd c:/folderName/folderName
Notice the Output:
Now execution is on C drive, earlier it was on c:/Users/Lenovo

3) Create a new directory/folder for the project. mkdir is the command to make a new directory
Note: In case you have space in the folder name, always use double quotes for folder name. Otherwise mkdir Git_Tutorial also can be used to create a folder with no space.

4) Folder with same name will be created under the mentioned path.

Step 2 Browse to New Project

1) As mentioned above to navigate, the command is cd folder path. We already are in C drive, so now command will be cd Git Tutorial.
Note: In case you have restarted the Command Prompt, the command to navigate to newly created project is cd c:/Git Tutorial.

Step 3 Initialize Git Repository for the Project

1) To create a New Git Repository for the project now you’ll use the git init command. The word init means initialize.  This command will create a Git Repository in the current directory. This is the one-time command you use during the initial setup of a new repository.
Note: Git does not care whether you start with an empty directory or if it contains already files.
Notice the Output:
Initialized empty Git repository in c:/Git Tutorial/.git/

2) Executing git init command will create a new .git sub directory in current working directory. This will also create a new master branch. 
Note: Every Git Repository is stored in the .git folder of the directory in which the Git repository has been created. This directory contains the complete history of the repository.

Create a Git Repository for Existing Project

There is no rocket science in creating git Repository for existing project, it is as same as creating a git repository for a new project with the only difference of step 1 below:
  1. Create a New Project/Folder
  2. Browse to Existing Project
  3. Initialize Git Repository for the Project