First questions that come to mind when anyone mentions Git is “Why to use Git?”, “Is it that important to use?”, “How does it fit into my day to day coding activity?”. The answer one usually gets is that it is a version control system. So?? Next obvious comment is “Ya, so what do I have to do with it? Is it not something that only corporate organizations need to worry about?” This post is going to throw some light into this and give an overall idea about git and how to set up the local environment for using git.
Why to use a version control system?
Main job of a version control systems is to record changes made to files, or a set of files over time and recall or backtrack them whenever required. It also means that if you screw over files or code, you can easily recover to a previous working version of the code.
It is particularly useful where there are more than two developers working on a piece of code. Fir example, the below two images depict, in a pictorial format the commits and branches created a project, respectively:-
This helps in visualizing how the project is evolving and different branches, commits, etc. which will be covered in detail in the next posts.
In general, version control will be useful if you-
- Made a change to code and need to revert back to a known good state
- Have to maintain multiple versions of the software
- Wanted to see the difference between two revisions
- Wanted to test if a particular version is broken or fixed
- Wanted to see how long a bug existed for
- Wanted to experiment without interfering with the working code
Popular version control systems are Git, SVN (subversion), Mercurial, TFS (Team Foundation Server), etc.
Git version control system
Git is a distributed version control and file management system. This is a very powerful feature when there is a project involving more than one developer. Every developer has a copy of the project on his local machine. This also means that unlike SVN, commands to git are very fast and will work offline too since all operations are done locally. Another very powerful feature of Git is ‘track changes’ and ‘merge’. As developers are modifying code – they are adding, deleting, moving files, it becomes very important to track these changes, merge changes and resolve merge-conflicts, if any.
Working directory is where you write your code. As you are done with some part of code, you reach to a point where you want to take a snapshot. This is called commit. So basically, commit is a snapshot you can refer back to. Commit is also a place where you can revert the code back to. In order to do this, you want to promote all the changes that you want to save to a staging area. This staging area is called Index (or cache). This is where Git will look to take a snapshot. So basically during a commit, you tell Git that, everything at the staging area, I want you to take a snapshot of. Once you have all your files in the staging area, you execute a commit command. That commit command will save a snapshot to your local repository. Once that is done, the staging area is wiped clean. So you follow these steps every single time you want to have a snapshot of your code. Once you have that snapshot, you can always revert to that snapshot, or look at differences between two snapshots. The git repository is located in your source code folder.
Installing Git client
- Open your web browser
- Git Scm is the official website
- Type Git Scm in Google search and open the first link
- Go here and download Git Bash, which is the command line application
- Install with the defaults
- The Git Client is necessary because everything is done locally on your machine. You get to choose what you want to share and you are dependent on a centralized server.
Next step is to do some configuration with Git Bash
- Open Git Bash
- Type in ls (list all files in a directory)
This will give you all the folders in the directory it is currently located
- Check where it is looking at using pwd (present working directory)
- Changing default path when Git Bash opens up to our working directory so that it always opens up with it
When Git Bash starts, it starts pointing to the home directory, from you we would need to navigate to the folder where you have the project that you want to version control with Git. However, there may be instances where developers have some folder (say, called, ‘workspace’) where they always create projects and want to avoid the time wasted to navigate to this folder from the home directory. No big deal, just change the default path.
–Right click on Git Bash –> Properties
Change the highlighted part to the directory where you want it to point to
- Configuring Git Username
–Open Git bash and type in
git config —global user.name “<name>”
- Configuring email
git config —global user.email “<email>”
The above two settings will now always be recorded with a commit. There is one more setting with respect to using GitHub along with Git, where you can create a SSH key and store in your local machine so that you no longer need to type in the credentials each time you push to a remote repository. We will see this step in the coming posts.
This is the end of configuration. In the next post we will create a project and start version controlling it, and will push it to GitHub.