23
Sat, Nov
1 New Articles

Use Git to Document and Manage Any Source Code with Version Control

Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 GitHub

When you hear about Git, you also typically hear about GitHub. Git and GitHub are two separate things:

  • Git is the client that you use on your machine to perform the version control with the code that you're working on. It provides a means of working with multiple developers. Git was initially developed by Linus Torvalds, who is also the creator of Linux.
  • GitHub can be used as the central repository where multiple developers can share their code and contribute remotely. GitHub is a popular repository for this purpose, but it's not the only repository available.

 

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 Control

Last 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 Git

Go 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.

 

042512Snyder_LinuxGit_fig01
Figure 1: Run Git from the Windows command prompt.

 

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.

 

042512Snyder_LinuxGit_fig02

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.

 

042512Snyder_LinuxGit_fig03
Figure 3: I chose Windows Checkout, Unix Commit.

Hands-On Git

Now 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:

 

042512Snyder_LinuxGit_fig04
Figure 4: Initialize the git repository.

 

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).

 

042512Snyder_LinuxGit_fig05
Figure 5: The git repository has a hidden directory.

Viewing the Status of Your Changes

The 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:

 

042512Snyder_LinuxGit_fig06
Figure 6: The git repository uses a simple HTML file.

 

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.

 

042512Snyder_LinuxGit_fig07
Figure 7: Here's the git status before the add.

 

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.

 

042512Snyder_LinuxGit_fig08
Figure 8: Here's the git status after the add.

 

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 This email address is being protected from spambots. You need JavaScript enabled to view it.

 

Committing the Changes

Up 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.

 

042512Snyder_LinuxGit_fig09
Figure 9: Commit!

Comparing Versions

We 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:

 

042512Snyder_LinuxGit_fig10
Figure 10: The simple HTML file is modified to add a hyperlink.

 

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:

 

042512Snyder_LinuxGit_fig11
Figure 11: Here are the git differences.

 

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 Log

Let'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:

 

042512Snyder_LinuxGit_fig12
Figure 12: View the git log to see changes.

 

The history log lists all of the changes along with the comments that were added during the commits.

Rolling Back Changes

As 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:

 

042512Snyder_LinuxGit_fig13
Figure 13: This is the simple HTML file before rollback/checkout.

 

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 Developers

This 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.

References

Git—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

Thomas Snyder

Thomas Snyder has a diverse spectrum of programming experience encompassing IBM technologies, open source, Apple, and Microsoft and using these technologies with applications on the server, on the web, or on mobile devices.

Tom has more than 20 years' experience as a software developer in various environments, primarily in RPG, Java, C#, and PHP. He holds certifications in Java from Sun and PHP from Zend. Prior to software development, Tom worked as a hardware engineer at Intel. He is a proud United States Naval Veteran Submariner who served aboard the USS Whale SSN638 submarine.

Tom is the bestselling author of Advanced, Integrated RPG, which covers the latest programming techniques for RPG ILE and Java to use open-source technologies. His latest book, co-written with Vedish Shah, is Extract, Transform, and Load with SQL Server Integration Services.

Originally from and currently residing in Scranton, Pennsylvania, Tom is currently involved in a mobile application startup company, JoltRabbit LLC.


MC Press books written by Thomas Snyder available now on the MC Press Bookstore.

Advanced, Integrated RPG Advanced, Integrated RPG
See how to take advantage of the latest technologies from within existing RPG applications.
List Price $79.95

Now On Sale

Extract, Transform, and Load with SQL Server Integration Services Extract, Transform, and Load with SQL Server Integration Services
Learn how to implement Microsoft’s SQL Server Integration Services for business applications.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: