Moved Google Analytics code to makeover script
[gitmagic.git] / grandmaster.txt
blobcee7dc9cb1d5090555582cf50cbf30c3ece9b0cd
1 == Git Grandmastery ==
3 This pretentiously named page is my dumping ground for uncategorized Git tricks.
5 === Source Releases ===
7 For my projects, Git tracks exactly the files I'd like to archive and release to users. So to create a tarball of the source code, I run:
9  $ git-archive --format==tar --prefix==proj-1.2.3/ HEAD
11 === Changelog Generation ===
13 It's good practice to keep a http://en.wikipedia.org/wiki/Changelog[changelog], and some projects even require it. If you've been committing frequently, which you should, generate a Changelog by typing
15  $ git-log > ChangeLog
17 === Git Over SSH, HTTP ===
19 Suppose you have ssh access to your web server, and it does not have Git installed, which is not an uncommon situation. Then download, compile and install Git in your account.
21 Create a repository in your web directory:
23  $ GIT_DIR==proj.git git-init
25 and in the "proj.git" directory, run
27  $ git --bare update-server-info
28  $ chmod a+x hooks/post-update
30 From your computer, you can push via ssh:
32  $ git push web.server:/path/to/proj.git master
34 and people can get your project via
36  $ git clone http://web.server/proj.git
38 === Commit What Changed ===
40 Telling Git when you've added, deleted and renamed files gets tedious. Instead, try:
42  $ git-ls-files -d -m -o -z | xargs -0 git-update-index --add --remove
44 and Git will look at the files in the current directory and work everything out for itself. The *-z* and *-0* options prevent ill side-effects from filenames containing strange characters.
46 You might want it to ignore particular files:
48  $ git-ls-files -d -m -o -z -x *.tmp | xargs -0 git-update-index --add --remove
50 If you have a big list of directories and files that should never be version controlled, type them into a separate file named ".gitignore" and run:
52  $ git-ls-files -d -m -o -z -X .gitignore | xargs -0 git-update-index --add --remove
54 You can also exclude files on a per-directory basis. See the help page.
56 === I Stand Corrected ===
58 Did you just commit, but wish you had typed a different message? Realized you forgot to add a file? Then:
60  $ git commit --amend
62 can help you out.
64 Since this changes the history, only do this if you have yet to push your changes, otherwise your tree will diverge from other trees. Of course, if you control all the other trees too, then there is no problem since you can always overwrite them.
66 === ... And Then Some ===
68 Let's suppose the previous problem is ten times worse. After a lengthy session you've made a bunch of commits. But you're not quite happy with the way they're organized, and some of those commit messages could use rewording. This is quite likely if you've been saving early and saving often. Then type
70  $ git rebase -i HEAD~10
72 and the last 10 commits will appear in your favourite $EDITOR. A sample excerpt:
74     pick 5c6eb73 Added repo.or.cz link
75     pick a311a64 Reordered analogies in "Work How You Want"
76     pick 100834f Added push target to Makefile
78 Then:
80 - Remove commits by deleting lines.
81 - Reorder commits by reordering lines.
82 - Replace "pick" with "edit" to mark a commit for amending.
83 - Replace "pick" with "squash" to merge a commit with the previous one.
85 Next run *git-commit --amend* if you marked a commit for editing. Otherwise, run:
87  $ git-rebase --continue
89 Again, only do this if no one else has a clone of your tree.
91 === Local Changes Last ===
93 You're working on an active project. You make some local commits over time, and
94 then you sync with the official tree with a merge. This cycle repeats itself a few times before you're ready to push to the cetnral tree.
96 But now the history in your local Git clone is a messy jumble of your changes and the official changes. You'd prefer to see all your changes in one contiguous section, and after all the official changes.
98 This is a job for *git-rebase* as described above.
100 Also see the manpage for other amazing uses of this command, which really deserves a chapter of its own. You can split commits. You can even rearrange branches of a tree!
102 === Don't Lose Your HEAD ===
104 The HEAD tag is like a cursor that normally points at the latest commit, advancing with each new commit. Some Git commands let you move it. For example:
106  $ git reset HEAD~3
108 will move the HEAD three commits backwards in time. Thus all Git commands now act as if you hadn't made those last three commits, while your files remain in the present. See the git-reset man page for some applications.
110 But how can you go back to the future? The past commits do not know anything of the future.
112 If you have the SHA1 of the original HEAD then:
114  $ git reset SHA1
116 But suppose you never took it down? Don't worry, for commands like these, Git saves the original HEAD as a tag called ORIG_HEAD, and you can return safe and sound with:
118  $ git reset ORIG_HEAD
120 === Building On Git ===
122 In true UNIX fashion, Git's design allows it to be easily used as a low-level component of other programs. There are GUI interfaces, web interfaces, alternative command-line interfaces, and perhaps soon you will have a script or two of your own that calls Git.
124 See http://git.or.cz/[the Git homepage] for some examples.