Introduction To GIT
- Abstract
- Creating a Repository
- Checking Out A Repository
- Add And Commit
- Pushing Changes
- Branches
- Update and Merge
- Tagging
- 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/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
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:
If you are checking out a remote repository, then use this command.
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.
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:
If you had multiple files you wanted to add, you can use the command
Currently the file is just a proposed addition, and not commited to the repository. To commit we use git commit.
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.
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
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
You should see a message like
Now, to switch back to master brach
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.
And finally, to delete the branch, we use the -d switch
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.
To merge another branch into your active branch eg: master, use this command.
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:
Some of these conflicts can be avoided by checking for conflicts before merging with git diff
`
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.
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.
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 –soft HEAD@{1}
git commit -m “Revert message to tell you have reverted”
git reset –hard