Introduction to GIT
Lets say that you are working on a final thesis report for your university. How would you normally name the files that you are saving. Most you all be saving final thesisV1, final thesisV2, final thesis final and so on. Now imagine if that final thesis is to be worked on by 3 or 4 people and everyone also starts naming their files in such manner. It would be very difficult to manage and find who did what and when they did it.
Just like in the above scenario we as a software engineers will be working on group projects most of the time so there must be a way to manage and control the workflow for these developers. That is where GIT comes in.
What is GIT?
According to the official side “Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.”
we know what speed and efficiency is, so lets look at the word that might confuse you all version control.
What is version control?
Version control is all about history. In GIT most of the time we will be working as a group. To finish the project the whole team will be working in a collaborative effort. So more than one may work on the same file. With the help of GIT we will be able to see the history of changes done by individual person. We can go and check the changes done by each and every member whenever they uploaded their file on GIT. With this it will be much easier to find defaults and fix errors. And if two people did changes to the same file GIT will give the option to MERGE those files as well.
Note : Before we jump into GIT I would also like to clarify a doubt that you might have, which is GIT and GITHUB are the same. This is not true. github is like a wrapper for GIT, there are multiple other services like Bitbucket, Gitlab which will provide the same remote services.
Now that we know about what GIT is and what version control means lets look into how to install GIT and the basic terminologies and commands used in it.
GIT Startup Guide
Installing GIT
First step before we do anything is to download git to your local system. Click here to go to the official GIT download page and select the installer you want according to the system you use.
Next you can launch the installer and just use the default settings to install. After installation is finished type the following command to see if it is installed properly and to see the version of your GIT.
git --version
Your output should look some thing like this
git version 2.31.1.windows.1
Now GIT is in your system. Lets see how to create a local repository and how to push it to remote.
Local repository
First step is to create a folder to work on. Then open the command prompt to that folder. After that enter the following command.
git init
the above command will initialize the current folder as a local repository. You can see this if you have your system to show hidden files. After entering the above command there should be a .git folder in your current working directory.
Note: .git folder will contain the config file and other files related to the workings of GIT. Do not meddle with the things in that folder.
Staging and adding files
Now in your folder you can add files that you are working with. For the tutorial lets say there are two files in that folder. A text file named math.txt and Hello.java.
after creating the above files enter the following command to see the status of your repo. (Repo is short term for repository)
On branch mainNo commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
Hello.java
math.txtnothing added to commit but untracked files present (use "git add" to track)
You will see an output like above. The status will return the current branch your in (we will see branches in the later section.) and details about commits and untracked files (these will be red colour).
Note : Keep in mind that working directory and local repo are not the same are and they mean two very different things. Just because there are files in the working directory it doesn’t mean that they are in the local repo.
Before we commit these files to the local repo we must have them right. That is where staging comes in. To add files to the stage we can use the following commands.
git add <filename> <filename> ... ---> Use this command to add one or multiple files to the staging area.git add *.<file extension> ---> Use this command to add all files with the specified extension.git add . ---> This command will add all the files in the working directory to the stage
The above mentioned commands can be used to add files to the staging area. For our example I used the last option. After adding the files check your status.
On branch mainNo commits yetChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: Hello.java
new file: math.txt
As you can see the output will say it can be committed (The files will be in green color.)
Adding files to local repo
Now that the files are in the staging are there is one last step needed to be done to add the files to the local repository. Enter the following command.
git commit -m "Your message"
In the above command in the your message section write about the commit your doing. If it is your first commit you will simple write as “First commit” . Then when you add changes to files, add new files and commit you will write the major changes done in the “Your message section”.
After the commit you will see an output like this.
[main (root-commit) 02ea5de] First commit
2 files changed, 8 insertions(+)
create mode 100644 Hello.java
create mode 100644 math.txt
Now the files are committed and are added to your local repository. Before pushing to the remote side we will talk a little about branches.
Branch
Now lets look at branches. Branches are sub folders within your main repository that you can do your work on without effecting the main branch(The repository). In more specifically branches are like sub components on a project. When a project is done by a group each person will be responsible for different tasks. Those persons will be working on and making changes to their own branches. Finally tech lead or the person who is in charge of the project will merge the sub branches with the main branch.
In GIT the root branch is knows as the main branch. It used to be ‘master’ but now it is called as the ‘main’ branch. If your main branch is named master you can now change it to main using the following command.
git branch -M main
You can then use the git status
command to check if the changes are saved.
Now to create your own branch you can use the following command.
git branch <branch name>
ex: git branch exam
After creating the branch type in the following command to see the list of branches that you have created.
git branch --listoutput:
exam
* main
After entering the command you will get the list of branches and the current working branch will be highlighted in green.
To move from one branch to another branch you can use the following command.
git checkout <branch name>
ex: git checkout exam
By typing the above command I will move from the main branch to the exam branch. I can confirm this by typing the git branch — list
command.
Saving to Remote
Now that we created the local repository now its time to learn how to push it to the remote site.
First go to your GitHub account and click on the + icon then select the create new repository option.
Then a new window will open up there you can give the name of your repository and set it to be either public (everyone can see) or private (only you can see). Then click on the create repository button.
I have given the name Test for my remote repo. You can give name according to your needs. After clicking the button a new page will open showing the basic commands to add your files to the local repo. (Commands that I have explained so far).
To add the files on your local repo to your remote repo you must first get the link to your remote repo. It can be found on the first page of your remote repo.
It would look something like the above. Now we must specify our local repo the location of our remote repo. For that type in the following command.
git remote add <your name> <remote link>
ex : git remote add origin https://github.com/ravikugan/Test.git
Note : You can give whatever name for <your name part> but for standardization people give origin as the name for the remote repo URL.
Now we have everything we need to push our local repo to the remote one. So finally type in the following command to push your local repo to remote.
git push -u origin main
Now the files in our local repository will be pushed to the remote repo. You can confirm this by going to your GITHUB repository page and refreshing it. If we are to push a specific branch we can specify the branch name instead of main.
Key Points
You all now know the basics of GIT and how to push a local repo to the remote one. Now there some other key things that you should know.
- Whenever working on a group projects, before you start coding always pull from the remote repo before you start working and then push to the remote repo after you are done with your work.
git pull -u origin main
- Keep tags for major version changes. (Tags are like system restore points.)Tags will take a screenshot of the whole system at the moment of tagging.(Click here to learn about tagging)
- Do not make changes to the main branch directly, if you are to make a change create a branch, specify the change and push it to that.
Now you all should have a basic understanding of GIT. This is just scratching the surface on the topic of GIT, so I hope everyone of you will keep learning.