Get To Know Git

Muhammad Fadhil Qorano W.
8 min readMar 21, 2021

Introduction to Version Control

The use of Version Control Systems (VCS) is a way to keep track of your project: from changes, updates, deletions, and more. It is meant to bring ease on group projects especially at a large scale, so you and your team can manage project modifications over time. Essentially, VCS helps multiple developers to work together on the same project. The more complex the development gets, the more requirement to manage multiple versions of the project.

Version Control Systems do more than just track your project. Using version control allows the team members to see each other works and also enabling them to share with one another. If you work on multiple devices (i.e. your personal computer and laptop), you could transfer the project you are working to one device and another. Another benefit from using version control is branching: a developer is able to make a copy from the master (main work) of the project and put corrections or updates as much as they want without disrupting the master or any of your teammate’s branch, and then to merge it again with the master.

What is Git?

git illustration. source: https://medium.com/@abhishekj/an-intro-to-git-and-github-1a0e2c7e3a2f

Git is a free and open source distributed version control system. Git is the most popular option amongst the others as it has become an industry standard for development. A distributed version control system means there are several remote repositories, so the team can work simultaneously within the same project. A repository stores information about the project’s progress. Changes made by group members will be tracked through the commits they made in the repository. The benefit of using Git is that it is designed to easily merge the code where members have worked on different parts into one, so when there is a bug, it can be tracked down by seeing through the different commits that have been done.

There are various commands with various purposes you can use in Git. To operate the commands the user must first download Git to their computer, and run Git Bash in the working directory folder. Here are some important Git commands to understand :

  • Git Init

To initialize a local repository, firstly the user must type this command inside the working directory folder.

git init 
  • Git Remote

This command is to manage the user’s remote repositories so it can connect the local repository to other repositories such as in GitLab.

git remote <command> <remote_name> <remote_URL>git remote add origin https://gitlab.com/aw_anowanggai/test-project.git git remote -v # list remote repositorygit remote remove origin # remove origin remote repository
  • Git Pull

Git Pull retrieves files from remote repositories to the user’s local repository. A developer uses this command if their teammate has made a commit to one of the branches in the remote.

git pull <remote_name><remote_branch>git pull origin master
git pull illustration. source: https://www.javatpoint.com/git-pull
  • Git Add

This command is used to add files from your local directory to your git repository. After doing this command, the files that have been added are not automatically stored into the git repository as other commands must also be done and the files are currently stored into the staging area.

git add <file name>git add . # add all files inside the directory folder
  • Git Status

Git Status is to know your status of what repository you are in now, and also files to add/commit if there are any.

git status
  • Git Commit

This command stores the file that has been added from the staging area to the local repository, followed by a comment that helps identify what you did, such as updating something, fixing a bug, refactoring codes etc.

git commit -m "insert comment here"
  • Git Push

This command sends the user’s local commit earlier to the remote repository such as GitLab.

git push <remote_name> <branch_name>
  • Git Log

Git Log shows the history of the user’s past commits so the user can keep track.

git log # show entire loggit log --<after/before/since/until>=<date> # show log with date parametergit log --<author>=”Author Name” # show log based on author
  • Git Branch

This command allows the user to create or delete a branch in their local repository, it also lets the user know on what branch they’re on.

git branch # identify what branchgit branch <branch_name> # create new branchgit branch -a # list all remote and local branchesgit branch -d <branch_name> # delete branch
  • Git Checkout

This command allows the user to switch between branches that have been created.

git checkout <branch_name> # checkout an existing branchgit checkout -b <new_branch_name> # checkout then create a new branch
  • Git Clone

Git clone is a command for downloading an existing source code from a remote repository to your local directory, including all the files, commits, and branches.

git clone <repository-link>
  • Git Merge

This command is used to merge the user’s changes with another branch.

git merge <branch_name>
git merge illustration. source : https://www.javatpoint.com/git-merge-and-merge-conflict
  • Git Rebase

This command is to combine the user works into a new branch.

git rebase <branch_name>git rebase --abort # undo the rebase
git rebase illustration, compared to merge and commit. source : https://dev.to/filizsenyuzluler/git-rebase-vs-merge-1ph0
  • Git RM

This command is used to delete files that have been committed earlier.

git rm -f <file_name>git rm -r -f <directory_name> # remove an entire directory
  • Git Diff

Git diff can enable the user to see the changes on the files they have modified (in this case the file has to be added first).

git diff
Before edit
After edit
  • Git Revert

Git revert can be considered as an safe method do undo changes done by a commit.

git revert <commit-number>

In this example, I have committed the README.MD file I have modified earlier. To undo this commit and revert the README.MD to its original form, I have to get the commit number from command git log and insert it along the git revert command.

README.MD file revert to its original form without ‘ADD’
  • Git Stash

Git stash will save the changes without having to commit, usually when a user is working on a feature that is not yet complete and want to switch branches. The user must just stash the changes.

git stash

Implementing Git Flow

Source: Panduan Git PPL 2021

Git Flow will be the main branching model that my team will rely on the group project, Logbook Elektronik. The platform that will be used is a version of GitLab that is provided by Computer Science UI. Because my team is required to continue last year’s project, the first thing that we did is to clone the repository made.

Each of us has been assigned to do their own tasks based on the earlier sprint planning. The tasks are divided into user stories (PBI). Each PBI branches stores the specific PBI provided for each group members.

The master branch is the main branch that stores the work of the project, and is ready to deploy. Meanwhile, the staging branch is the development branch that stores the work from every member. If we want to merge each of our works, we can merge request it into the staging branch.

The hotfix branch is a branch that originate from the master branch when there is a detected bug or error, so the user will use the hotfix branch to fix the bug. After it is done, the hotfix branch will be merged into the master branch.

The coldfix branch is a branch that is used to change or modify our PBI that has been merged into the staging branch.

Like previously stated, the benefit of using Git is its branching feature. Since it’s a DVCS (Distributed Version Control System), the branches are easy to merge and able to work with multiple repositories. Working on an own branch provides a isolated environment for every changes made to the project.

references:

  • Panduan Git PPL 2021
  • sources for each pictures have been included as well.

--

--