Setting up Git as your Version Control System
Version control is an essential task to be performed not only on coding projects but also in any creative process such as writing or designing, and becomes even more important when it is a collaborative process. Git is a Distributed Version Control System that has become very popular not only in Open Source projects, but also in many other types of coding projects. Its versatility and processing speed are turning it into a must-have tool in web development. However, its documentation (as with any other software tool) can be rather difficult, even obscure at times, to read and assimilate.
The first step is to install Git. You can get the installer for your operating system at Git’s homepage. If you are on Windows, the .exe installer will give you a couple of options and will install several tools as the Git Bash. The Git Bash is a command prompt to run Git that can run other Unix commands as well.
In Mac OSX, if you have installed Xcode, then you already have Git. If not, there are some .dmg files on Git’s homepage corresponding to the latest versions of OSX. With this installed, you are ready to start tracking versions of your local projects’ files
- To start doing this, just go to directory where your files to be tracked reside, using the bash in OSX or the Git Bash in Windows, and type the following command:
- The command prompt should respond with a confirmation message like the following:
Initialized empty Git repository in /path/to/your/project/.git/
This will create a hidden .git folder that will be used to keep track of changes in your directory.
- Then just add the folders and files you want to track and, when ready to do your first commit, return to the bash and use the following command:
This will output a list of current changes such as untracked files, modified files and deleted files.
You can then add the changes you want to commit to your staging area, also called index, by using the command:
git add file1 file2 file3
Then you can effectively create a new revision of your folder by committing what’s on the index:
git commit –m “This is a message summarizing the contents of this commit”
Then you can check the history of your project by using the following command:
The output should be something like this:
commit 3692dff96ec63a66b379a4fa90c08de45f7dfa4f Author: dvergara <firstname.lastname@example.org> Date: Mon Feb 6 19:33:50 2012 -0500 Third version, second on this branch
commit a84128fbab6e5cb791660b082bb0a38ab46987ab Author: dvergara <email@example.com> Date: Mon Feb 6 18:24:48 2012 -0500 version 2
commit 4acb6bf8f245dfa471ea1a4cdb176941aa798494 Author: dvergara <firstname.lastname@example.org> Date: Mon Feb 6 18:07:00 2012 -0500 Initial Commit
Finally, you can see the commit key in case you want to change to a different revision. Let’s say we want to go back to the first commit. We need to execute:
git checkout 4acb6bf8f245dfa471ea1a4cdb176941aa798494
Note that I’m using the SHA1 key of the first commit as its identifier. It also works with just the initial characters of the string. Now we are on the state of the initial commit of out project. There we can perform changes and create a new commit, or just get back to the latest commit with the following command:
git checkout master
These few steps are the basics of what you need to know to start working with Git. In a future post, I will explain more about one of Git’s most interesting features: branches. Play around with them and start leveraging the advantages of this versioning system on your next project.