What is Git and how to use Git in your own project?

What is Git?

In short, Git is a free and open-source distributed version control system. If you have used CVS/SVN, they are centralized repositories, in contrast, Git is the “distributed” version. It was created by Linus Torvalds himself as a tool to manage the Kernel. He has a very nice Google Talk about Git, which I recommend you to check it out first. If you have had some experience using SVN, you can easily understand. I’m going to include some summary of the video in this article.

Why Git?

  1. It is distributed, and it is the only right way to do version control (as Linus say so). Well, actually it means every one owns a full copy of the repository and no single repo is more important than the others. Collaboration happens by pulling and pushing code between repos and branches.
  2. Branching is inherent property of development. Branching and Merging are made easy and are encouraged in Git.
  3. It guarantees integrity and consistency of data you put into Git. (SHA1 digest for every version of committed data) That means your files would not be corrupted during clone, push, pull. Or if it is, you’ll easily detect it.
  4. It encourages Commit often, because everyone owns their repos and branches, they don’t need to touch the “Central” Repo. Developers can commit their code as early as possible and keep history of them without worrying other people would get the un-finished or un-tested code.
  5. No Commit-access issue.

How to start using Git in your own project?

I’ll put it in this way, most articles I’ve found would introduce each of the commands individually, I would just show the usage of the commands in the following:

Installing Git

I’m not going to cover installation as it is trivial, use the package manager of your Linux distro to intall it or get it from the Git official site. You should be able to use the “git” command in the terminal to start.

Do it Locally

  1. Create a folder called “MyProject” to hold the Local repository
  2. This will create the meta data(.git) of repo in this folder
  3. Start coding or put your existing source codes in this folder
  4. Tell Git to track all the files
  5. Commit the code to your local repo, and with the comment “I wrote some cool code!”
  6. Done! Now just code and commit.

Setup in your private server

First you must have SSH access to the server, Git supports SSH internally. You will use the server to host the repository only, not directly working on it. If you use Github to host your project, you can skip the first server setup. (If you want to setup HTTP access, this article can’t help you 🙂 )

On the Server:

  1. Create a user called “git”
  2. Give it a password
  3. Switch user to git
  4. create a folder in your home dir called “MyProject.git” for the Repo
  5. Init the repository (–bare means bare repository, no files/work tree)

On your machine:

  1. Clone the repo from the remote server(yourserver.com) to a local folder called “MyProject”
  2. create a file
  3. Edit the file
  4. Tell Git to track all the files
  5. Commit the code to your local repo
  6. Push to the branch “origin/master”, which is your remote server ( you can use “git branch -a” to view all branches)

That’s basically it, I’ve just started to use Git, so I don’t know a lot of advanced functions in Git. Please feel free to write comments below or email me!

Share your thoughts