Kickstarting with git #1

Sonika Baniya
7 min readOct 24, 2020

Git for beginners, a basic concept, git system, familiarization with workflow

git for version control system

How do we know physical growth of person around us? Our mind captures the snapshot of the last moment we saw that person and this snapshot remains with us. Next time we see that person, our mind compares the snapshot from our memory and knows the physical growth of the person. Now, Git is a distributed version-control system for tracking changes in source code during software development. So, does that make git ‘brain’ of version control system? Yes. Does git also make snapshot of each files in each specific point of time? Yes. What do you mean by specific point of time? By specific point of time, I mean each commit.

what does version update looks like?

1. Version Control System:

VCS is for configuration management system. It is due to Version Control System that today multiple frontend developers, backend developers, designers team up and work together at same project at same time. For example: Perforce, Mercurial, SVN, Git, CVS, etc.

Version Control System helps in concurrent development

There are two types of version control system
i. Distributed Version Control System
ii. Centralized Version Control System

Centralized vs Distributed Version Control System

2. What is git?

Git is distributed, open source, most common Version Control System. Most importantly, git is not github. Whenever I am delivering session on git, I usually make one slide that says “git is not github” in big and bold font. Because this is important. As a beginner to Software development, one person can have a hard time differentiating between git and github. So then what is github? Github is repository hosting place for version control with very easy GUI and very user friendly collaboration platform.

GIT IS NOT GITHUB
GIT IS NOT GITHUB
GIT IS NOT GITHUB

Alternative to github, we have gitlab, bitbucket as most commonly used repository hosting platforms.

3. Most Commonly used terms in git:

i. repository: This is collection of commits (which is also an reflection of what your project looked like at some point). Repository also defines HEAD (we will discuss about HEAD in following point). It also contains branches and tags.

ii. HEAD: HEAD is used by repository to know what is currently checked out. Checking out generally means in which point of working tree are we living right now. So bascially, we can checkout:
a) branch: In this case HEAD indicates branch.
b) commit: In this case HEAD indicated that comit which is generally called as detached head.

iii. commit: The commit is basically snapshot of your working file at one point of time. The whole persona of git that tracks history is based on commit. We can save new commit object in local git repository by simple git commit command. But exchanging commits has to be done explicitly.

iv. branch: The concept of branching is to make clone of main line of development and do neccessary changes there and not alter, practise the coding in main line of development. So, branch is basically name of commit i.e. parentage of commit that defines its history. Branching in git is very lightweight and instanteneous unlike other VCS where branching could be really expensive.

HEAD and branch concept

Break it down: In this diagram Master is parentage branch of commit num 3 which is ahead of previous commit 1 and commit 2. We make branch name “New branch” from master. So, master and new branch both is pointing to same commit. Since currently we are in master branch i.e. we are checkout at master branch currently so our HEAD is pointing at master branch.

v. tag: The tag is very similar to branch but this always points to same commit and is rigid.

vi. master/main: This is default branch and almost always the main line of development branch. From Oct 1st 2020, github has renamed their ‘master’ naming culture to ‘main’ to make it more community friendly.

vii. working tree: The working tree is directory in local filesystem which is linked with repository. All the changes are done before commit in working tree and then done only it is commited.

viii. the index: The index is staging area for your committed changes. Unlike othe VCS, git doesnt directly commit changes from working tree to repository. Firstly, changes are registered in the index (staging area).

4. Simple guideline to start working with git workflow:

(Taking example of github as repository hosting platform.)

Make an simple index.html, style.css, main.js files and put it into folder. Go to github and create a repository from GUI of github and copy its link either SSH or HTTPS in clipboard

1. cd that-folder-name
2. git init

Explanation: init means intialization of git in your working project folder. By this command an empty folder will be created in your folder that is called “.git”.

3. git add . 

Explanation: Here ‘.’ represents all unchanged files. In this case you are just initializing git so this includes all 3 files. So, we moved this 3 files from working area to the index as we discussed above.

git add . difference as explained above
4. git commit -m "This is my project initial setup"

Explanation: The snapshot is created with hash. Here, we are doing it on default branch i.e. master branch. So, parent of this commit will be master branch. This commit object will only be saved in local git repository.

5. git remote add -u origin link-you-copied-above-step-1-paste-here

Explanation: git exchange commits and communicate via remote in outside world. Adding this to your origin means you dont always have to push your code to long url. After adding this origin to your local git remote, you can now push pull or fetch code directly from your repository hosted without having to type long url.

6. git push origin master

Explanation: This command says push the commits in local branch named master to origin branch named master.

Now you can see your code in github’s repository. In this way you pushed your first project into github directory.

4. Branch vs tag:

As mentioned above, branch is parentage of commit that defines history. But meanwhile tag is branch with always same commit. So, why we need to know about tag? Because branch doesnt parentage same commit always. So, in the scenario where we have to revert back to previous stable version then tag is more promising than branch. So, its always best practice to merge branch with tags.

5. Commonly used commands:

git checkout -b "new-branch"

Explanation: To make new branch and checkout at the same time.

git stash

Explanation: To push all current unstage changes into temporary shelve so that you can later comeback to current scenario. This is most handy command and I personally am always using this like always. Its use case scenario is when you need to work on some other part of project but dont want to commit the changes because you are in middle of something and your job is not complete.

git stash pop

Explanation: To pull all the changes that you have recently stashed and revert into your job.

But Can I do multiple stash? Yes, you can.

git stash list

Explanation: This will show you list of mutiple stash that you have previously done and its save according to index like stash@{0}

git stash pop stash@{2}

Explanation: To pop the stash saved in 2nd index

git cherry-pick commit-hash

Explanation: If your commit in wrong chain of development or wrong parentage. This will pick all the changes made in that hash to the line from where you are running this command.

git rebase master

Explanation: When your branch or commit is not up-to-date with master, you need to rebase your branch so that your branch gets up-to-date with master. Please note that this will only be up-to-date if your local branch is already the latest branch. If not then you need to pull master branch by entering command git pull origin master and then git rebase master

Also, you will find yourself easily performing in ZSH terminal. Lots of command have very cool shortcut in ZSH like

git checkout master -> gcm
git pull origin branch -> ggpull
git push origin branch -> ggpush
git checkout -b “new branch” -> gco -b “new branch”
git checkout branch-you-last-checkout -> gco -
git log ->gl

and many more …

On this note, hope this article was helpful. Happy reading, would love to know if anything is confusing and needs to improvise.

All the images used here are © Sonika Baniya. Use of image without credits will not be entertained.

--

--