Mike Mukul

Mike Mukul

Managing Code in GIT

Getting the most out of a distributed version control system.

When programming, most software developers create code on their local machine. It’s common then to upload the code when everything is ready for production. But in a team setting, that’s not feasible. To make development easier, your team needs a system that can control your version of code.

Git: a version control system

Git is an actively-maintained, open-source, version control system used for software development. Developed by Linus Torvalds in 2005, the distributed version control Git architecture was set up so that every working copy of code is a repository. Rather than having a centralized repo, like in Subversion, each repo has a complete history of code commits and changes.

Put more succinctly, this version control system records all the changes made to a file over time so that any revisions can be made to a specific version later.
ITH_database diagram (1)

Benefits of Git

  • Fast.
  • Distributed.
  • Tracks every change made, and by who, along with the time of change (or addition to code).
  • Provides simple and easy steps to rollback your changes whenever needed.
  • Gives a globally-dispersed team the ability to facilitate work and collaborate.
  • Uses branching/merging features to copy and update the code from existing repo without affecting the Master Branch.
  • It can version any file, not only code files.
  • No need to keep multiple versions with different names of the same file.

What is Github?

Github is a source-code management tool and a web-based repository hosting service. Sometimes people, especially beginners, get confused with Git and Github, but it’s not a single identity. Github is different than Git, but Github uses Git to push any change to the repository, which is hosted on Github.

To start with Github, you need to create an account and then create a repository inside. Once you create a repo you should see a screen on Github that displays all the basic commands you will use to set up and start pushing to the repository.

Git Workflow
Git Commands

Git commands

When using Git, here are some basic commands you can use.

Setting Up Git

First of all, you have to check if Git is installed on your machine. If Git is not installed, then you can download it or install it via the command line, depending on which OS you have.

This command will tell you which version of Git you have already.

git --version

The command should bring up information similar to this image.
screen-shot-2016-10-14-at-5-20-35-pm

Set your Identity

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Note: –global flag will make this setting for the entire system. You can see this setting by typing the command:

git config --list

Initialize a Repository

By using the command git init you can initialize an empty git repository (repo) into any directory/folder. After that, you can start creating files/folders inside this folder. By running the command git init you should see something similar to this:
screen-shot-2016-10-14-at-5-23-29-pm

Cloning a Repository

An existing repo can also be cloned using the git clone command and it will initialize the git repo into your cloned directory.

git clone https://github.com/username/reponame.git

screen-shot-2016-10-14-at-5-26-05-pm

Review Status before Adding and Committing

The command git status will show you the current status of your repo. It can show both changes and untracked files. It’s always recommended to review the status of your repo before committing a file (or files) to your repo.

Untracked File
screen-shot-2016-10-14-at-5-28-45-pm

Untracked and Added Files
screen-shot-2016-10-14-at-5-29-21-pm

Stage Files for Commit

The git add command will stage the files for a commit. You can either add all files together or specific files for a single commit.

git add --all or git add .

screen-shot-2016-10-14-at-5-30-16-pm

Adding individual file

git add header.html

screen-shot-2016-10-14-at-5-32-24-pm

Committing Your Work

Once files are added and ready to commit, you can use the commit command to commit your work. (Note: As mentioned above, before executing the commit command always review the status).

git commit -m "Initial Commit"

screen-shot-2016-10-14-at-5-33-27-pm

Branching & Checkout

A new branch can be easily created by using the git branch command.

git branch dev

Git checkout command is used to switch between available branches

git checkout dev

screen-shot-2016-10-14-at-5-34-08-pm

Push to Origin

git remote add origin https://github.com/yourgitusername/yourgitreponame.git
git push -u origin master

screen-shot-2016-10-14-at-5-35-28-pm

Merging Branches

git checkout master
git merge dev

By using Git Merge you can merge your other branches to the Master Branch.

screen-shot-2016-10-14-at-5-36-00-pm

Opinions will vary on whether a centralized or distributed version control system is better. Here at IT Hands, as a globally-dispersed team, we like using this quick-and-easy software development system. Is Git the right system for your next project? Contact us today with any project on your list at solutions@ithands.com.

Subscribe to our blog: