If you’re totally new to Git, this guide will walk you through getting started with Git, knowing what it’s for, and some basic principles you’ll need to know.
What is Version Control ?
The method of storing and controlling changes to software code is known as version control, sometimes known as source control. Version control systems are software tools that aid software development teams in managing source code changes over time.
What is GIT ?
Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. Git’s goal is to maintain track of projects and files as they evolve and are manipulated by many users. Git uses a repository to keep track of the project’s progress. A repository contains project commits or heads, which are a collection of references to the commits. All of this information is kept in the same location as the project in a sub-folder called
Git maintains track of the modifications made by the people those who are working on a single project and then combines the code from separate part of the project into one. So , if anyone made a conflict, you can walk through the commits and find the code that caused that conflict. Mostly we can use GUI or command line to interact with GIT.
A working directory for a Git repository can be any directory or directory tree on your local system. Any number of sub directories can be found in a working directory, forming an overall workspace.
One of the ideas of Git that many new users have difficulty understanding is the staging area. It may appear at first look to be an unneeded intermediary level that obstructs attempts to promote material from the working directory to the local repository. It is critical to key aspects of Git’s operation.
The local repository is the last of the Git tiers that may be found on a user’s computer (the local environment). It’s time to commit the material to the local repository after it’s been produced or changed and staged. As previously stated, this repository is physically stored in a distinct (usually hidden) sub directory under the working directory’s root. It may be generated in one of two ways: by cloning (copying) a remote repository or by requesting Git to establish a new environment locally.
The remote repository is the Git level that hosts and provides material to a larger audience.It’s the location where numerous Git users may sync updates from their local repositories. In other source management systems, it correlates to what you may think of as the server.
For more information, visit — https://kelvinleong.github.io/git/2017/01/20/Use-Git.html
To use GIT, you must install it in your machine. Simply go to https://git-scm.com/downloads which is the official site to download GIT. In there, select the version based on your Operating System. GitHub is one of the cloud-based hosting service that lets you manage Git repositories and we also going to use GitHub for the demo. So you need to have an account in GitHub or simply create one.
Once you installed GIT you can use the following code in the Command line to check the version of GIT
Creating a Local Repository
Before creating a Local Repository, we need to initialize the Repository. So select a directory where you want to initialize and open CMD in the particular folder location and type the following code.
So I have created a folder called “Test” and initialized the git inside that folder as you can see in the picture below
So once I initialize, .git folder will be created inside the Test folder as follows
Adding new file to the Staging Area
For the demo, lets add a text file called “demo.txt” and inside that lets put “Hello World!”. So once you have created the file, we can check the status of the Repo by entering the following command in the CMD
So it will shows as follows,
as you can see from the above image, there is our text file which is in read color. which means it is still in the working directory. To add that to the staging area we need to use the following command
git add .
we can also specify by the file name and if we want to add entire files, we need to put full stop (
.) and now lets check it with the
git status command
as you can see it is in green color.
Adding new file to the Local Repo
Git commit gathers all changes in the Staging Area, bundles them up, and saves them to your Local Repository. A commit is essentially a checkpoint that instructs git to log all changes that have occurred up to this point by comparing them to our previous commit. Your Staging Area will be vacant when you commit. So lets see,
git commit -m "First Commit"
In here I used
git commit to save the from Staging Area to local repo. While using commit we have to give the message and this message should clearly specify the changes made to that particular commit. So once I did that I get the response as follows,
Branches allow you to move back and forth between ‘states’ of a project. Lets say you want add a new feature for the project, so if you create a new branch, you can work on the branch without disturbing the main branch and once you done with the task it can be merged with the main branch. Because most of the developers are making mistake at this point. In GitHub, the main branch name is
main . Recently, GitHub has decided to change the default branch upon creating a new Repo to
main. Usually it comes with the name of
git branch -M main
take a look at the code above, I am using
-M to rename the main branch from
But typing above code, it will show the entire branches. This is similar to
git branch --list . To create a new branch we have to specify the name of the branch at the end of the above code. once we give that list all the branches as follows,
The branch I have created is “feature-test” is showing in the above image. The current branch will be in green color. So what if I want to switch the branch, for that we have to use
checkout by specifying the branch name that I want to switch. lets see how,
as you can see from the above image I have entered
git checkout feature-test and now I am in ‘feature-test’ and it showing in green color when I gave the command
Save the files in the Remote Repository
Before saving we have create a new repo in GitHub and lets see how to do that,
Go to your GitHub page and click the plus icon (+) in the upper right corner of the page and select New repository as I showed in the image below
After that you have to specify a name for the Repo. In my case I am going to give the name as “TestRepo”. You can make the Repo Public or Private. Private means only you and the people who are collaborated with the Repo can view them.
Once it is done click on create repository and you will get the screen as below
So now we have successfully created our new repo in GitHub. Lets see how to save our files to this repo. Before that we need to specify the remote repo’s address to origin in our local repository.
git remote add origin https://github.com/Ramsunthar/TestRepo.git
your repository link will be on your github as follows
So we are adding the remote repo link to the origin by giving
git remote add command as I mentioned above. To confirm this you can go to your .git folder which is inside your local directory, there is a file called config and if you check that you will see like this,
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
url = https://github.com/Ramsunthar/TestRepo.git
fetch = +refs/heads/*:refs/remotes/origin/*
The remote URL that you gave will be stored inside the config file. So now we are good to go.
To store the files to the remote repo you have to use
git push by specifying the branch name as follows,
git push -u origin main
Once you give the above code you will get the output as follows,
Now go and check github and you will see your file as follows,
Retrieve files from Remote Repository
lets say you are working on a project with a team and they also push their code to the remote repo. Now you want get those code in the main branch. For that you need to use
git pull origin main
here we are pulling the code from the main branch and once we did that you will get the output in the CMD as follows,
And files will updated on your local repo as ,
Ta-da!!!!!!!!. This is very basic of git and github. But there are lot more to learn about these. To learn more about the git commands, visit https://www.atlassian.com/git/glossary.
Basic Git Commands | Atlassian Git Tutorial
These are the basic commands you need to get started with Git. Learn the most commonly used commands, then start using…