Git: Powerful Tool

Hana F. Ardiansyah
5 min readMar 18, 2021

--

source: https://www.piggyride.com/

I am currently pursuing my third year of Bachelor of Computer Science at University of Indonesia. It has become commonplace for third-year students to take a PPL course. PPL is different from other courses since we can develop our new skills and understandings through developing software projects so we can implement them in real life. We collaboratively do this project with our teammates through ✨Git✨.

Git is a version control system in a software project created by Linus Torvalds for recording or tracking any changes to the project files done by many people or by themselves.

Why do we need version control?

Although it is clear that project development teams need to have advanced code management systems to keep an eye on bugs and avoid conflicts. But with Git, you can do a lot more because Git is such a powerful tool.

Here’s a Git essential command.

  • git config: used to set the author name and email address respectively to be used with your commits.
  • git init: used to create a git repository from the directory that has been created
  • git clone: used to create a working copy of a repository
  • git add: used to add one or more to the staging area.
  • git commit: used to record the file the version history.
  • git status: used to view files that you haven’t added or committed yet.
  • git rm: used to remove the file from your working directory.
  • git branch: used to show all local branches in the current repository.
  • git checkout: used to switch from one branch to the specified branch.
  • git merge: used to merge another branch to the current branch.
  • git remote: used to connect your local repository to the remote server.
  • git push: used to send changes to the remote repository.
  • git pull: used to update your local repository to the latest commit.
  • git revert: used to undo changes made in the last commit.
  • git diff: used to compare changes.
  • git reset: used to undo local changes to the state of a Git repo.
  • git stash: used to record the working directory's current state and the index, but want to go back to a clean working directory.
  • git rebase: used to merge branches (commit to branches to be merged one by one in sequence).

Here’s a simple guide to getting started with Git.

SETUP

Create a git repository from the directory that has been created.

git init

Use git clone if you want to make a copy of an existing repository.

git clone https://gitlab.cs.ui.ac.id/ppl-fasilkom-ui/2021/CC/igri.git #create a working copy of a local repositorygit clone username@host:/path/to/repository #create a working copy of a remote server

Set up a virtual environment

python -m venv env

For Unix users:

. env/bin/activate

For Windows users:

.\venv\Scripts\activate

Install requirements

pip install -r requirements.txt

WORKFLOW

The local repository consists of three “trees”.

  • Working Directory: stores the actual files.
  • Index (Stage): staging area.
  • HEAD: points to the last commit.

ADD & COMMIT

Add your changes to the stage.

git add [file_name] #add a file the staging area.
git add * #add one or more to the staging area.

Commit changes and record your file permanently.

git commit -m “[type in the commit message]"git commit -m "[RED] membuat test_melihat_daftar_pembinaan"

*good commit messages matter.

Great! The file is committed to HEAD.

To see files that you haven’t committed yet, run:

git status

PUSHING CHANGES

Now, send changes to the remote repository.

git push origin [branch_name]git push origin PBI-40-operasional_pembinaan_melihat_daftar_pembinaan

*change [branch name] to the branch you want to push your changes to.

Congratulations! Those changes are sent to your remote repository.

If you have not copied an existing repository, connect your repository to the remote server.

git remote add origin [server]

BRANCHING

Create a new branch and switch it to that branch.

git checkout -b [new branch] git checkout -b PBI-40-operasional_pembinaan_melihat_daftar_pembinaan

Switch from one branch to the specified branch.

git checkout [branch] git checkout PBI-40-operasional_pembinaan_melihat_daftar_pembinaan

Delete the branch.

git branch -d [branch]git branch -d PBI-40-operasional_pembinaan_melihat_daftar_pembinaan

UPDATE & MERGE

Update your local repository to the latest commits.

git pull

Merge one branch with another branch into one.

git merge [branch]

So, what is the difference between git merge and rebase?

Suppose that there are three commits (A, B, C), then you made a commit D, and the other developer made a commit E:

Commits
  • Merge: When the two branches are merged, it will create one new commit (M). This new commit contains merge notes. Use merge if you are working in a repository with very few branches(only 2–3 branches).
Merge
  • Rebase: When the two branches are merged, it won’t create a new commit. (We get rid of the E commit, as it never was (denoted by vanishing dots)). We create an R commit, whose actual file contents are identical to the commit concatenation in M. Use rebase if the repository will have many branches.
Rebase

Here’s our Git repository.

In this PPL project, our group is using a pattern called Git Flow. There are several branches in our repository i.e,

Our Branches

Branch master is the main branch that stores source code that is ready to deploy into production and ready to be used by the user. Branch staging is the main branch associated with the development process. Branch PBI[1..n] is a branch for the implementation of a Product Backlog Item (PBI).

I was also helped by the board feature. I got a frontend role so of course, frontend and backend had to “talk to each other”. With this feature, I can track all my back-end partner's progress and it makes it easier for me.

Our Board

Conclusion

From implementing Git flow, I learned that branching and merging are powerful for integrating changes without sacrificing other work. Git branches are also cheap and easy to integrate. This can facilitate the developer’s workflow. Additionally, Git provides an isolated environment for any changes to our code.

References

https://git-scm.com/docs

https://qastack.id/programming/16666089/whats-the-difference-between-git-merge-and-git-rebase

--

--