GitHub

Git is a distributed revision control system that allows a team of developers to work together, when all developers are using the same code, and it also helps the team of developers to cope with confusion that tends to happen when multiple developers are editing the same code.

What is Git?

Git is a distributed revision control system that allows a team of developers to work together, when all developers are using the same code, and it also helps the team of developers to cope with confusion that tends to happen when multiple developers are editing the same code.

At first, a new user starts to develop code; he first downloads files to the local directory and other developers have their own changes in their local directories. GitHub makes it easier for the developer to modify and commit the code online in a very organized way. Git stores everything as BLOB and every commit has its own SHA key. It also helps developers to fetch the branch as they needed in an easy way.

We can also get the other GitHub users’ code to our local repository, and work on them. So, using this tool doesn’t cause any data loss, because GitHub maintains a snapshot of every commit, and we have the ability to rollback any changes at any time

GitHub is used to host the code on web in a safe manner, and it is used by developers to fix the bug and develop new features in any platform. GitHub is a free or private code hosting system. Most of the open source developments are hosted on GitHub, and users can download the code from the web.

Each developer has their own branches for development and bug fixing. After fixing bugs or developing new features in projects, the branches are merged as a single branch and ready to be deployed to the production using that branch. It also helps developers patch their code in an organized way.

There are many version control systems currently used by the developers to organize their code in the web, like SVN, Git, Mercurial by IBM, CVS and Perforce.

Creating a Github.com account makes it easier to host the code Online. The core of the Git was originally written in C programming language, but Git has also been implemented in other languages like Java, Ruby and Python.

Advantages:

  • Using the Git, we can to track the changes made by the team as well as new files added.
  • Find out who made the changes in the team and when there were committed.
  • Deploy changes to production, using GitHub by commands.
  • Rollback the code, when needed.

Setting up Git in command line:

First install the Git, using apt-get install git-core on a Linux machine.

sudo port install git-core is used to install the git on a Mac machine.

Next, we need to configure your (user) name using the following command:

git config –global user.name “User name”

Also, use the following command to associate your email address, as it will help you to associate your commits to your GitHub account.

git config –global user.email “User Email”

Creating GitHub account, Organization and Online Repository:

The user needs to first create an account from here. After creating the GitHub account, the user needs to create an organization to maintain the project repositories. Then, the user needs to create a repository name (Gitsamplerepository). After creating the repository, the default branch becomes a Master, and the user needs to add files to it and commit the changes with a push operation. A single organization can have multiple repositories and a single repository can have multiple branches.

Creating a Local Repository:

After creating the online repository, we need to create local repository to clone our remote repository using git clone repositoryurl. Before doing that, we need to create a directory in the local machine using mkdir GitDemo , andthen cd GitDemo to change the directory to the GitDemo Directory.

Now, we need to initialize the repository to local using git init command; otherwise, we can’t use any git command.

 Repository:

A repository is simply a place where the history of your work is stored. It often lives in a .git subdirectory of your working copy – a copy of the most recent state of the files you’re working on.

Version Control System:

In a typical multi-developer environment here are how things will work:

  1. Every developer creates their own branch for fixing bugs and development.
  2. Every release will have an Integration branch also.
  3. Once the code is developed or bug is fixed, developer moves the changes to the integration branch and build will be made from the integration branch and application will be tested.
  4. Once the System is tested and verified, the changes in integration branch will be sent to the main branch.
  5. All the change in the main branch will be sent to the production.

Specific Git commands:

There are lot of git commands; however, the basic commands are mostly used by the GitHub users as below:

Git init:

Initializes a new Git repository. Only after running this command, the folder becomes a Git repository or Git folder, and also the terminal accepts any Git commands.

Git Config:

This is used to set up the git account with local.

Git help:

Git help command is used to understand git commands. Users can also use this command to understand a specific command, like git help merge.

Git add:

This command is used to add the newly created or modified files to staged area. Staged area is the place where the files are grouped before they are committed.

Git commit:

This is the most important command in git and it commits the files to git repository. After the commit, the file is added to snapshot of repository.

Git push:

The local changes will be moved to the remote branches using the git push command.

Git merge:

Git merge command merges two different branches into one branch. For example developer A changes his own commits with his branch and developer B changes his own commits with his branch. The merge command can merge both developer’s code to a single branch.

Git checkout:

Git checkout command allows you to navigate between branches.

Git Branch:

Git branch command is used to create a new remote branch.

Git pull:       

Fetches the code from remote branches and merge them with local branches

Git Stash:

Git stash is used to keep the changes made by developers in the branch aside for future use, allow to checkout another branch and do modifications on that branch, and checkout back to same branch.

Using GitHub in command line

Git with command line

To initialize Git repository, we need to use the command git init. It creates a .git directory and contains all necessary files in it to maintain the projects.

Next, type git status to see the current status of the project.

Now, you can developer code from your local machine and host the code in GitHub using the following commands:

For example, the repository name is MSTSolutions and the project name is MyProject. First, we need to create the MSTSolutions repository, create MyProject branch, and host the local code to that MyProject branch using git add command.

Now, clone an existing repository using the git clone repostoryurl (git clone git@github.com:mstsolutions/TestRepository.git)

git clone git@github.com:keybo/TestRepository.git directoryname

We also need to specify the target directory name to store the remote copy into our local directory using above command.

Add files to the stage using git add command. Multiple files names are separated by a space.

Git add command used to add the local files to staged area.

“Git add -A  .”  is used to add all files including deleted files to staged area.  (Here, the dot indicates the current directory).

Git Stash:

Git stash is used to keep the changes made by developers in the branch aside for future use, allow to checkout another branch, and do modifications on that branch and checkout back to same branch.

Git Differences:

Developer commits changes to a branch and needs to know the code differences between the working directory and most recent commit.  Here, we can use git diff HEAD.

Staged Differences:

Another great use of diff is used to compare the files that are staged.  git diff –stage will show all differences in the stage like new change, modifications on the code, and everything related to the changes of on the stage.

To see the differences between two commits, use the following commands with its corresponding SHA code.

git diff –name-only SHA1,SHA2

If we don’t know SHA, we can use the following command to see the differences between two corresponding commits.

git diff –name-only HEAD~10 HEAD~5 which shows differences between 10th and 5th commit.

Staged Area:

The place where the files are grouped together before the files are committed to Git. There are four steps in staging the files.

  1. Stage:

Files ready to be committed to the branch are called staged files.

  1. Unstaged:

Files in the local folder but not ready to be committed.

  1. Untracked:

Files are new to the git and not tracked yet by the git.

  1. Delete:

Files are ready to be deleted from the Git.

We also use the wildcards symbol to add all files in git using the file extension like, git add “*.txt”.

After the files are added to the staged area, every time we need to commit the changes using

git commit -m “the message that you want add about the changes”.

We can also use the git log to view all commits that we have made to GitHub.  Using this command, we can see all deleted and added files in chronological order. (git log –summary)

Git Branching:

While we are working as team to develop or fix bugs in a project, we need to create a separate branch for every developer working on that project.

To create new branch, use git branch branchname.  

To delete a branch, use git branch -D branchname.

If we want to clean up a particular directory, use the recursive command and specify the folder to delete: git rm -r foldertodelete.

Switching branches:

After creating branches, we need to switch back to the newly created branch to modify the code using git checkout branchname.

Example:

git checkout sampleproject

Merging the Branches:

To merge a developer branch with a master branch,  we need to first checkout master branch and use merge command.

Example:

git merge sampleproject

Fetching Branches:

Fetch will get any changes from a remote branch, updating your repository data, but it will not change any of our local data.

Example:

git fetch – fetches all remote branches to local

Pulling Branches:

Pull will perform a fetch and additionally merge the changes into the local branch.

Git Rebasing:

git rebasing is a another technique for merging.

Clean Untracked directories from Git:

When we push the code from local to Git, there will be a lot of untracked files that can’t be pushed. So, we have to clean all the directories and files from the local branch and then push the code to remote branch by using this command git clean -d -fx.

Summary

GitHub is an effective online version control system that allows developers manage their code and versions in the most collaborative way.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.