Git: Developers Best?Friend
As a software developer, one of the hardest things to do is to work together in a team. In order to code together, the team must be able to control every persons work while still building up to the same target. This is very hard to achieve whether you are working in a small team of five or in a larger group. One such famous solution to this problem is git! Git enables developers to work together systematically in such a way that developers do not need to worry too much on the details.
So what is Git?
Git is simply a version control system (VCS) that enables developers to work together. The project that you have on your git is called a repository. Each repository will consist of several commits. A commit is the smallest part of a repository which are changes that are made to your source code.
Basic commands of Git
- To start a repository we can use the command git init
- After adding a few changes to our code, we may use git add <file-name> to add changed codes. To add all changes immediately we can use git add .
- Next, we use git commit to commit our changed codes. Commits may be given messages by using git commit -m “<messages>”
Remote Repository
When developing a software, often we will work with others online. Git hosts such as Gitlab or Github enables an online repository to be used by multiple users at once. The beauty of an online repository is that developers may now work together on an online system. To immediately clone an existing repository to our local work space, we can use the git clone <repository-url> command.
Git also enables us to add multiple online repository to be saved on our local repository. Use the git remote add <remote-name> <repository-url>. Some of the common remote names are origin and upstream.
Next, we would want to push our commits to be on our online repository so that others may have that code. We can use the git push <remote-name> <remote-branch> command to to this where the remote branch is the branch name where the default branch is named master (more on branches later). As the opposite, should we want to have codes on the online repository to be on our local repository we can use git pull <remote-name> <remote-branch>.
Branching: The Beauty of Git
As mentioned beforehand, there is a term called branch in git. What are branches? Branches are a feature where developers may change code independent of each other. To check on your existing branches use git branch command. You may also create other branches by using git checkout -b <new-branch-name>. To go to another branch that exists, you can use git checkout <branch-name>.
The best feature about branches is merging. As developers may develop code on different branches, we would want to sometimes combine our code. Git allows this by using the merging feature. To merge on an online repository, use the merge request feature and choose the branches you wish to merge.
Why is branch useful? At first-timers, you may think that this is hard or not that useful. But the impact of using branches is significant when you are coding on the same project together with your team. For example if you and your friend are about to implement two different features on the same project, the first person may code on branch A while the second will code on branch B. The two of you are working on separate directories, thus no conflict can be made. When both of you are done, each person will do a merge request to the master branch. By doing it this way, both of your codes will reach master branch without any conflicting codes on the same time! This is just a small example of the benefit of branches, but imagine if 100 people are doing different codes on 100 different branches. They can still reach the master branch together on the same time and this is where it gets very significant with git branches.
Advance Commands on Git
The commands I have mentioned above are not the only commands available on Git. There are plenty more commands available on git and I will discuss a few of them.
First is rebase. Rebasing is the process of combining a sequence of commits to a new base commit. It can be seen as changing the base of your branch from one commit to another, thus making it appear like you have created your branch from a different commit. It is very important to note that git accomplishes this by creating new commits. So even though your branch may look the same, it is actually made up from entirely new commits.
Next is revert. The revert command can be considered as an 'undo' command, however, it is not such a simple undo operation. Usual undo command will just remove your previous work, so on git that will be to remove your commit. This is dangerous! Instead of doing that, git will figure out how to invert changes and adds a new commit with the resulting inverse content. The benefit of this is that it prevents git from losing history, which is quite important for your team's revision history.
The most common case when you should use revert is when you're tracking down a bug and found out that it was because of a single commit. Instead of fixing it immediately and test until no bugs exists anymore, we can use git revert to automatically temporarily remove the bugs. After reverting, you can continue on taking care of that bug until it is fixed.
Lastly is stash. Git stash, as it names suggests will temporarily shelves all changes you've made so you can work on something else. Later, you can come back and rework on them. Stashing is quite handy if you need to quickly work on something else but you're midway and aren't quite ready to make a commit. This will be quite useful during development for example when a fast bug fix must be implemented.
There are still many more useful commands on git. You can try an explore them from many sources!
My Experience of Using Git during PPL 2020
During my time on developing my project for PPL this week, I had quite an interesting experience using git with my team. On this project development course, we are asked to create a few branches as seen on the image above. The master branch is of course the main branch for the project. The staging branch is the branch that has very similar environment with master branch. It has the deployment process and very similar behavior with master branch. The only difference is that in staging, real users aren't able to use them and that every new feature that wants to be released on master branch should be merged through staging.
The hotfix and coldfix branches are similar in a way that it has the ability to fix the master and staging branch respectively. It is used as a branch to fix problems on the master and staging branches as fast as possible. There are also PBI (Product Backlog Item) branches. These are the main branches where my team will be developing our features. Each backlog item will have its own branch. You may ask why this is necessary, but one of the main reasons is so that when we would develop an existing feature further everything that should be connected to that feature exists on that PBI branch. Note that every PBI branch should be merged to the staging branch and when it is done and tested on the staging environment it is then ready to be deployed to master (on this course we will deploy during our sprint review).
On the first week, I have been tasked to develop an image compressor feature. I am doing this specific task on a single branch (PBI5). While I am developing this feature, my teammates are also developing their own tasks. Since my team has two repositories (one for front end and one for back end), some of us are working on the FE repository while others are working on the BE repository. My task is currently being done on the BE branch.
Everyone of my team members have their own tasks and their own branches to work with. This is due to the fact that our tasks are independent from each other. It is also beneficial for our team because we are able to work on a lot of tasks on the same time, thus increasing the teams efficiency.
As a conclusion, the use of git and especially branches proves to be beneficial and efficient during my time on developing a software. It proves to be very helpful for developers with its variety of commands and capabilities. Thus the reason why git is a developers best friend.