As a developer, you may feel that any changes that you made on your project should be documented somewhere. Especially, when working in a team one must know who made what changes at which time etc.
Moreover, since many developers are working on the same project it may result in a conflict or there may be some project sync problems. Fortunately, a solution to these problems exists in the form of "Version Control System".
As the name suggests, A version control system is a software that has the responsibility to control/manage the versions i.e changes to the system.
One of the most popular version control system which is used all over the world is "GIT".
Git is a version control system that operates on different commands. In this article, we will discuss some of the important GIT commands one by one and will see how they can be used in your project.
So here we go.
GIT COMMANDS:
Before starting to talk about GIT commands, I will present an analogy that will help you to understand them well.
"Suppose you have clothes scattered on a bed. You have to put them in different baskets according to their category, then take the baskets to the nearest Locker and put the clothes from each basket into the locker".
1. GIT INIT:
Project on which git works is called a repository. To make your project a git repository you have to initialize git on it.
git init: This command will initialize your project as a git repository.
After you execute this command, A hidden folder named .git will be created in the project root folder. This folder will contain all the files required to make git work on the current project.
This command is like telling git:" Hey, Git initialize my project as a repository so you can start to track files in this project"
GIT will say: "Ok, let me create a git folder in your root folder to start tracking"
This git repository is also called " Git Working Directory"
2. GIT ADD:
We have told git to make our project as a git repository but we haven't told git about on which files the changes should to tracked. This git command is like putting clothes on the bed without being placed into the basket.
To tell git about which file changes should be tracked, we have to put them in what we called a "Staging Area". Any file on which changes needed to be tracked should be in a staging area. Think of a staging area as a bed where clothes needed to be placed.
Now To add files in the staging area, write this command: git add file-name
But if we want to track all files on a project instead of a single file then use this command: git add .
This command will tell git to add all files of the project into the staging area.
Now after executing the above command we have placed clothes on the bed. It's time to put clothes in their respective baskets like shirts into shirt basket etc.
3. GIT COMMIT:
Commit simple meaning is a change. Remember git goal is to track/revert changes? To do this we have to make sure a change is saved so we can revert it whenever we want.
So to do this we use this git command. It will create a copy of this change. This is like scattered clothes on bed but we haven't put any clothes in the basket yet.
Command: git commit -m "Something Changed"
After executing the above command, our change will be saved. This is like clothes on the bed which is being placed into their respective baskets.
4. GIT PUSH:
If a project is located on a remote server then we have to make sure our local changes should be sync with the remote directory. Here local changes mean all commits of a project as described above.
We have to push any changes made in the local directory to the remote directory. Here changes mean all commits of the project.
Before moving the commits to remote, we have to tell git about the location of the server.
To do this use the below command:
Command: git remote add origin https://github.com/Username/app.git
As the GitHub is the most popular git repository, we are using it as our remote location. But If your project lies on different hosting then you should be able to find the option of "Git Version Control" from your Cpanel where you can find the remote address of your project.
Now we have added the remote address, it's time to place the clothes from the basket to the locker i.e push project changes to remote.
Use the command: git push
4. GIT PULL:
To fetch the latest commits from the remote server use this command: git pull
Conclusion:
Still, confused? Well, there is a solution in the form of GUI for those who don't want to fight with git commands. The most popular one is the Github Desktop.
For those who prefer to write Git commands can download Git Bash.
I hope these basic concepts about the working of GIT commands will help you to better work with your team and to better manage your projects.
HAPPY CODING!
Thank you so much for such a nice explanation. Now i can start working on git
ReplyDelete