Let’s “Git” Started! : A guide to git

Image source : perforce.com

When you’re a computer science student like I am, the word “git” may sound familiar to you. It is a tool that’s frequently used by developer to ease their work. Git comes in handy when you’re working on a group project. By using git, we can easily track other collaborator’s work and combine them with ours. Not only that, we can also compare changes and see modifications made by others. So, what is git exactly?

Git is a version control tool that allows you to keep track of changes or versions of multiple files while collaborating with other people on a program.

  • Git Init

The first way to get started is to create a git repository. If you already have an existing directory, you can directly go to it and simply use the git init command. The command will make the existing directory be managed by the Git program.

git init

Next, connect your computer to a remote repository :

git remote add origin https://gitlab.com/audillaputri/test.git

This command will connect you to a remote repository and make an alias for it. From the example above, we’re telling git that we’re adding a remote repository that will be called “origin” from the URL “https://gitlab.com/audillaputri/test.git”.

To see all the remote repository connected to your computer, you can use the following command :

git remote -v
  • Git Clone

Another way to get started is to copy an existing remote repository. When you clone a repository, git will automatically name the remote repository as “origin”.

git clone https://gitlab.com/audillaputri/test.git#Clone a repository into a directory with a custom name
git clone https://gitlab.com/audillaputri/test.git custom-director

Once the set up is done, you can continue to work on your project.

After working on your code, you’d want to tell git that you need any changes on certain files to be tracked. To do that, we use the git add command :

#To track only one file
git add file1
#To track all files
git add .

Great! Now git knows that you care about this file. Whenever you want to check the status of your files, you can use the git status command :

git status

The command above will tell you informations such as which branch you’re on and whether there are untracked or modified files.

The next step is to store the changes by using the commit command. A commit can be seen as a checkpoint in which later can be useful when you want to see how the files looked like at some point of time. Thus, a commit message should be descriptive. While working on my PPL project, there is a guide on how to write a commit message :

  • [RED] is used when you want to commit a test that is destined to fail as there hasn’t been any implementation
  • [GREEN] is used when you have implemented the code for the test and expect it to pass
  • [REFACTOR] is used when you want to make a few changes in the code. Note that the changes shouldn’t make the passed test to fail.
  • [CHORES] is used when you want to make some changes that is unrelated to functionality such as resizing a picture, changing fonts, et cetera.

You can make a commit by entering :

git commit -m "Commit message"

Now let’s push the commits to a remote repository. To do that, you can type in:

git push origin master

The command above means you push the commits to a remote repository “origin” from a local branch named “master”.

“What if I made some mistakes? Now there’s an error! Can I turn back time?” I wish we could :( But don’t worry! Git has a command that will allow you to make a new commit that returns the previous commit. It doesn’t actually undo what’s done, but it will fix your problem. You can simply enter the git revert command.

git revert 

When you’re working with other people, there may be some changes made in the remote repository that you don’t have on your local computer. To make sure that you have the most recent changes, you need to pull the new version of the files. You can do this by using the git pull command :

git pull origin master

By default, git uses the local branch “master”, but you can add more branches if necessary. Branching is very useful when you’re doing a group project as it is the key that allows collaborators to work simultaneously. You can switch between branches and work on them without affecting the others. Lastly, you can merge the branches.

To create a new branch you can use the following command :

git branch new-branch

If you want to switch to other branches, you can enter:

git checkout other-branch# Create a new branch and immediately switch to it
git checkout -b new-branch

You can also see the list of branches available and which one you’re on :

$ git branchmaster
* new-branch
other-branch

The above example shows that there are three branches and you’re currently on the branch called “new-branch”.

If you want to delete a branch, you can simply use the following command :

# Delete a branch if it has been pushed
git branch -d "new-branch"
# Delete a branch that has never been pushed
git branch -D "new-branch"

Quick tip! When you are working on a project like I am now, it is important to name the branch descriptively too. Here’s how my team and I name the branches in our project.

  • Main will be the branch we use when all is set and ready to deploy.
  • Staging is a branch that combines all of the developers work. This branch is used to demonstrate the product in sprint review.
  • PBI-[1,..,n] is the branch used to store the product backlog items of a sprint.
  • Hotfix is a branch from the master branch that will be used to bug-fixing.
  • Coldfix will be used if the product owner rejects one or more PBI that has been implemented. In order to fix that, we need to do a rollback and we do it on this branch.

Lastly, this command is used to merge branches. The example below shows how to merge “new-branch” branch to the “master” branch.

$ git checkout master
$ git merge new-branch

Now you’re all set and ready to code!

This article is written for Universitas Indonesia Faculty of Computer Science PPL course’s individual review, but I hope this quick guide will be useful to anyone wanting to “git” started with git.

https://blog.udemy.com/git-tutorial-a-comprehensive-guide/

https://opensource.com/article/18/1/step-step-guide-git

csui 18