“the stupid content tracker” — Linus Torvalds, creator of Git
Linus Torvalds creates Git as a Version Control System with speed and efficiency ranging from small to big projects. But first, what is a Version Control System?
Version Control System
Version Control System (VCS) is a software tools that keeps track of every modification to the code. If mistakes happen, VCS can turn back and compare earlier versions of the code to compare and fix their mistakes.
The most used VCS right now is Git, and the reason is that Git is more widely distributed than other VCSs. GitHub and GitLab being the example, are the most popular code repository today, and its using Git as their base for version control and collaboration. In this article, I am gonna explain how to use and setup for the first time using Git.
Setup your Git Project
If you have existing project and never use Git before, you can initialize Git using
This command will make a hidden .git folder in the current project directory, which keeps track of code modification and others. But, if you wanna strat using someone source code from Gitlab or Github, you can use
git clone <link of the repository>
This command will “clone” someone source code and store it in your target directory.
Lets say, you wanna try something, but you did not want to break the source code. You can use git branch to make a new branch and edit the code on that branch without making changes to the source code on master branch.
If you wanna make a new branch, you can use
git checkout -b <branch name>
- Remove -b flag if you wanna move to existing branch
- Make sure you have committed your code before moving to other branch, use “git stash” if you do not want to commit your code first, i.e.
This command will make Git save your current uncommitted work on the stash list, so you can create new commit or move to other branch. If you wanna go back to the previous saved stash, you can use
git stash pop
This command will reapply your previous uncommit work to the branch and remove the stash from the stash list.
Commit your code version
If you are done with the code, and you wanna add it to the git branch, first we use
git add <file name> #use . for all modified files
After adding your file to git, we gonna commit the modification with
git commit -m “<your commit messsage>”
Make sure your commit message is informative so other developers know what changes you make to the code. If you wanna go back to previous versions, you can use
git revert HEAD
This command will revert back to previous versions if you made mistakes on the current version commit.
Merging with other branch
After you commit your code, you can upload it to GitLab or GitHub using
git push origin <branch name>
- Origin is the link of the repository you clone before
If you use “git init” before, we need to add link remote repository to git using
git remote add origin <link repository>
Also, you can download someone else branch using
git pull origin <branch name>
This command will merge your local committed code with downloaded branch code. Usually it is happening automatically, but sometimes you need to merge it manually if git telling you there’s conflict with the codes.
If you wanna merge your code to other local branches, you can use
git merge <target branch name>
But if you want to do it vice versa, like merge other branch to current branch, you can use
git rebase <from branch name>
Using our team project git as an example for PPL Course Faculty of Computer Science UI 2021
GitFlow dictates a strict branching that implement DevOps practices. It means that there is a specific branch naming and how to merge them together.
In our team, we implement GitFlow workflow, which means we have two main branches, main for the official release, staging for the development release.
Also, new features need to be on the separated branches, i.e. f_brian_component_dashboard branch for the dashboard component. After that branch is done, then its gonna be merge to PBI-2-dashboard branch and PBI-2-dashboard gonna be merge to staging with approval from other members and issues raised is solved.
If the team and the users rest assured that staging branch have no bugs with the Product Owner approval, then it can be merge to main branch to go to the production software state.
There is a lot of commands Git have, you can read their documentation if you wanna learn more about Git.
To sum up, Git is a powerful tool to keep track of your code modification and make developers easier to collaborate code with others.