Source Control for Noobies

Sarannithish K S
IceApple Tech Talks
7 min readMay 5, 2023
Photo by Mitchell Leach on Unsplash

What is Source Control ?

Hey folks, what is source control? Is it controlling something at source?

Well, not exactly!! This is a beginners guide to understand the fundamentals of source control.

Let’s start with the problem statement first.

Assuming only a single developer has coded a product.
The product has come to a good shape, and it is delivered to a customer.

Many features were added to the product and now become difficult to be maintained by a single developer and the company hires more people to maintain the product.

The new developer now comes in and looks at the code and wants to fix a bug.

The new developer has to understand the history of the bug to fix it.

What are the Options?

1. Ask the previous developer who has developed the code.

What if the previous developer has left the company?

It is going to be catastrophic where fixing one bug can introduce many more.

So we need a solution to know what all happened in the development life cycle, so that anyone can trace back the time and see what change introduced this bug.

That’s where control comes to the rescue. Source control system keeps track of the changes that undergo in a file by maintaining log of every difference the file undergoes.

What is the business impact ?

Let’s jump into one more story. Let’s assume the product is given to a customer (named Alice) and it is in use.

We have added more features on top of existing product which is not yet released to the Alice. Customer named Bob buys our product with the added set of features.

All of a sudden, Alice comes back with a bug in the product and wants it to be fixed, but she is not ready to pay the cost for the additional features.

What do we do now? Our product now has all the added features and if we give the fix on top of it, Alice will be able be able to use the new features.

Again, source control is there to save us.

Source control system helps version the product and maintain the releases as well.

Every single release can be independently tracked and delivered so that the business does not end up having a monolith which is difficult to sell.

With the help of versions and releases you will be able to provide the bug fix on top of the feature list which Alice already had without giving the additional features built for Bob.

A very powerful tool for source control is Git.

So, let’s jump into the details folks.

Git architecture

Git provides a two layered architecture for convenience and safety.

The first layer is remote and the second one is local.

The remote layer resides on a server which can be hosted in the cloud or any data centre. The local layer runs on the development machine.

Git tracks all the changes made to the files and provide mechanisms to freeze them.

The freezing of change needs to happen on both at the local layer and the remote layer.

The changes are initially frozen locally and then later pushed to the server.

This architecture provides an advantage to the developer to still hold lot of changes locally without having to push them or publish them until the changes are ready to be exposed to the world.

Repositories are the basic controllable/synchronizable entities and files are the basic trackable entities in the world of source control.

Repositories can contain many folders and files, but control and sync actions happen only at the repository level.

Git Branches

Git branches are like independent copies of code which gives freedom to the developers to experiment all their stuff without impacting the product mainline.

It also helps to keep the main product line intact without being exposed to any experimental code.

Sample branching strategy

The creation of “Feature” branch gives full freedom to the developer to play with his/her code without impacting the “Main” or “Dev” branch which are intended for customer and development release respectively.

The changes in the feature branch can be merged to the appropriate release branches only after the feature has become stable.

Let’s watch it in action

Git Commands

git init

· The command git init is used to create an empty Git repository.

· After the git init command is used, a .git folder is created in the directory with some subdirectories. Once the repository is initialized, the process of creating other files begins.

git add

· Add command is used after checking the status of the files, to add those files to the staging area.

· Before running the commit command, “git add” is used to add any new or modified files.

git commit

· The commit command makes sure that the changes are saved to the local repository.

· The command “git commit –m <message>” allows you to describe everyone and help them understand what has happened.

git status

· The git status command tells the current state of the repository.

· The command provides the current working branch. If the files are in the staging area, but not committed, it will be shown by the git status. Also, if there are no changes, it will show the message no changes to commit, working directory clean.

git config

· The git config command is used initially to configure the user.name and user.email. This specifies what email id and username will be used from a local repository.

· When git config is used with — global flag, it writes the settings to all repositories on the computer.

git branch

· The git branch command is used to determine what branch the local repository is on.

· The command enables adding and deleting a branch.

git checkout

· The git checkout command is used to switch branches, whenever the work is to be started on a different branch.

· The command works on three separate entities: files, commits, and branches.

git merge

· The git merge command is used to integrate the branches together. The command combines the changes from one branch to another branch.

· It is used to merge the changes in the staging branch to the stable branch.

Git Commands: Working With Remote Repositories

git remote

· The git remote command is used to create, view, and delete connections to other repositories.

· The connections here are not like direct links into other repositories, but as bookmarks that serve as convenient names to be used as a reference.

git clone

· The git clone command is used to create a local working copy of an existing remote repository.

· The command downloads the remote repository to the computer. It is equivalent to the Git init command when working with a remote repository.

git pull

· The git pull command is used to fetch and merge changes from the remote repository to the local repository.

· The command “git pull origin master” copies all the files from the master branch of the remote repository to the local repository.

git fetch

· The “git fetchcommand is used to pull the updates from remote-tracking branches.

· Additionally, we can get the updates that have been pushed to our remote branches to our local machines.

· As we know, a branch is a variation of our repositories main code, so the remote-tracking branches are branches that have been set up to pull and push from remote repository.

git push

· The command git push is used to transfer the commits or pushing the content from the local repository to the remote repository.

· The command is used after a local repository has been modified, and the modifications are to be shared with the remote team members.

Some Advanced Git Commands

git stash

· The git stash command takes your modified tracked files and saves it on a pile of incomplete changes that you can reapply at any time. To go back to work, you can use the stash pop.

· The git stash command will help a developer switch branches to work on something else without committing to incomplete work.

git log

· The git log command shows the order of the commit history for a repository.

· The command helps in understanding the state of the current branch by showing the commits that lead to this state.

--

--