Git-ting Started with Git

Introduction

Arguably one of, if not, the most popular development tools there is. It helps users manage their files. Git allows users to track changes on files and can even go back to a certain version of a file (revert). Files on a specific project can also be changed by multiple members (if they have the authority). This makes git useful for group projects. In short, it is a place to freely maintain files/codes.

Definition

According to git’s official website:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

If you’re new to git, you’re probably unfamiliar with the term “distributed version control system”. It is a subset of a version control system. And what exactly is a version control system?

According to git’s official website:

A version control system (VCS) is a system that records changes to a file or set of files over time so that you can recall specific versions later.

There are several types of a version control system and distributed is one of them. In a distributed version control system, users will fully mirror the repository. Server-issues won’t be a problem for this type of VCS, unlike Centralized Version Control System (CVCS). Users can easily backup their files. This is just the perfect type for a collaborative team project.

Installation

Linux:

  • Fedora: $ sudo dnf install git-all
  • Debian-based: $ sudo apt install git-all

MacOS:

  • install the Xcode Command Line Tools
  • If you’re on Mavericks (10.9) or above, run git from the Terminal the very first time. Then, if you haven’t installed it yet, it’ll guide you to install it.

Windows:

Setup

If git is already installed, you need to setup your user and email for making commits. Simply run these commands:

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

Commands

There are lots of commands on git. In this section, I’ll cover a few important and notable git commands available.

git init [-q | --quiet] [--bare] [--template=<template_directory>]
[--separate-git-dir <git dir>] [--object-format=<format>]
[-b <branch-name> | --initial-branch=<branch-name>]
[--shared[=<permissions>]] [directory]

This command will simply create an empty git repository. You can also run this command on an existing repository. It is safe to do it and it’s usually done to collect newly added templates.

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
[--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
[--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
[--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>…​]

In short, it is used to any new or modified files to the index before running the command “commit”. I’ll be covering “commit” command next.

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
[--dry-run] [(-c | -C | --fixup | --squash) <commit>]
[-F <file> | -m <msg>] [--reset-author] [--allow-empty]
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
[-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
[-S[<keyid>]] [--] [<pathspec>…​]

A command to record changes to the repository. It’ll create a new commit containing newly added/modified contents along with a “note” to describe the changes.

git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
[--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
[-u | --set-upstream] [-o <string> | --push-option=<string>]
[--[no-]signed|--signed=(true|false|if-asked)]
[--force-with-lease[=<refname>[:<expect>]] [--force-if-includes]]
[--no-verify] [<repository> [<refspec>…​]]

A command to update remote refs using local refs, while sending objects necessary to complete the given refs. In short, this command is used to “push” your newly added/modified contents into your repository.

git clone [--template=<template_directory>]
[-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
[-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
[--dissociate] [--separate-git-dir <git dir>]
[--depth <depth>] [--[no-]single-branch] [--no-tags]
[--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
[--[no-]remote-submodules] [--jobs <n>] [--sparse]
[--filter=<filter>] [--] <repository>
[<directory>]

As the name implies, it is a command to “clone” a repository into a directory. As simple as it gets.

git pull [<options>] [<repository> [<refspec>…​]]

It is a command to fetch and download contents from a remote repository to your local repository, updating your local repository with same contents listed on the remote repository.

git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>…​
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>…​]

A command to switch between branches and can even create a new branch through this command.

git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
[--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
[--[no-]allow-unrelated-histories]
[--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>…​]
git merge (--continue | --abort | --quit)

A command to merge a source branch to a target branch. The source branch history remains.

git rebase [-i | --interactive] [<options>] [--exec <cmd>]
[--onto <newbase> | --keep-base] [<upstream> [<branch>]]
git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
--root [<branch>]
git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)

It is a command to take all the changes that were committed on one branch and replay them on a different branch.

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…​
git revert (--continue | --skip | --abort | --quit)

Revert the changes that the related patches introduce, and record some new commits that record them.

git stash list [<log-options>]
git stash show [<diff-options>] [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>…​]]
git stash clear
git stash create [<message>]
git stash store [-m|--message <message>] [-q|--quiet] <commit>

Saves local modifications away and reverts the working directory to match the head commit.

git remote [-v | --verbose]
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <url>
git remote rename <old> <new>
git remote remove <name>
git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
git remote set-branches [--add] <name> <branch>…​
git remote get-url [--push] [--all] <name>
git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <url>
git remote [-v | --verbose] show [-n] <name>…​
git remote prune [-n | --dry-run] <name>…​
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)…​]

Manage set of tracked repositories.

git branch [--color[=<when>] | --no-color] [--show-current]
[-v [--abbrev=<n> | --no-abbrev]]
[--column[=<options>] | --no-column] [--sort=<key>]
[--merged [<commit>]] [--no-merged [<commit>]]
[--contains [<commit>]] [--no-contains [<commit>]]
[--points-at <object>] [--format=<format>]
[(-r | --remotes) | (-a | --all)]
[--list] [<pattern>…​]
git branch [--track | --no-track] [-f] <branchname> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
git branch --unset-upstream [<branchname>]
git branch (-m | -M) [<oldbranch>] <newbranch>
git branch (-c | -C) [<oldbranch>] <newbranch>
git branch (-d | -D) [-r] <branchname>…​
git branch --edit-description [<branchname>]

A command to list, create, or delete branches.

As stated before, these are only a few of commands available meaning there are plenty of commands that I haven’t stated. You can search all of the available commands through git’s official website, https://git-scm.com/.

Implementation

On our project, we “store” our code in Fasilkom’s gitlab, https://gitlab.cs.ui.ac.id/. Every developer has their own respective branch to put their individual codes (which is distributed on each Sprint Planning). Here is one of my branch for example:

Before each Sprint Review, we merge our code to “staging” branch so it’ll be sort of a finished product but not really.

Luckily, there is a GUI provided by gitlab to merge and resolve conflicts when merging through merge requests. This is such a helpful feature for me. But sometimes, merging would be much more convenient if we’re doing it locally.
Here are some examples of previously completed merge requests:

Gitlab helps our project a lot. Doing this project without gitlab is possible but very time-consuming.

Conclusion

Git is a perfect tool to maintain files. It is not just good for personal use, it is also perfect to maintain a collaborative project. We can easily track records of changes on files with git. Use git to maintain your files!

Source: https://git-scm.com/

Currently studying Computer Science at Universitas Indonesia