Version Control with Git and Github

What is “version control”, and why should I care?

Mohit Sharma
8 min readJan 25, 2019

Version control is a system that records changes to a file or set of files
over time so that any specific versions can be accessed later.

What is Git ?

Git is a version control system (VCS). When developers create something (a project for example), then they modify their code constantly, releasing new versions up of project to and after the first official release. Git now-a-days has become an inevitable skill in resume.

A version control system is like a logging system. It keeps a record of the modifications made by individual users in a central repository. With the help of VSC, developers can easily collaborate with each other, as they can download a new version of the software, make some changes to it, and upload the newest revision. Every other developer can see these changes, download them, and contribute.

Why should you use Git?

With the help of Git, you can —

  • Maintain different versions of the same project. We no longer need to create the copies of same project in order to make the changes (maybe add a new feature to the project). Since Git keeps track of changes you make in our project, it not only saves you a lot of memory, it also makes the project clean and maintainable.
  • Ensure that you never lose any code. If something goes wrong you can easily revert selected files or whole project back to any of its previous state.
  • Compare changes made to the project over time. This makes debugging the code super easy and manageable.
  • See who did what. For example while working in a team of developers, synchronization your work is always a tedious task. With the help of Git you can see who last modified something that might be causing a problem. And many more. Thus collaboration of work becomes smooth and efficient.

So what are we waiting for. Let’s get started !!

Installing Git

  1. Go to https://git-scm.com/downloads
  2. Download Git Application by selecting your Operating System (Windows, Mac or Linux).
  3. Install the application on your device.

Setting basic git configuration: git config

The git config command is used to set basic Git configuration on a global or local project level on your system. We need to set user name and user email id to identify different users working on same system.

git config --global user.name “your_name”
git config --global user.email “your_email@example.com”

Git Repository

A Git repository (or Git Repo for short) in the most layman terms is the folder where your project files are stored and that I want to track. It has Git active in it.

So what do I mean by “Git active in it”. Well whenever we track a folder with Git, then Git creates a hidden folder called .git inside that project folder. Just this much makes a folder into a Git repository.
Any folder can be made into a Git repo (I’ll tell you how). It is quite obvious that you’d only want to track your project files and not everything on your system.

Setting up a repository — git init

  • The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository.
  • Open your terminal or git bash in Windows . Now move to the folder with the project: cd |path-to-project-folder|
  • Once you’re inside the project folder, initialize a new repository by typing the command
git init
  • Executing git init creates a .git subdirectory in the current working directory, which contains all of the necessary Git metadata for the new repository. You don’t need to worry about how Git stores project and changes made to it. Git will take care of everything for you.

Inspecting a repository — git status | git log

Git sees every file in your working directory as one of three things:

  1. tracked — a file which is already being tracked by Git.
  2. untracked — a file which are currently not been tracked by Git.
  3. ignored — a file which Git has been explicitly told to ignore. For example heavy media files like Images and Videos which do not undergo changes frequently.

In the layman terms, git status and git log commands tells you the current state of your project.

  1. The git status command displays the current state of the working directory. It means it displays all files which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
  2. The git log command displays different versions of project committed. It lets you list the project history, filter it, and search for specific changes. While git status lets you inspect the working directory and the staging area, git log only operates on the committed history.

Note: Ignored files are tracked in a special file named .gitignore that is checked in at the root of your repository. The .gitignore file must be edited and committed by hand when you have new files that you wish to ignore.

Getting your hands dirty — git add | git commit

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.

git add name_of_your_file

Commits are like checkpoints or versions of your software. Even a new single line added to your code can count as a new version. So after you add the file you need to do a commit to make a version out of it.

A commit has two important parts:

  • A commit id. Unique version id to identify a version of the commit.
  • A commit message. A commit message is helpful for you to understand what was the commit all about or version created for.
git commit -m “add message to remember your work”

Try to find out what git checkout, git reset, git revert commands are used for.

Branching

One of the most important feature of Git is Branching. Branches are used to develop features isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

It’s important to understand that branches are just pointers to commits. When we create a branch, all Git needs to do is create a new pointer. It doesn’t change the repository in any other way.

Then, you create a branch using the following command -

git branch crazy-experiment

The repository history remains unchanged. All you get is a new pointer to the current commit.

Understand branching a little deeper

  • Open Command prompt (Windows) or Terminal (Mac or Linux) and type in git branch and press Enter. We’ll notice that it says master branch (default branch).
  • Now let’s create another branch of our current project using the command
git branch <branch-name>
  • Now type in git checkout <branch-name> to go to the newly created branch.
  • Let’s do some changes in the new branch and commit the changes.
    Don’t forget to add the changes first.
  • Now let’s say we want to merge the newly created branch into the master branch. We will go to the master branch git checkout master
  • Then we merge newly created branch into master branch using
    git merge <branch-name>

What is Github?

  1. Github is not the same as Git. GitHub is a cloud based repository hosting service. Just like we use Google Drive, think of Github as the “cloud” for code.
  2. But why not Google drive or Dropbox for same task? Well, Github is designed specially for this purpose.
  3. While Github is a cloud hosting codes, Git is a tool. Github uses git to manage codes uploaded on it.
  4. GitHub will host our source code projects written in a variety of different programming languages and keep track of the various changes made to every iteration. It is able to do this by using Git, a revision control system that runs in the command line interface.
  5. Are there any other platforms like Github ? Yes, Oher websites such as such as BitBucket, GitLab or Gitorious use git as well.

Github is like a social networking site for programmers. We can safely store our codes on the cloud. To share our code with others, we do not need to email the recipients the compressed zip files or anything. Sharing codes via compressed zip files and emailing the code is history. Companies love it when you have a Github profile and they can see your work before even calling you for an interview.

Remote Repository

Github Repo is often referred to as remote repo. Why Remote? Because it is not close to you, it is remote, it is on the cloud. Every project needs one remote repo for itself. Let’s create one right now.

Before you can put your code on github remote you need to tell your local repo the url for the remote repo.

Connecting to remote repository

  • Once you have a remote repo setup, you will need to add a remote repo url to your local git config, and set an upstream branch for your local branches. The git remote command offers such utility.
  • This command will map remote repository at <remote_repo_url> to a ref in your local repo under <remote_name>. Once you have mapped the remote repo you can push local branches to it.
git remote add origin <remote-repo-url>

Note: Remote connections are more like bookmarks rather than direct links into other repositories.

Syncing your code to remote repo

git remote | git fetch | git pull | git push

  • Pushing is how you transfer commits from your local repository to a remote repo.
git push <remote> <branch>
  • If we want to push the commits in the master branch to the remote repo at origin : git push origin master

How often should I push my code to github?

As often as possible. It is your CTRL+S in the cloud. Your laptop is unreliable. You should push every few hours or whenever you have had some significant progress in your work. This means multiple commits.

  • Merging upstream changes into your local repository is a common task in Git-based collaboration workflows.
  • This can also be done with git fetch followed by git merge, but git pull rolls this into a single command.
git pull <remote>

Quick recap

  1. Create a repository on Github.
  2. Configure your local repo to add the github repo url
    git remote add origin <remote-url-repo>
  3. Push/upload the code from the laptop to github repo
    git push origin <existing-branch-name>
  4. Pull the code from remote repository to local computer
    git pull <remote>

Cloning an existing repository: git clone

  1. Fetching is what you do when you want to see what everybody else has been working on. Since fetched content is represented as a remote branch, it has absolutely no effect on your local development work. This makes fetching a safe way to review commits before integrating them with your local repository.
  2. The git fetch command imports commits from a remote repository into your local repo. The resulting commits are stored as remote branches instead of the normal local branches that we’ve been working with.
  3. This gives you a chance to review changes before integrating them into your copy of the project. git fetch <remote> <branch>

I hope now you have more clarity on Version Control System. Feel Free to reach out to me in case of any doubts or updates.

--

--