GIT – Distributed Version Control and Source Code Management (SCM) System

Introduction To GIT

  1. Abstract
  2. Creating a Repository
  3. Checking Out A Repository
  4. Add And Commit
  5. Pushing Changes
  6. Branches
  7. Update and Merge
  8. Tagging
  9. Revert To Specifit Commit

Abstract

GIT is a distributed version control and source code management (SCM) system developed by by Linus Torvalds for Linux kernel development, Git has since been adopted as the choice of source code management by many projects and companies. This document covers the creation, usage and management of the GIT versioning system.

The installation of GIT is beyond the scope of this tutorial, however, here are some links that will help

 

Creating a Repository

To create a repository for your project, select a place where you have write privileges. For our purposes here, we will create a repository for a project name ACL. So lets begin with creating the repository(repo).

mkdir ~/git_repos
mkdir ~/git_repos/acl
cd ~/git_repos/acl
git init

The above commands will firstly create a directory name .git in which, in turn, will contain some directories and files need to work with the GIT system. A quick look inside the .git directory shows us the following

# ls ~/git_repos/acl/.git
branches config description HEAD hooks info objects refs

Checking Out A Repostiory

If you are using a local repository, checking out a copy is as simple as:

git clone ~/git_repos/acl

If you are checking out a remote repository, then use this command.

git clone username@hostname:/home/kevin/git_repos/acl

The local repository consists of three “trees” maintained by git

Working Directory
Contains the acutal files
Index
Acts as a staging area
HEAD
Points to the last commit

The workflow will look like this.

WORKING DIR -> add -> INDEX -> commit -> HEAD

Add And Commit

When you add a file to GIT, the addition is stored as a proposed changed, which means it is stored in the Index. To add a file named acl.php is as simple as:

git add acl.php

If you had multiple files you wanted to add, you can use the command

git add *

Currently the file is just a proposed addition, and not commited to the repository. To commit we use git commit.

git commit -m “Message to say I added acl.php file”

The acl.php is now in HEAD, but not yet in the remote repository repository. Any additions and changes need to be pushed to the remote repository before they are available to all.

Pushing Changes

To push changes to the remote repository, the git push command is used.

git push origin master

The master can be whatever branch you want to push the changes to.

Thats IT!?. Yup, thats it

If you have not cloned an existing repository and want to connect your repository to a remote server, you need this command

git remote add origin server_name

Now you are able to push your changes to the selected remote server

Branches

Branches are used to develop features in isolation. 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.

The workflow will look like this..

           ---------- Feature ----------
          |                            |
          |                            |
--------------------- Master -------------------------
       branch                        merge

So, to create a branch called my_feature_branch, and switch directly to it

git checkout -b my_feature_branch

You should see a message like

Switched to a new branch ‘my_feature_branch’

Now, to switch back to master brach

git checkout master
Switched to branch ‘master’

A local branch is not available to others, as it is not yet in the repository. The branch needs to be pushed to the remote repository to be available to all.

git push origin my_feature_branch

And finally, to delete the branch, we use the -d switch

git branch -d my_feature_branch
Deleted branch my_feature_branch (was b37035b).

Update and Merge

Of course, you are not the only person using the repository. Others will commit changes and you need to keep your local copy up to date by pulling new changes from the remote repository. To do so, use the pull command. This will both pull and merge changes from the remote repo, into your local copy.

git pull

To merge another branch into your active branch eg: master, use this command.

git merge my_feature_branch

When git pulls from the remote repository, or merges a branch, it tries to auto-merge the content. Inevitably, there are conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged. Using:

git add

Some of these conflicts can be avoided by checking for conflicts before merging with git diff

git diff source_branch target_branch

`

Tagging

Tagging a release is a great idea for tracking releases and milestones in a project. Those who are familiar with SVN would be familiar with this concept. To create a new tag, first use the git log command to get the last commit ID and then use git tag with the ID you just gained.

git log
commit b37035b424c82dd1b4baee3b8184ddbead32edd0
Author: Kevin Waterson <kevin@jyotish.(none)>
Date: Fri Jul 5 09:37:27 2013 +1000git tag 1.0.0 b37035b424c82dd1b4baee3b8184ddbead32edd0

Another simple way to use a an annotated tag in GIT, which is really just a pointer to the current commit is this. This example assumes a tag for a release of version 37 of a project.

git tag -a v37 -m ‘Release 37’

Revert To Specifit Commit

Sometimes you need to revert or rollback to a specific commit. Here we show how to revert back to commit id 296b09c

git reset 296b09c
git reset –soft HEAD@{1}
git commit -m “Revert message to tell you have reverted”
git reset –hard
Comments

Published by

PakCu

I'm a simple person