Skip to content

Commit f32ad24

Browse files
Ben LynnBen Lynn
authored andcommitted
More detail in second chapter
Added outline of other chapters and other helper files
1 parent b1b1103 commit f32ad24

File tree

10 files changed

+290
-40
lines changed

10 files changed

+290
-40
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
target: book.html
1+
.PHONY: target clean
22

3-
TXTFILES=intro.txt basic.txt
3+
target: book book.html
4+
5+
TXTFILES=intro.txt basic.txt clone.txt branch.txt
46

57
book.xml: $(TXTFILES)
6-
./bookmake $^ > book.xml
8+
cat $^ | sed 's/<tt>/<command>/g' | sed 's/<\/tt>/<\/command>/g' | ./bookmake > book.xml
79

810
book: book.xml
911
xmlto -m custom-html.xsl -o book html book.xml
1012
-ls book/*.html | xargs -n 1 tidy -utf8 -m -i -q
13+
./makeover
1114

1215
book.html: book.xml
1316
xmlto -m custom-nochunks.xsl html-nochunks $^

basic.txt

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,71 @@
1-
= Basic Git Tricks =
1+
= Basic Tricks =
2+
3+
Rather than diving into a sea of Git commands, use these elementary examples to get your feet wet. Despite their simplicity, each of them are useful in real life.
24

35
== Instant Backup ==
46

57
When I'm about to attempt something drastic I like to save the current state, so I can go back and try again should things go awry.
68

79
$ git-init
810
$ git-add .
9-
$ git-commit -m "Initial commit"
11+
$ git-commit -m "my first backup"
1012

1113
to take a snapshot of all files in the current directory.
1214
Then if something goes wrong type
1315

14-
$ git-reset --hard
15-
16-
to go back to where you were. To save the state again, type <command>git-commit -a</command> and provide a description.
16+
$ git-checkout HEAD .
1717

18-
One benefit to doing this instead of simply copying files is that with Git's hash chaining, you can tell if a backup gets corrupted.
18+
to go back to where you were. To save the state again, you can type
1919

20-
== Undo/Redo History ==
20+
$ git-commit -a -m "another backup"
2121

22-
More generally, you can do the above, and every so often "save the game" by typing
22+
=== Adding, Deleting, Renaming Files ===
2323

24-
$ git-commit -a -m "description of current state"
24+
The above will only keep track of the files that were present when you first ran <tt>git-add</tt>. If you add new files to the directory, you'll have to tell Git:
2525

26-
Note if you want to keep track of newly added files or forget about deleted files you'll need to first run <command>git-add</command> or <command>git-delete</command> accordingly.
26+
$ git-add NEWFILES...
2727

28-
Typing git-log shows you a list of recent commits, and their SHA1 hashes. Then typing
28+
Similarly, if you want Git to forget about certain files, maybe because you've deleted them
2929

30-
$ git-commit -a
31-
$ git-revert SHA1_HASH
30+
$ git-rm OLDFILES...
3231

33-
will restore the state to the commit with the given hash. You might like to use something like the following instead:
32+
Renaming a file is the same as removing the old name and adding the new name. There's also the shortcut <tt>git-mv</tt> which has the same syntax as the <tt>mv</tt> command. For example:
3433

35-
$ git-revert "@{10 minutes ago}"
34+
$ git-mv OLDFILE NEWFILE
3635

37-
You can undo the undo: type git-log and you'll see that the other commits you made are still there.
36+
== Advanced Undo/Redo ==
3837

39-
Typing
38+
Typing <tt>git-log</tt> shows you a list of recent commits, and their SHA1 hashes. Then typing:
4039

4140
$ git-checkout SHA1_HASH .
4241

43-
loads a saved state without recording the fact that you've gone back to an old state. This is sometimes what you want.
42+
will load the previous state with the given hash.
43+
Don't like working with hashes? Then use:
44+
45+
$ git-checkout "@{10 minutes ago}" .
46+
47+
Other time specifications work too. Or you can ask for the 5th-last saved state:
48+
49+
$ git-checkout "@{5}" .
50+
51+
In some circumstances, it is preferable to type:
52+
53+
$ git-commit -a
54+
$ git-revert SHA1_HASH
55+
56+
This appears to have the same affect, but <tt>git-log</tt> reveals that the fact that you loaded an old saved state has been recorded as new commit. In other words, you can have Git track you when you undo and redo.
57+
58+
Lastly, other times you might want:
59+
60+
$ git-reset --hard SHA1_HASH
61+
62+
which restores the state to a given commit but also erases all newer commits from the record permanently.
4463

45-
To take the computer game analogy again, git-checkout is like loading a game, git-revert is like loading a game and recording this fact as another saved game, and git-reset --hard is like loading an old save and deleting all saved games newer than the one just loaded.
64+
To take the computer game analogy again, <tt>git-checkout</tt> is like loading a game, <tt>git-revert</tt> is like loading a game and recording this fact as another saved game, and <tt>git-reset --hard</tt> is like loading an old save and deleting all saved games newer than the one just loaded.
4665

47-
== Synchronize Files Between Computers ==
66+
== Sync Computers ==
4867

49-
This is the reason I first used Git. I can make tarballs or use rsync to do backups. The problem was sometimes I'd edit on my laptop, other times on my desktop, and they may not have talked to each other in between.
68+
This is the reason I first used Git. I can tolerate making tarballs or using <tt>rsync</tt> for backups. The problem was sometimes I'd edit on my laptop, other times on my desktop, and they may not have talked to each other in between.
5069

5170
Initialize a Git repository and commit your files as above on one machine. Then on the other:
5271

bookmake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ echo '<?xml version="1.0" encoding="utf-8"?>
1010
<copyright><year>2007</year><holder>Ben Lynn</holder></copyright>
1111
</bookinfo>'
1212

13-
for FILE in $*; do
14-
./wiki2xml $FILE
15-
done
13+
if [ $# -gt 0 ]; then
14+
for FILE in $*; do
15+
./wiki2xml $FILE
16+
done
17+
else
18+
./wiki2xml
19+
fi
20+
1621
echo '</book>'

branch.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
= Branch Magic =
2+
Cheap Context Switching
3+
4+
== The Boss Key ==
5+
6+
Ever play one of those games where you could hit a special key combination at
7+
any time, and the screen would instantly display a spreadsheet or something? So if the boss walked in the office while you were playing the game you could quickly hide this fact?
8+
9+
In some directory, edit a text file and write "I'm smarter than my boss".
10+
Create a Git repository, that is, <tt>git-init ; git-add . ; git-commit -m "Initial commit"</tt>. Then type
11+
12+
$ git checkout -b boss
13+
14+
Edit the text file to say "My boss is smarter than me", and type <tt>git-commit -a</tt>. Now you can switch between the two versions of the file with
15+
16+
$ git branch master # see original version of the file
17+
18+
and
19+
20+
$ git branch boss # see version of the file suitable for boss' eyes
21+
22+
One can imagine reasons to use this that have nothing to do with source code management. Perhaps you have a program that reads data from a certain directory, and every now and then you'd like to switch the data back and forth without reconfiguring the program.
23+
24+
== Dirty Work ==
25+
26+
TODO
27+
28+
== Quick Fixes ==
29+
30+
TODO
31+
32+
== Working While Being Reviewed ==
33+
34+
Some projects require your code to be reviewed before you can submit it. To make life easier for those reviewing your code, if you have a big change to make you might break it into two or more parts, and get each parts separately reviewed.
35+
36+
What if the second part cannot be written until the first part is approved and checked in? In many version control systems, you'd have to send the first part to the reviewers, and then wait until it has been approved before starting on the second part.
37+
38+
Actually that's not quite true, but in many systems editing part 2 before part 1 had been submitted involves a lot of suffering and hardship. In Git, branching and merging are painless. So after you've committed the first part and sent it for review:
39+
40+
$ git checkout -b part2
41+
42+
Next, code the second part of the big change while you're waiting for the first part to be accepted. When the first part is approved and submitted,
43+
44+
$ git branch master
45+
$ git merge part2
46+
$ git branch -d part2
47+
48+
and the second part of the change is ready to review.
49+
50+
But wait! What if it wasn't that simple? Say you made a mistake in the first part, which you have to correct before submitting. No problem! First, switch back to the master branch with <tt>git branch master</tt>. Fix the issue with the first part of the change and hope it gets approved. If not we simply repeat this step.
51+
52+
Eventually, once the first part has been approved and submitted:
53+
54+
$ git merge part2
55+
56+
and again, the second part is ready to be reviewed.

clone.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
= Cloning Around =
2+
3+
== Classic Source Control ==
4+
5+
Copy your project to a directory in your main server. Initialize a Git
6+
repository: <tt>git init ; git add . ; git commit -m "Initial commit"</tt>.
7+
8+
To check out source, a developer types
9+
10+
$ git clone git+ssh://main.server/directory
11+
12+
After making changes, the code is checked in to the main server by:
13+
14+
$ git commit -a
15+
$ git push
16+
17+
If the main server has been updated, the latest version needs to be checked out before the push. To sync to the latest version:
18+
19+
$ git commit -a
20+
$ git pull
21+
22+
== Forking a Project ==
23+
24+
Sick of the way a project is being run? Think you could do a better job?
25+
26+
First, on your server:
27+
28+
$ git clone git+ssh://main.server/directory
29+
30+
Then tell everyone to check out your fork of the project at your server.
31+
32+
At any later time, you can merge in the changes from the original project with:
33+
34+
$ git pull
35+
36+
== Ultimate Backups ==
37+
38+
How would you like multiple tamper-proof geographically diverse redundant archives?
39+
40+
TODO
41+
42+
== Guerilla Version Control ==
43+
44+
TODO
45+
46+
== Working On Features In Parallel ==
47+
48+
TODO
49+
50+
== Source Control Engine Tools and Utilities ==
51+
52+
TODO

find_selflink.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// From my own website(!)
2+
//TODO: only do this for links in the table of contents menu
3+
4+
function find_selflink() {
5+
var a = document.links;
6+
var i = 0;
7+
while (i < a.length) {
8+
if (a[i].href == document.URL) {
9+
var c;
10+
var j;
11+
var s_new = document.createElement("span");
12+
s_new.className = "currentlink";
13+
c = a[i].childNodes;
14+
for (j=0; j<c.length; j++) {
15+
s_new.appendChild(c[j]);
16+
}
17+
a[i].parentNode.replaceChild(s_new, a[i]);
18+
} else {
19+
i++;
20+
}
21+
22+
/*
23+
if (a[i].href == document.URL) {
24+
a[i].className = "currentlink";
25+
if (0) {
26+
var s_new = document.createElement("span");
27+
s_new.className = "currentlink";
28+
s_new.appendChild(a[i]);
29+
a[i].parentNode.replaceChild(s_new, a[i]);
30+
}
31+
}
32+
i++;
33+
*/
34+
}
35+
}
36+
37+
find_selflink();

intro.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
= Introduction to Version Control =
1+
= Introduction =
22

33
I'll use an analogy to introduce version control, also termed revision control, source control, or source code management.
44
See [[http://en.wikipedia.org/wiki/Revision_control][the Wikipedia entry on revision control]] for a normal explanation.
55

66
== Work is Play ==
77

8-
I've played computer games almost all my life. In contrast only started using version control systems as an adult. Not surprisingly, I'm a lot more familiar with video game concepts than version control ones. I'm certain I'm not alone.
8+
I've played computer games almost all my life. In contrast I only started using version control systems as an adult. I suspect I'm not alone, and comparing the two may make version control easier to explain and understand.
99

1010
Think of editing your document, or your code, or whatever, as playing a game. Once you've made a lot of progress, you'd like to save. To do so, you click on the "Save" button in your trusty editor.
1111

12-
But this will overwrite the old version. It's like those old school games which only had one save slot: sure you could save, but you could never go back to an older state. Which was a shame, because your previous save might have been right at a really fun part of the game that you'd like to revisit one day.
12+
But this will overwrite the old version. It's like those old school games which only had one save slot: sure you could save, but you could never go back to an older state. Which was a shame, because your previous save might have been right at a really fun part of the game that you'd like to revisit one day. Or worse still, your current save is in an unwinnable state, which means you have to start again.
1313

1414
== Version Control ==
1515

16-
You can "Save As..." a different file, or copy the old one somewhere first before saving if you want to savour old versions. Maybe compress them too to save space. We have just described a primitive labour-intensive form of version control.
16+
When editing, you can "Save As..." a different file, or copy the file somewhere first before saving if you want to savour old versions. Maybe compress them too to save space. This is version control, but very primitive and labour-intensive.
1717

18-
Let's make the problem slightly tougher now. Say you have a bunch of files that go together, such as collection of source code for the same project, or files for a website. Now if you want to keep an old version you have to copy a whole directory of stuff. Keeping many versions around is inconvenient to do by hand.
18+
Let's make the problem slightly tougher now. Say you have a bunch of files that go together, such as collection of source code for the same project, or files for a website. Now if you want to keep an old version you have to archive a whole directory of stuff. Keeping many versions around is inconvenient to do by hand, and gets expensive fast.
1919

2020
In some computer games, saving the game actually writes a directory full of files. They hide these details from the player and present a convenient interface for you to manage saves.
2121

@@ -29,13 +29,13 @@ How would you set up a system so they can get at each other's saves easily? And
2929

3030
In the old days, every project used centralized version control. A server somewhere held all the saved games. Nobody else did. Every player kept at most a few saved games on their machine. When a player wanted to make progress, they'd download the latest save from the main server, play a while, save and upload back to the server for everyone else to use.
3131

32-
What if a player wanted to get an older saved game for any reason? Maybe the current saved game is in an unfinishable state because somebody forgot to pick up an object back in level three, and they want to find the latest saved game where the game still can completed. Or maybe they want to compare two older saved games to see how much work a particular player did.
32+
What if a player wanted to get an older saved game for some reason? Maybe the current saved game is in an unwinnable state because somebody forgot to pick up an object back in level three, and they want to find the latest saved game where the game can still be completed. Or maybe they want to compare two older saved games to see how much work a particular player did.
3333

34-
There could be many reasons to want to see an older revision, but the outcome is the same. They have to ask the central server for that old save. The more saves they want, the more communication that is required.
34+
There could be many reasons to want to see an older revision, but the outcome is the same. They have to ask the central server for that old saved game. The more saved games they want, the more communication that is required.
3535

36-
The new generation of version control systems, to which Git belong, are known as distributed systems, and can be thought of as a generalization of the centralized systems. When players download from the main server they get every saved game, not just the latest one. It's as if they're mirroring the central server.
36+
The new generation of version control systems, of which Git is a member, are known as distributed systems, and can be thought of as a generalization of centralized systems. When players download from the main server they get every saved game, not just the latest one. It's as if they're mirroring the central server.
3737

38-
One immediately obvious benefit is that when an old save is desired for any reason, communication with the central server is unnecessary.
38+
Thus the initial cloning can be expensive, especially if there's a long history, but it pays off in the long run. One immediately obvious benefit is that when an old save is desired for any reason, communication with the central server is unnecessary.
3939

4040
== Merge Conflicts ==
4141

makeover

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# extract table of contents from index.html
4+
BOOKDIR=book
5+
gawk '
6+
/<div class="toc">/ {
7+
print $0
8+
getline #TODO: check this is the <ul> line
9+
print $0
10+
print "<li><a href=\".\">Git Magic</a></li>"
11+
getline
12+
while (!match($0, "</div>")) {
13+
print $0
14+
getline
15+
}
16+
print "</div>"
17+
exit
18+
}
19+
' < $BOOKDIR/index.html > toc.tmp
20+
21+
# for every chapter...
22+
for FILE in $BOOKDIR/*.html
23+
do
24+
if [ $FILE != "$BOOKDIR/index.html" ]
25+
then
26+
# add " - Git Magic" to titles of all pages
27+
sed '/<\/title>/ s/<\/title>/ - Git Magic&/' -i $FILE
28+
# paste ToC into beginning
29+
# and add div section with class content for CSS
30+
sed '/<body/{n; r toc.tmp
31+
a <div class="content">
32+
} ' -i $FILE
33+
sed '/^<\/body/i </div>' -i $FILE
34+
fi
35+
done
36+
37+
# extract shell of index.html
38+
# then insert ToC and preface
39+
gawk '
40+
/<div class="book"/ {
41+
i = 0
42+
for(;;) {
43+
getline
44+
if (match($0, "<div")) i++;
45+
else if (match($0, "</div")) {
46+
i--;
47+
if (i < 0) break;
48+
}
49+
}
50+
sub("</div>","")
51+
}
52+
{ print }
53+
' < $BOOKDIR/index.html | sed '/<body/{n; r toc.tmp
54+
a <div class="content">
55+
r preface.html
56+
a </div>
57+
} ' > tmp.tmp
58+
mv tmp.tmp $BOOKDIR/index.html
59+
rm toc.tmp

preface.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h1>Git Magic</h1>
2+
<p>
3+
Git is a version control Swiss army knife. The duct tape of source code management. A reliable versatile multipurpose tool for all your revision control needs.
4+
The tricky part is learning to use it. I'm recording what I've figured out so far in these pages.
5+
</p>
6+
<p>
7+
Rather than explain how it works, I'll provide recipes for particular tasks. After practice, you eventually figure out what's going on behind each trick.
8+
</p>
9+
<p>
10+
<a href="/~blynn/">Ben Lynn</a>
11+
</p>

0 commit comments

Comments
 (0)