what is Git ?
Git is an Open Source Distributed Version Control System. Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer.
Why we need to git?
Real life projects generally have multiple developers working in parallel. So a version control system like Git is needed to ensure there are no code conflicts between the developers.
Additionally, the requirements in such projects change often. So a version control system allows developers to revert and go back to an older version of the code. Sometimes several projects which are being run in parallel involve the same codebase. In such a case, the concept of branching in Git is very important.
Let’s see how to use Git
Step 01 : Download Git
This link has details on how to install Git in multiple operating systems:
Verify if Git is installed by using the following command in the command prompt
git — version
Step 02 : Create a local Git repository
In your computer, create a folder for your project. Let’s call the project folder
Go into your project folder and add a local Git repository to the project using the following commands:
The git init command adds a local Git repository to the project.
Step 03 : Add a new file to the repo
Create a file called demo.txt in the project folder and add the following text into it : initial commit
Step 04 :Staging and Committing the code
Committing is the process in which the code is added to the local repository. Before committing the code, it has to be in the staging area. The staging area is there to keep track of all the files which are to be committed.
Use the following command for staging the file:
git add demo.txt
If you want to add all the files inside your project folder to the staging area, use the following command:
git add .
Use the following command to commit the file:
git commit -m ” commit message”
Enter a relevant commit message to indicate what code changes were done in that particular commit.
Use git status to find out information regarding what files are modified and what files are there in the staging area — it shows other information as well, which we can ignore for now.
Use the following command to see the status:
Create a new Branch in Local
Create a new branch called test using the following command:
git branch test
We are still in the context of the master branch. In order to switch to the test branch. use the following command:
git checkout test
you can list out all the branches using following command:
Now do some commit into new branch
modify demo.txt file and use the following command:
git add demo.txt
git commit -m”modify the txt file”
This commit was done in the Test Branch, and now Test Branch is ahead of Master Branch by 1 commit — as the test branch also includes the 2 commits from the master branch.
Currently, Test Branch is ahead of the Master by 1 commit. Let’s say that now we want all the code in the Test Branch to be brought back to the Master Branch. This is where git merge is very useful.
In order to merge the code from the test branch into the master branch, follow these steps:
First go back to the master branch:
git checkout master
then run the merge command:
git merge test
In order to push all the code from the local repository into the remote repository, use the following command:
git push -u origin master
This pushes the code from the master branch in the local repository to the master branch in the remote repository.
git pull is used to pull the latest changes from the remote repository into the local repository. The remote repository code is updated continuously by various developers, hence git pull is necessary:
git pull origin master
git clone is used to clone an existing remote repository into your computer. The command for this is:
git clone [repo url]