I hope the ones who are reading this might have just started their career as a developer and suddenly you guys got to know something called Git. In this article, we are going to learn the fundamentals of git which every developer should know.
Git is a (VCS) version control system that is used for tracking the file changes done by any one of the developers in the team and keeping the different versions of our project with it.
Why do we need git? what problems does it solve?
Imagine you are a team of 5 developers and you guys are working on a project. Now you want to add a new feature to your project
Steps which you will follow :
- You will ask for the latest copy of the project from co-developers because you always will want to make changes to the latest code base and copy it to your local.
- Whenever you will make code changes you will need to remember where you have added what because potentially the code which you have written might be having some bug that can affect the whole codebase.
- Copying code over and over will fill up your disk space in the computer.
What git does is it keeps track of all the changes which you have done and you can also revert back to the previous version of your code anytime and that solves problems for developers as they don’t need to keep a copy of the project and it’s easy To maintain.
Now as you got some idea about git, now we will see how you can get comfortable using it.
1. git init
git init command is used for initializing a repository as a git repository. A local repository on your computer might be tracked by git or untracked by git. If you want your project to be tracked by git then you can move into the project directory and run this command that will create a .git hidden folder inside your project which means now git knows about all the changes that you will do in your project.
2. git clone
Another command you would use more often will be git clone. As the name suggests this command is used to clone ( copy ) a remote repository onto your local computer. The remote repository of the project may be hosted on Github, GitLab, etc.
When you go to the remote repository you will find two options as HTTPS and SSH. You can use one of them but SSH is more secure as compared to HTTPS which is recommended if your project is a private one.
before learning more commands we need to learn about some concepts about git that are really important.
When we work on a directory, our work is divided into something called a working directory, staging area, and changed repository with committed changes.
Working Directory: A file will be treated as a working directory until you did not add it to the staging area.
Staging Area: It's a place where you add your files for some time that you want to commit in a future commit.
Committed: The files which are placed in the staging area previously are committed ( Simply means that now these file changes are permanent and when you push changes these changes will be shown in the remote repo.
3. git status
Tracked Files: These files are under version control and git knows about them. These files are the files that were in the last commit ( Snapshot ) or were in the staging area.
Untracked Files: Git does not know about these files and their files are never added into a staging area or committed because we generally want that git does not to track them.
This command shows you the current status of your working directory with respect to all committed, newly added, or staged files.
Story to learn ( git add, git commit and git push )
These are the most important commands which you will use day in and day out so, I will try to explain them in a form of the story so that you don't forget.
Suppose your friend having his wedding and you were given a task to manage the guests. You are also given the responsibility to work with the photographer so that you get all photos of guests who were at the wedding.
Assume the stage of the wedding hall to be the working directory of your project like consisting of many files and folders same as guests.
- Compare stage with the staging area in git
Now you want to take pictures of the first 5 guests, you would tell them to go to the stage.
git add command is used to add files in the staging area ( same like you sent the first 5 guests on stage ).
But wait, your job is to also take their pictures so that we would know who all were present at the wedding, so you would tell the photographer to take pictures.
git commit command is used to commit the files which are in the staging area while committing ( when you take picture of guests present on stage it means you can go back to that picture any time and look how was your stage looking at that moment )
git commit command takes a snapshot of the project at that moment so that if by any chance you want to know how your project was looking at (wed 22, 2021 3.04 pm) . You can revert back to the same commit.
Now comes the main step, Yes you managed the guests well and also took their photos but now your friend is asking to send those photos to relatives who were not present at the wedding.
Somebody who was not present at the wedding would never know how the stage was decorated or who all were present.
Similarly, if you do changes to a project and also commit those changes but did not push any changes to a remote server then other developers would never know the changes you did RIGHT ?
git push is a command which is used to push your commits to the remote server so that other developers would know the file changes which you did.
4. git add
// add one file at a time
git add < file name with extension >
// add all files at once
git add .
5. git commit
// -m flag is for adding the message for the commit
git commit -m “ Your commit message “
6. git push
// command for pushing the code
git push origin<branch_name >
where origin points to a remote repo URL
7. git pull ( Fetch + Merge )
This is just the opposite of the git push command, git pull command is used to pull out the code from the remote to the local repo.
Suppose your friend pushed the code to the master branch, now until you pull the code you cant see changes made by him
// to pull the code from the master branch
git pull origin master
These are the most important commands which you need to know as a beginner, We will learn more about git in further articles in which I will cover the topics such as branches, merging, stash, and many more.
Thanks for Reading !!