Git: You Can Go Back in Time, Only with Git

Intermezzo

There is a group of students who are making a project. They were so new to this that they had no project experience at all. The project included here is a web-based project that will be used for public use.

They work on GitLab, based on suggestions from their seniors, to save the code that each student has worked on. One day, there was one student who made a fatal mistake, namely forgetting to change the branch and destroying the arrangement of code that they had created together. They were frustrated, seeing their frustration, their seniors just smiled.

smile from GIPHY

The senior said, it’s not something big, you can go back to the previous version of the code, that’s why it’s important for you to learn git before coding said the senior.

Start with WHY: Why using Git?

Why should we use Git? is it really important? the answer is yes and yes very important! For software developers, using Git is a game-like checkpoint that can save us when there are unwanted events such as push errors, merge errors, etc. Not only this, there are lots of benefits of Git to know before starting our work on software development. In a real world project, there will be many developers working in parallel. A version control is necessary to ensure that there are no code conflicts between each member participating in the repository

What is Git?

Git is a version control that functions to track every activity carried out in our repository, from changing file names to changing file contents. All activities, no matter how small the changes you make, will be recorded in Git. The advantage of Git is that we can work with many people in one repository. Git really supports collaboration. Where each person assigned to a repository will be able to see all activities carried out in the repository by other members.

How to use Git

On this occasion, to make it easier to imagine, I will explain how to use Git with an example of the implementation I used in my PPL group project. I will divide it into two points, namely the first point is about the Git Manual contains guidelines for using Git and Git Implementation in my group project PPL.

Git Manual

Command to make a new repository. Convert an existing, unversioned project to a Git repository or initialize a new, empty Git repository

Command for downloading existing source code from a remote repository (like Github, for example). This command is used only once per project. That project can be empty, or it can already have some files in it

I tried to clone my repo, so it move repo on cloud to local files in our computer. With git clone, you don’t have to do git init and git remote again to your repo, it automatically connect with your local computer

We need to use the git add command to include the changes of a file(s) into our next commit. We’ve made some changes in our working directory (our project), and we want to include these changes in our next commit

The git pull command is used to get updates from the remote repo. When you run git pull, it will automatically merge the branch you are pulling with the branch you are currently on.

Git push uploads your commits to the remote repository. You have to set remote name first with git remote add {name} {url}, so that Git will know what the remote is.

Git merge basically integrates your feature branch with all of its commits back to the dev (or master) branch. If there are conflicts when you merge, you’ll have to fix the conflicts first, remove the part you don’t need and leave the part that you need. The current branch will be updated to reflect the merge, but the target branch will be completely unaffected

source: https://www.atlassian.com/git/tutorials/using-branches/git-merge

An example implementation that we can do to merge is when a conflict occurs, as shown below

From the existing conflicts, we must choose which part we want to take, it can be one or both. Once selected, we can merge to the repo with the latest version which no longer conflicts

Rebase is another way to integrate changes from one branch to another. The goal is to maintain the linearity of that commit array.

source: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

If Rebase is executed, it will advance the initial branch position to the updated commit master as shown above so that if it is done the merging will use the Fast Forward Merge type. To rebase, then on your new branch, do the rebase command.

A safer way that we can undo our commits is by using git revert. When we want to return to some point in time (to some commit), but we want to be sure that no commit will be lost.

source: https://git.logikum.hu/tutorials/reset-checkout-revert/cl-revert

git revert should be used to undo changes on a public branch, you can also think of git revert as a tool for undoing committed changes.

We can revert commit with the commit hash id.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory.

source: https://www.maixuanviet.com/wp-content/uploads/2020/05/git-stash.png

With this stash you can safely not lose your files even if you haven’t committed them yet.

Manage the set of repositories (“remotes”) whose branches you track. At the begining of the project, we should set new remote. We can add many remotes, as long as they have different names (origin, upstream, etc.). If the remote name already exists, Git will throw an error.

We use git checkout mostly for switching from one branch to another. We can also use it for checking out files and commits.

We can use the git branch command for creating, listing and deleting branches. We want to see what branches do we have locally.

Git commit is like setting a checkpoint in the development process which you can go back to later if needed. We also need to write a short message to explain what we have developed or changed in the source code.

Git Implementation

The implementation of Git in working on my group projects really helped me collaborate with members of my team, especially during this pandemic, where we rarely get to meet face to face. There are several Git implementations that have been very useful in the course of our project, there is:

  1. Git Flow

Git Flow is a branching model and release management strategy for Git. It defines a well-defined pathway for the project development life cycle and ensures that the development team accepts to the process. It offers a set of extensions via Git to provide high-level repository operations. The Git Flow framework provides visibility to the developer to plan the workspace that was successfully addressed and what was achieved.

Source by Panduan Git PPL 2021

At Git Flow PPL 2021 there are several branches, namely:

  • Branch Master
    It is the main branch that stores source code ready to be deployed into a production environment.
  • Branch Staging
    Staging branch It is the main branch associated with the development process. This banch will store the source code of the work of each developer that is collected in one place.
  • Branch PBI[1..n]
    Is a branch (branch) for the implementation of a Product Backlog Item taken during a Sprint.
  • Branch Hotfix
    It is a branch of the master branch that is created if there is a bug or error in the master branch. The hotfix branch is used as a place to fix bugs (bug fixing) that occurred in the master branch.
  • Branch coldfix
    This is a branch created to rollback (remove all changes from all product backlog item branches).
Branch in our PPL Project 2021

On this project, has branch and commit naming rules. Commits can be divided into RED, CHORES, GREEN, REFACTOR. For merging, do a squash commit so that commits on the user story branch are only saved as one commit to make hotfixes easier.

[RED]: There is at least 1 commit that makes a unit test for each class and empty function / method that has been created, with the commit message tagged.

[GREEN]: There is at least 1 commit containing an implementation to pass unit testing.

[REFACTOR]: For every implementation that does improvements to previously created code.

[CHORES]: For implementations that are not related to functionality (such as adding assets in the form of images without changing code implementation)

2. Team Coding
Team coding contains broadly how a team with different backgrounds and abilities can come together and collaborate to achieve the same goals in accordance with their respective positions. An example of a simple implementation is to have an online discussion when a member of our team has a problem and is looking for a solution together.

Screenshot of Regular Meeting on Zoom

That’s all! Thanks for your time in reading this article!

To connect with me, you can connect with my LinkedIn

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store