Git is the most popular version control system now a days. The reason behind popularity is its simple yet tremendous functionality. Unlike other VCSs (Version Control Systems), Git has some unique ways to control and share your data. Because of its huge functionality it also tends to become a lot complex for advanced users. As they say a thousand miles journey starts with a single step, we are going to understand the very basics of git in this article!
So, before starting anything we need to understand what Git is and what it does. Git is a version control system, which records changes in your code/files over a period of time. You can recall older version and manage changes. The working of Git is a little different than other VCSs. Most of the VCSs store the information about the data as the changes that have been made in different files. If you have 3 files in a project and you change any one of them, the VCSs will keep record of the change. That’s where Git differs, it keeps record of the changes along with the new changed file. Moreover, other VCSs generally operate online, storing all the data at the remote servers, while Git stores all the data on the local system as well as the remote server, which makes it easy for the user to make changes even when she is offline and commit the changes when she gets the network connection. In other benefits of Git, you don’t need to go to remote server to check for your project history, it’s all there on your local machine.
Before getting started, I would like to discuss one more feature of Git and that is repositories. There are two kind of repositories in Git, namely, local repositories and remote repositories. And believe me, they are the exact same thing! So, what exactly is a repository? It’s the place where you store all of your working data, your project files etc. Local repository is the one that is set up on your system and the remote is the one that is hosted on a server, probably on another continent! And here is the beauty of a distributed version control system, you can work with both of them just as they are the same thing! You can make changes to your local repository, add/delete files etc. and push them to remote repository.
Enough of concepts, let’s get started! The very basic thing to do with Git is to create a local repository, of-course, first you have to install it on your system! Type this in your RHEL based system to install git (make appropriate changes for Debian based systems):
sudo yum install git
And the next step is:
$ git init Initialized empty Git repository in /home/peeyush/git_test/.git/
And that’s it, there is your local repository. You can add files, make changes to them, commit the changes etc. (How? We will see shortly!). Now, generally after this people keep going on about working with local repository, but as I am getting you started, I am going to take a little different but practical approach. We are going to start with a remote repository! Now, where do you get a remote repository? Well, GitHub, of-course! Go towww.github.com
and create a account there if you don’t already have one. Now, create a new repository by clicking on the link on the top left corner:
Now, enter a name for the repository and click on the “create repository” button. And your remote repository is ready! First of all you need to copy you remote repository to your local repository. Copy the http address and you can copy the repository using git protocol, like this:
$ git clone https://github.com/Pensu/test.git Cloning into 'test'... warning: You appear to have cloned an empty repository.
So, there appears to be a warning, but we already know that as we haven’t added anything to the repository! Now you will see a directory by the name of remote repository in your current directory. That is your local repository. To verify try this on your system:
$ cd test/ $ git remote -v origin https://github.com/Pensu/test.git (fetch) origin https://github.com/Pensu/test.git (push)
Here fetch and push are the repositories where your local repository gets the data from and where it sends the changes respectively. As I just cloned a remote repository, I can fetch and push from this repository. Now, we are going to add a file and pushing it to the remote repository.
$ touch newfile.txt $ echo "Getting Started With Git" > newfile.txt $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # newfile.txt nothing added to commit but untracked files present (use "git add" to track)
I created a new file, echoed some content and I checked the status of my repository. Git can track what new files have been added and what files have changed using checksum. So, it shows me that my “newfile.txt” is untracked. Before committing any changes to the repositories, we need to add the files/changes to Git.
$ git add newfile.txt $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: newfile.txt #
As you can see the file is ready to be committed now. Let’s do it:
$ git commit -m "Added new file" [master (root-commit) 0c53cdc] Added new file 1 file changed, 1 insertion(+) create mode 100644 newfile.txt
Here -m denotes the message which you will need to remember the changes you made in this commit. I am adding newfile.txt here, so the message says that a new file has been added. You local repository is fully updated now, with all the changes. If you want to check, what all changes you have made to the local repository before pushing it to remote repository, just run “git log”.
$ git log commit 0c53cdc27eda90c67219ba15ff8457f337c4aa14 Author: Peeyush Gupta <email@example.com> Date: Tue Apr 1 16:37:59 2014 +0530 Added new file
It will show you the commit checksum, author name/email, date and time of the commit and the message you provided at the time of commit. Well, it seems alright, so now we are gonna push the changes to remote repository.
$ git push origin master Username for 'https://github.com': Pensu Password for 'https://Pensu@github.com': Counting objects: 3, done. Writing objects: 100% (3/3), 249 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/Pensu/test.git * [new branch] master -> master
So, what’s origin and master? Here master is the branch where I am working on my local repository and origin is the branch where master is going to be merged at the remote repository. Wait, what is a branch? Branch is basically the context that you are working in. You can have many branches in the same project and merge them in the remote repository branches. For our purpose we only need one branch and that is the master one. You can verify it using:
$ git branch * master
Now, as it’s all done and dusted, time to check the remote repository if the changes were successfully committed. Open up your browser and go to your repository, you should see a newfile.txt there.
Git is a very huge topic, but this article should get you started to clone remote repositories and push the changes. Happy sharing!