What is Git? – Understanding how it works

Posted by

You have probably heard of GitHub if you are on the web development path. You might answer, “It is a tool to share codes with a team”. That is true, but not quite.

What is Git?

Git is a version control system. You can manage multiple versions of your project. Git makes it easier to manage codes with its commands.

How Git controls versions

Git creates a new directory with a copy of your original project directory (= git repository). What it actually does under the hood is to sync files between two directories and track the files that are modified. Also, git repository can track an arbitrary number of branches. That way, it manages multiple versions of your project.

Commands and glossaries

Here is a list of commands that are used to manage your project. All the glossaries below can be found here.

Commit

A commit is the state of your project, which stores new states from the last commit. Each commit contains states from one before so that it chains all the states from the beginning.

git add <filename>

git commit -m "message here"

Branch

A branch is an active line of development. The most recent commit on a branch is referred to as the tip of branch. That means, a branch has a series of commits from the base branch(= where they diverged from). Branch head refers to the tip of branch by default. So, its head moves forward as additional commits are created on the branch.

git branch

master
*develop
feature/create-modal

Working tree

A working tree is a tree of checked-out files. If you checkout another branch, the working tree will contain the HEAD of the commit’s tree on the branch. It also holds any local changes not yet comited.

Checkout

It updates all or part of the working tree. You use this command to point at another branch or commit.

git checkout feature/create-modal

Push

It compares the local head ref to its remote and checks whether it is an ancestor to the local ref. If so, it updates the remote head ref with new changes made in the local head ref.

git push origin develop

Merge

It is to merge another branch into the current branch. Merging is performed by identifying changes made since the branches diverged. It applies all those changes into the current branch. In cases where changes conflict, there needs to be manual intervention of selecting which changes to be applied.

git merge feature/create-modal

Fetch

Fetching a branch means to get the branch’s head ref from a remote repository, to find out which objects are missing from the local object database, and to get them, too.

git fetch origin master

Pull

It is a combination of “fetch” and “merge”.

git pull origin master

Fast-forward

A fast forward is a special type of merge. It applies another branch’s changes as updates to its ancestor branch. The “git merge” does fast-forward by default unless specify –no-ff.

Cherry picking

Cherry picking in Git means to choose a commit from one branch and apply it onto the current branch. The changes are added as a new commit.

git cherry-pick <commit-hash>

Rebase

To reapply a series of changes from a branch to a different base, and reset the head of that branch to the result. In simpler words, “moving the base of a branch onto a different position.” It doesn’t make a commit like “merge” makes one.

git rebase develop

Also, these changes become new commits from Git perspective even though they are identical to what they were before. Since it plucked a series of changes from the branch and applied them onto another, the rebased branch history will be completely rewritten. It can be a major issue when collaborating with someone.

Thanks for reading.

Hope you enjoyed the article. If you have any question or opinion to share, feel free to write some comments.

Facebook Comments