GIT is a repository (the place where the project is stored) and it’s an open-source software used for version control
What is Version Control?
The code which is stored in the GIT repository will undergo many code changes whether by a single developer or multiple developers. So Version control help in handling this by maintaining a history of what code changes have happened.
Why Version Control is needed for a Developer?
Real-life projects will have multiple developers working parallel. The version control system like Git is responsible to avoid or ensure that there are no code conflicts between developers and also developers can go back to the older version.
Let’s get started with GIT
Download GIT Setup
Download Git for OSX
Download Git for Windows
Download Git for Linux
After Downloading and Installing GIT in your local machine check for the GIT version
Command to check GIT version
Now create a project folder in your local directory
Once you created the local directory perform git init command to create a new git repository
Now will create a text document example: Demo.txt
Add a simple text like “Hello GIT”
Now your local repo consists of three trees maintained by GIT
Working Directory – will have actual files
Index – acts as staging are
Head – points to the last commit
What is the Staging area in GIT?
The staging area is used to keep track of all the file which are to be committed.
Command for staging single file
The command for multiple file staging:
Command to add all the file inside the folder to staging area
What is the Commit process in GIT?
Commit operation is used when you commit your code, the code will be added to the local repository.
Before you commit the code, it should be in the staging area. If the file/code is not in staging area you can’t perform commit operation.
Command to commit:
Now modify the text file
GIT Status –used check the status like what file is modified and what file are in staging
The status shows that demo.txt is modified and is not yet in the staging area.
Now let us add demo.txt to the staging area and commit it using the following commands:
Use to display all the commits which are done.
Next will look on to GIT Branches
What is Branch in git?
Its a pointer to the latest commit.
Branches are used to develop features isolated from each other.
the master branch is the “default” branch when you create a repository.
Use other branches for development & merge them to the master branch.
Why we need multiple branches?
It support multiple parallel developments.
Until now we didn’t create any branch in GIT. So all the commit will go to master branch.
How to create Branch in GIT?
the command to create a new branch with name “dev1”
the command to switch to dev1 branch from master branch
the command to list out all branches
Let we do some new commits in the new branch
Verify GIT commit using “git log”
To move the commit code from all the branch to master branch git merge is used
Command to merge the branch code to Master branch
First, go back to Master Branch
Remote Git Repository
Till now, we have been working only in the local repository. But in real life project each developer will work in their local repository, they will push the code into a remote repository.
Once the code is in the remote repository, other developers can see and modify that code.
Command to Connect to Remote Repo from Local Repo
Command to Push all the code from Local Repo to Remote Repo
Command to Pull the latest commit from Remote Repo to Local Repo
Command to Clone Remote Repo in Local Repo