Appendix B
Version Control
B.1 Github.com . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
B.2 Cloning a Repository and Modifying a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
B.3 Adding Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
B.4 Revising a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
A complex program cannot be finished in a single day. Furthermore, it is usually released
to customers in stages by creating different versions of the same program. Version control
is a method keeping track of all of the different versions of the many files used in a software
project. Version control can back up files and can also manage the files written by a team
of people. Many tools for version control have been developed, for example: CVS, SVN,
mercurial, SourceSafe, and git.
B.1 Github.com
This book uses github.com because it is a popular web site that offers free version
control service for students and teachers. After creating an account, use the web interface
to create a new repository (also called repo).
B.2 Cloning a Repository and Modifying a File
After creating a repository, start a terminal in Linux and type:
$ git clone https://account:password@github.com/.../demorepo.git
Here account is your account name and password is your password. Replace github.
com/.../demorepo.git with the correct path for your repository. You will see something
like the following on the Linux terminal:
Cloning into ’demorepo’...
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
Type the ls command in terminal and you can see the demorepo directory and the
README file. There is also a hidden file called .gitignore. When a file begins with a period,
447
448 Intermediate C Programming
FIGURE B.1: Create a new repository. In this example, I call it “demorepo”. As a teacher,
I can create a free private repository. Check the box of “Initialize this repository with a
README”. Add .gitignore for C. This will ignore files that are not supposed to be in the
repository. Click “Create repository”.
the file is hidden. To see hidden files, please type ls -a. Use your preferred text editor to
add one line into README. Type the git diff command and the difference is shown between
the edited file, and the version of the file before changes. In particular, the line just added
appears with the “+” in front of it. Type this command to commit the change:
$ git commit README.md
Please give a meaningful comment. The comment becomes a part of the history in the
repository. Meaningful comments are required when working with a team. Comments help
document the progression of the program, and also make it easier to track old versions of
files. After committing the changes, the new version is stored. You can see the history using
this command:
$ git log
Creating a new version allows you to roll back to previous versions when necessary.
However, if your computer is broken, then you will lose everything in the computer. To
protect your programs, you want to keep another copy outside of your computer. You can
use github.com to store the repository using this command:
$ git push
If you are the only person working on the project and you have only one computer,
you can push right after commit. If you are working on a team project or you have several
computers, you should do
Version Control 449
$ git pull
before push to ensure that you have the latest version before pushing. Now you can go to
github.com and see the history of the repository in github.com.
B.3 Adding Files and Directories
You can add files and directories by using the git add command. The following com-
mands add a file (called prog1.c) to the repository and push it to github.com.
$ git add prog1.c
$ git commit -m ”add a program” prog1.c
$ git push
B.4 Revising a Program
Version control is designed to keep track of changes. Try adding two lines to prog1.c
and then type the following commands:
$ git commit -m ”added two lines” prog1.c
$ git push
Now remove some lines and type the following commands:
$ git commit -m ”changed some lines” prog1.c
$ git push
The history of changes can be seen at github.com. If a line is added, then a “+” sign
appears in front of the line. If a line is deleted, then a sign appears in front of the
line. Using git, you can keep track of changes line-by-line. It is necessary to take small
steps to build a complex program. A professional programmer adds one function at a time,
makes sure it works, and commits a new version before adding another function. Sometimes,
the functions written earlier require improvement. Perhaps the function no longer works as
expected due to some other changes. If you do not use version control, you are out of luck.
It is difficult to remember what changes have been made. Version control can show which
lines have been changed since the previous commit, and thus saves a lot of time. Version
control is very helpful when you learn a new programming language. You can change your
programs without the fear of losing previously working functions. If you make a mistake,
you can easily roll back to an earlier version. Version control can help only if you commit
often. It is quite reasonable if you commit every hour, or even every few minutes. If you work
in a team, you may need to create a branch so that you can commit incomplete functions
without affecting the other people. You can find many tutorials on the Internet about how
to use git. Spend some time learning git and you can save a lot of time managing your
software projects.
This page intentionally left blankThis page intentionally left blank
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset