HomePortfolioBlogGitHub

Git & GitHub

19 March, 2021 - 4 min read

Recently I got a new laptop and I have to set up everything new to code again. This means installing a bunch of stuff like Visual Studio Code (one of my favourites IDE out there), nodejs, and of course very important "git". So then I thought, why not write down about Git.

What is version control and why should we use version control

Meme about never ending final version

Remember the time when you have to write your thesis? You have made so many changes either after you reconstructed your thinking or after your supervisor commented, that your brain can't catch up anymore. What if there is a tool out there to help you keep track of the changes you make to the files, that would be cool, right? Well, indeed it is out there.


Version control (VC) 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 two types of VC: centralised VC and distributed VC. While centralised VC means there is a central server where it stores all the files for team to collaborate, distributed VC means everyone can have the whole version on their local machine.

Install Git

Git is free and open source distributed VC. Fact I didn't know until I write this piece: Linus Torvalds, the guy who invented Linux kernel, created Git in 2005. Git is free and open source distributed version control system. Please find the respective package to install on your machine (macOS, Windows or Linux). The guide is very intuitive to follow I believe.

Repositories

Repository or repo for short is where your files and directories plus the project's history that Git creates to track the changes are stored. Usually you will see a .git in your working directory.

  • to initiate a repository

    git init

Staging Area

In your working directory (where files and .git are stored) you can make modifications (add new files, change content etc). After modification you need to add these files or changes to the staging area. Staging area is where the modifications get recognised.

  • to add a file to a staging area:

    git add FILENAME 

    or

    git add .. 

    to add all files

  • to check the state of a repository

    git status

Commit

However, to save the changes in the staging area, you need to perform commit. It is like when you get a stamp on your card recognising your arrival time at work.

git commit -m "this is the change i want to save"

-m stands for message I assume.

Branch

Imagine you want to build a new feature but not yet want to mess up with the production. In this case you create a new branch. By default every Git repo has a branch called master. The new branch created will split out from the main branch master, so it has all the info of master up until it is created. To list all the branch you simply type

git branch

The branch you currently be will be marked with a star "*"

If you know which branch you want to switch you can type:

git checkout branchName

In case you want to create a brand new branch, then add "-b" after "checkout" and before the new branch name.

git checkout -b newBranchName 

So after you are done with the new branch (modify, add, commit), and you want to merge into master, and delete the new branch, simply type:

git checkout master
git merge newBranchName
git branch -d newBranchName

GitHub

OK, you make it the local repo. So you want to cooperate with people in your team or simply you want to be able to remotely access the repo you have been working on your work computer while you are not in the office. You need somewhere host your repo remotely, so you can always "clone" a working copy of it.

Github is a provider of hosting for software development using git. With Github you can have a decent basic services free of charge, like hosting your repo and collaborating with people. Go ahead to their website, register yourself and get an account. You can create a remote repo on Github and then clone a copy to your local machine, or you can create a local repo and push it to your remote repo.

Next time I will write about working with remote, pull and push...

Here are some links to dig in further:

Git Cheatsheet

Pro Git Book

© 2021, Built with

Gatsby