Starting Software Development Journey with Git

Oliver Muhammad Fadhlurrahman
Portelier
Published in
6 min readOct 8, 2020

Introduction

Creating software is usually done in teams, but how can we manage all our source codes? Every software development team needs something that can control and manage all members’ source code throughout the project’s lifetime. This is where version control system (VCS) comes into the picture. VCS is a category of software tools that helps developers manage and track changes to the source code.

What is Git?

There’s a lot of version control system available out there, such as Git, Mercurial, CVS, Monotone, and many others. Git is one of the most commonly used free open source version control tools. Git is a distributed version control system (DVCS), which means every developer has their own repository unlike in centralized VCS where every developer shares a single repository.

Having a clone of a repository in our local workspace gives lots of benefits. The most important thing is that we don’t have to access the remote server every time, thus almost all operations are very fast and don’t need an internet connection. Committing changes can also be done locally without changing the main repository and can be pushed all at once.

Git 101

Git has plenty of commands we can use to manage our source code. Here are some essential commands everyone starting Git should know.

Init

Short for initialization, this command is used to create a new empty local git repository.

git init

Remote

We use this command to manage remote repositories. One local git repository can have more than one remote repository.

View list of remote repositories:

git remote 
git remote -v
# With -v it'll show the url of each remote repositories

Adding a new remote repository:

git remote add <remote_name> <repo_url>

Renaming remote repository:

git remote rename <old_name> <new_name>

Removing remote repository:

git remote remove <remote_name>

Changing a remote repository’s URL:

git remote set-url <remote_name> <new_url>

Pull

This command will is used for updating the local repository by fetching from another repository. We can pull from remote repositories or even from another branch of our local repository.

git pull origin
git pull origin <branch_name>
# We can specify which branch we want to pull from

Push

If pull is about updating our local repository by fetching from another repository, then push is the opposite. Push will update a remote repository with our local repository.

git push <remote_name> <branch_name>

This command will push the changes we’ve made in our local repository to a remote repository specified by remote_name, specifically, the branch specified by branch_name.

Before doing any pushing, we also need to “add” and “commit” to save the changes we’ve done in the repository.

# Adding one specific file
git add <file_name>
# Adding all file with changes
git add .
# Cancel addition of files
git rm <file_name>

After adding the changed files we need to save it with commit:

git commit -m "commit message"

We can also check the status of all file changes:

git status

Clone

This command will clone/copy a remote repository into a new local repository. It’ll also create a new remote called origin that points to the branch where the clone is performed from.

git clone <repo_url>

Merge

By doing merge, we can join two or more development histories into one. For example, we can merge changes from another branch to the branch we’re currently working on.

git merge <branch_name>

Merge two other branches with the current one:

git merge <branch_name_1> <branch_name_2>

Rebase

Similar to merge, rebase is also used for integrating changes from one branch to another. The difference is that merge only make changes to the destination branch. But by doingrebase, it’ll move all commit histories from the source branch to the destination branch. merge also adds a new commit to the destination branch, unlike rebase where it’ll rewrite project history by creating new commits for each commit in the specified source branch.

This following command will move the entirety of the current branch to the specified branch:

git rebase <branch_name>

Reset

We can use reset when there’s a mistake in the commit we’ve done in our local repository.

# Cancel last commit and preserve the changes
git reset --soft HEAD^
# Cancel last commit and remove all changes in that commit
git reset --hard HEAD^

Revert

Like reset, this command is used to go back from existing commits or changes. The difference is that revert is used for undoing commits that have been pushed, as it creates a new commit and doesn’t rewrite the commit history.

We can revert changes from a specific commit with the unique hash that identifies the commit:

git revert <commit_hash>

In default, revert will create a new commit that records the revert process. If we want, a specific command can be used so that it won’t create a new commit:

git revert --no-commit <commit_hash>

Besides choosing specific commits to revert by its hash, we can also revert by specifying the nth last commit in HEAD. For example, we want to revert changes specified by the fourth last commit in head:

git revert HEAD~3

Stash

If we want to change branch but there are files that haven’t been committed, we can use stash to store our uncommitted changes and return the state of the working directory to the latest commit.

Stash changes:

git stash

View list of stash:

git stash list

Inspect a stash:

git stash <stash_name>

Applying a stash on top of the current working directory:

# Standard apply without removing stash
git stash apply <stash_name>
# Apply stash and remove it from stash list
git stash pop <stash_name>

Removing stash:

# Remove a specific stash
git stash drop <stash_name>
# Remove all stashes
git stash clear

Checkout

This command is usually used to switch branches.

# Creating new branch and move to it
git checkout -b <new_branch_name>
# Move to an existing branch
git checkout <branch_name>

Branch

We can use this command to manage branches, such as listing all branches, creating, deleting, and many more. Here are some examples.

# List all branches
git branch --list
# Create new branch
git branch <new_branch_name>
# Deleting a branch
git branch --delete <branch_name>

Git Implementation in Software Engineering Project Course

I will now tell how Git is implemented in my group project for Software Engineering Project Course of Faculty of Computer Science, University of Indonesia.

For the platform of our project repository, we use GitLab hosted by the Faculty of Computer Science. We have a git workflow that is provided by the course, here’s the illustration.

As of the time of writing, my group is still working on the user story branches of the first sprint to work on our own given task. I’ve had my exposure to Git before doing this course, thus I’ve learned some basic knowledge and commands of it.

Connecting all of the Git I’ve done in this course so far with the essential Git commands I explained in the previous section, I usually use git remote add and then git pull instead of just using git clone. It basically translates to the same thing, I guess it’s just because I’m used to using that from the start. For integrating changes from one branch to another, I’ve only used merge. Rebase is one of the commands I’ve never used before. Another command I’ve never used so far is stash.

For revert, I’ve used it a couple of times, including during this course’s project. Sometimes there’s changes that I’ve committed and then I realized that the commit isn’t exactly done, so that I have to revert it. Finally about creating a new branch. I personally haven’t used git branch to create a new branch, usually I just use git checkout -b to create a new one and then immediately switch to that branch.

--

--