Have you ever worked on a group project document and decided everyone works on a part, and then combine it at the end? Have you ever wondered on how that worked for programming? Well programming has its own system for collaborative programming called ‘Git’.
What is Git?
Git is a version control system originally developed in 2005 by Linus Torvalds. A version control system is a system that keep tracks of multiple versions of a software or code. Git is open source and actively maintained. It is used by a lot of software projects for version control, including commercial and open source project. Think of it as, go*gle docs but for programming.
How to use Git?
Using git is pretty simple. Here are some beginner commands that every programmer should know. All you need, is a terminal that has git installed and an internet connection.
The first command you should know is git clone. The way to use it is:
git clone <repository git link>
The git link can be obtained by opening a git repository and clicking the ‘clone’ button. And then click on the https link.
After that, open a terminal in the folder or directory you want the git project to be in.
In the screenshot above, I git cloned a project. After that there will be messages indicating the clone was successful and we can enter the directory.
In git, a version of a code is saved on “branches”. The main version is saved on the main branch or “master”. Other versions can be saved to other branches. The command to make a branch is:
git branch <branch name>
An example is as follows:
If it is successful, there will be no warning messages like the previous screenshot.
After you made a new branch, you first have to switch to that branch. The command is:
git checkout <branch name>
In the previous image, I switched to the new branch I just made. If successful there will be a message like in the image.
Git status is for finding out what branch and the condition of that branch.
In the screenshot above, the status indicates that the branch is up to date and there is nothing to commit.
In the screenshot above, the status indicates that there are files which has been changed and other file which haven’t been tracked by git.
In git, a file is not automatically tracked. In order for a file to be tracked by git, it must be added first.
The command is:
git add <file name>
To above command is for adding specific files or changes. Another alternative is:
git add .
This adds all changed and untracked files to git.
After a files has been tracked or added to git. It must be saved to git. The command to do that is:
After that, you will be asked to type a message to describe that commit.
an alternative is:
git commit -m"<message for commit>"
This is just for convenience so the command is a single line.
Here is an example:
In this screen shot. I committed some code with the message “[RED] Made test for attachment model”. So if another programmer where to check that commit, they would have an image of what that commit does.
After git commit, the changes you made were just saved to your local machine. They haven’t been saved to the remote repository. In order to do that, you have to push the changes to the remote repository. To do that, here is the following command:
But would most likely would happen when you push a branch that hasn’t been pushed to the remote repository is this:
So for a new branch, this command should work:
git push --set-upstream origin <branch name>
And something like this should happen:
There will be messages indicating that it was successful.
So there are a few commands to get started with git. Happy coding.