| Use Git to Document and Manage Any Source Code with Version Control |
|
|
|
| Operating Systems - Linux / Open Source | |||||
| Written by Thomas Snyder | |||||
| Wednesday, 25 April 2012 00:00 | |||||
|
Provide versioning and rollback capabilities to your source code with the resolution of individual code modifications.
Programmers in any language have supported multiple versions of source code during development, the simplest case being that you can keep a copy of the current production code while you work on a new version that contains your current development changes. But what if you wanted to document each change and have the ability to selectively reset any changes while supporting multiple developers working on the same source code? Git can help with this. The Difference Between Git and GitHubWhen you hear about Git, you also typically hear about GitHub. Git and GitHub are two separate things:
This article covers the Git software using a local repository; my next article will cover GitHub and other remote repository options. So please tune in for the follow-up article. Source Code Versioning and Commitment ControlLast month, I wrote an article called "Update Your Data with Piece of Mind Using Commitment Control." It showed a sample of using commitment control within RPG to ensure that a series of database transactions are committed properly. This was done using journaling, which allows one or more database changes to be committed or rolled back.
The Git software uses the same type of philosophy in committing changes to the repository. You can commit or roll back changes during the process of changing your code. Installing GitGo to the official Web site, http://git-scm.com/, and download the latest stable version of the Git software for the operating system that you are using. For this article, I'll be using the Windows 7 operating system, but you could just as easily install on a Mac or UNIX/Linux. As of the time this article is being written, the executable file name is Git-1.7.9-preview20120201.exe. Depending upon the operating system and version you are using, the name may be different.
On Windows, you'll see a standard installation screen where you just click on Next using the defaults. There are a few screens that you may want to review, but here are some of the settings that I've used.
On my Windows machine, I like to use Git from the command line, so I opted to let the installation program modify my PATH to take care of this for me.
Git will most likely be using SSH when you're sharing your projects with other developers. I have the Putty software installed on my computer, which was detected by the install. But I chose to use the OpenSSH software that comes with Git.
Figure 2: Use OpenSSH.
The final setting that I found deserving of attention is the line-ending conversions. I'm using a Windows machine to develop for a Linux environment, so I opted to use the Checkout Windows/ Commit Unix line-ending option.
Hands-On GitNow that you've installed Git, I'll demonstrate how to use it by creating a Git repository that will support all changes in a new MCPressOnline directory off the root of the C: drive.
Start the Windows command prompt and navigate to the directory that you will use version control within. Once you're in this folder, initiate Git using the git init command:
You will see a message displayed indicating that an empty repository has been initialized. You can see that a physical directory has been created by executing the dir /a command (The /a is used to display the newly created .git folder because it is hidden).
Viewing the Status of Your ChangesThe status command is a very useful option to see a list of files that have changed since your last commit. If you execute git status at this point, it will indicate that the repository has just been initiated and there are no changes to commit.
Let's create a simple HTML file within the C:\MCPressOnline folder with the editor of choice as follows:
After you have created and saved the file into the MCPressOnline folder, you can execute the git status command to see if the changes have been identified by git.
You can see that the file has been identified, but it is not being tracked. The comments are helpful with git; to track the file execute git add. After adding the file, you can run another status command to see that it is now being tracked.
You will want to configure git to have your user name and email address so that you can identify who made the changes and how to contact them if you're sharing code. You set these configuration settings as follows:
git config --global user.name 'Your Name' git config --global user.email you@example.com
Committing the ChangesUp to this point, we have started recording the changes and configured git, but we have not yet committed the changes to the repository. To commit the changes, use the commit option. You will most likely want to provide a comment describing any changes you've made since the last commit; you can do this using the -m option with commit.
Comparing VersionsWe now have a committed git repository that could be used to initiate a collaborative development effort, which will be discussed in the next article. To continue in a single developer environment, let's change our HTML to turn our URL into a hyperlink on the Web page, as follows:
To view the changes that have occurred since the last commit, you can use the diff option. This option will list all of the changed lines, as follows:
After you save the file, you can execute the git status command again and see that the welcome.html is now noted as modified, but there is nothing staged to commit. This is because you need to add the file again. Every time you capture a snapshot of the data, you need to add the file to git.
If you add the file using git add welcome.html and run the git diff command again, you will see that there are no listed differences. This is because the changes have been added to the git repository and are staged for a commit. To view the differences now, you can use git diff –cached. Reviewing the Change LogLet's commit our latest changes using the following command:
git commit -m "Added HyperLink"
Now, to see a historical overview of the changes we have made, we can view the log using the following command:
git log
We'll get the following output:
The history log lists all of the changes along with the comments that were added during the commits. Rolling Back ChangesAs with commitment control, you can also roll back changes that have not been committed by using the checkout option. To demonstrate this, modify the welcome.html file to add some additional text as follows:
Close the file that you're working on from your editor. Then execute the following command:
git checkout welcome.html
Now reopen your welcome.html file in your text editor. You'll see that the additional text has been removed; the changes have been rolled back. This is because you checked out the last committed version. Coming Soon: Multiple DevelopersThis article discussed some basic operations of using git for source code version control for a single user. In my next article, I'll discuss how to use git with multiple developers. ReferencesGit—Free, open-source, distributed version control system
Git Reference—A quick reference for learning and remembering the most important and commonly used git commands.
Windows Dir Syntax at Microsoft.com—Dir command syntax
Update Your Data with Peace of Mind Using Commitment Control—Using commitment control within RPG | |||||
View all articles by this author
|
|||||
| Last Updated on Wednesday, 25 April 2012 00:00 |




















