|
Variant Management is usually handled by branching the code at a specific point in time. Some folks use branches to plan releases or track features. We will show examples of these approaches in future articles. In this article, we address the most essential branching pattern which is branching from a known baseline (as represented by a tag). For this example, I created a new git repository and simply added one readme file containing a date and time stamp. I tagged the repository at several points to make it easier to understand the example.
[bob@myserver ~]$ mkdir gitexample
[bob@myserver ~]$ cd gitexample
[bob@myserver gitexample]$ git init
Initialized empty Git repository in /home/bob/gitexample/.git/
[bob@myserver gitexample]$ date > readme.txt
[bob@myserver gitexample]$ git add readme.txt
[bob@myserver gitexample]$ git commit -a -m'first commit'
Created initial commit de0d064: first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
[bob@myserver gitexample]$ git tag v1.0
[bob@myserver gitexample]$ git show v1.0
commit de0d0643cf32a032bb31cd9791ec758fb7d03348
Author: Bob Aiello <
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
>
Date: Wed May 30 17:07:23 2012 -0700
first commit
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..184f470
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1 @@
+1. Wed May 30 17:06:55 MST 2012
At this point I have one readme file with a single date stamp that has been labeled v1.0. Next I will add another date stamp, commit the change and then baseline again using a tag called v2.0.
[bob@myserver gitexample]$ date >> readme.txt
[bob@myserver gitexample]$ git commit -a -m'second time'
Created commit 907832f: second time
1 files changed, 1 insertions(+), 0 deletions(-)
[bob@myserver gitexample]$ git tag v2.0
Next we examine (using the git show command) the two tags that were just created – v1.0 and v2.0. Note the cryptographic hash on the commit as shown below.
[bob@myserver gitexample]$ git show v1.0
commit de0d0643cf32a032bb31cd9791ec758fb7d03348
Author: Bob Aiello <
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
>
Date: Wed May 30 17:07:23 2012 -0700
first commit
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..184f470
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1 @@
+1. Wed May 30 17:06:55 MST 2012
[bob@myserver gitexample]$ git show v2.0
commit 907832f7650b5c504ec7c41d3d418f25d27a9e2c
Author: Bob Aiello <
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
>
Date: Wed May 30 17:09:05 2012 -0700
second time
diff --git a/readme.txt b/readme.txt
index 184f470..b861d3f 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
1. Wed May 30 17:06:55 MST 2012
+2. Wed May 30 17:08:40 MST 2012
The branch can be created using the hash associated with v1.0. I verify that the readme.txt file has two date stamps as expected. Then I checkout the branch created based upon the first baseline and verify that the readme file has only one date stamp as expected. We have effectively gotten into our version control time machine and created a branch for our development using the original baseline. This is exactly what you need to do when you are creating a bugfix branch.
[bob@myserver gitexample]$ git branch br1.0 de0d0643cf32a032bb31cd9791ec758fb7d03348
[bob@myserver gitexample]$ cat readme.txt
Wed May 30 17:06:55 MST 2012
Wed May 30 17:08:40 MST 2012
[bob@myserver gitexample]$ git checkout br1.0
Switched to branch "br1.0"
[bob@myserver gitexample]$ cat readme.txt
Wed May 30 17:06:55 MST 2012
Using the cryptographic hash as the starting point helps me to really understand how git works, but it's pretty ugly. Let's try creating branches based upon the tag names which is much easier to read. In the following example, we create branch br-v1.0 based upon tag v1.0. We then checkout the branch that we just created and verify that we only see one datestamp.
[bob@myserver gitexample]$ cat readme.txt
Wed May 30 17:06:55 MST 2012 Wed May 30 17:08:40 MST 2012
[bob@myserver gitexample]$ git branch br-v1.0 v1.0
[bob@myserver gitexample]$ git checkout br-v1.0
Switched to branch "br-v1.0"
[bob@myserver gitexample]$ ls -lt
total 4
-rw-rw-r-- 1 bob bob 32 May 30 18:09 readme.txt
[bob@myserver gitexample]$ cat readme.txt
1. Wed May 30 17:06:55 MST 2012
Next we will create a new branch based upon the v3.0 tag. This time checking out the new branch that we just created shows the readme file with the second datestamp.
[bob@myserver gitexample]$ git branch br-v3.0 v3.0
[bob@myserver gitexample]$ git checkout br-v3.0
Switched to branch "br-v3.0"
[bob@myserver gitexample]$ ls -lt
total 4
-rw-rw-r-- 1 bob bob 64 May 30 18:11 readme.txt
[bob@myserver gitexample]$ cat readme.txt
1. Wed May 30 17:06:55 MST 2012
2. Wed May 30 17:08:40 MST 2012
Git has some pretty cool features and effective use of tags and branches are essential for managing baselines and code variants. Please drop me a line and tell me about your own experiences coming up to speed with Git!
|