Forgot to commit the untranslated files in "es".
[gitmagic/gitmagic.git] / es / grandmaster.txt
blobc5399e83f41362e298c6204a63bacbf85eea017f
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. 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 a web server, but Git is not installed. Though less efficient than its native protocol, Git can communicate over HTTP.
21 Download, compile and install Git in your account, and create a repository in your web directory:
23  $ GIT_DIR=proj.git git init
25 In the "proj.git" directory, run:
27  $ git --bare update-server-info
28  $ chmod a+x hooks/post-update
30 From your computer, push via ssh:
32  $ git push web.server:/path/to/proj.git master
34 and others get your project via:
36  $ git clone http://web.server/proj.git
38 === Git Over Anything ===
40 Want to synchronize repositories without servers, or even a network connection?
41 Need to improvise during an emergency?
42 We've seen <<makinghistory, *git fast-export* and *git fast-import* can convert
43 repositories to a single file and back>>. We could shuttle such files back and
44 forth to transport git repositories over any medium, but a more efficient tool
45 is *git bundle*.
47 The sender creates a 'bundle':
49  $ git bundle create somefile HEAD
51 then transports the bundle, +somefile+, to the other party somehow: email,
52 thumb drive, floppy disk, an *xxd* printout and an OCR machine,
53 reading bits over the phone, smoke signals, etc. The receiver retrieves
54 commits from the bundle by typing:
56  $ git pull somefile
58 The receiver can even do this from an empty repository. Despite its
59 size, +somefile+ contains the entire original git repository.
61 In larger projects, eliminate waste by bundling only changes the other
62 repository lacks:
64  $ git bundle create somefile HEAD ^COMMON_SHA1
66 If done frequently, one could easily forget which commit was last sent. The
67 help page suggests using tags to solve this. Namely, after you send a bundle,
68 type:
70  $ git tag -f lastbundle HEAD
72 and create new refresher bundles with:
74  $ git bundle create newbundle HEAD ^lastbundle
76 === Patches: The Global Currency ===
78 Patches are text representations of your changes that can be easily understood
79 by computers and humans alike. This gives them universal appeal. You can email a
80 patch to developers no matter what version control system they're using. As long
81 as your audience can read their email, they can see your edits. Similarly, on
82 your side, all you require is an email account: there's no need to setup an online Git repository.
84 Recall from the first chapter:
86  $ git diff COMMIT
88 outputs a patch which can be pasted into an email for discussion. In a Git
89 repository, type:
91  $ git apply < FILE
93 to apply the patch.
95 In more formal settings, when author names and perhaps signatures should be
96 recorded, generate the corresponding patches past a certain point by typing:
98  $ git format-patch START_COMMIT
100 The resulting files can be given to *git-send-email*, or sent by hand. You can also specify a range of commits:
102  $ git format-patch START_COMMIT..END_COMMIT
104 On the receving end, save an email to a file, then type:
106  $ git am < FILE
108 This applies the incoming patch and also creates a commit, including information such as the author.
110 With a browser email client, you may need to click a button to see the email in its raw original form before saving the patch to a file.
112 There are slight differences for mbox-based email clients, but if you use one
113 of these, you're probably the sort of person who can figure them out easily
114 without reading tutorials!
116 === Commit What Changed ===
118 Telling Git when you've added, deleted and renamed files is troublesome for certain projects. Instead, you can type:
120  $ git add .
121  $ git add -u
123 Git will look at the files in the current directory and work out the details by itself. Instead of the second add command, run `git commit -a` if you also intend to commit at this time.
125 You can perform the above in a single pass with:
127  $ git ls-files -d -m -o -z | xargs -0 git update-index --add --remove
129 The *-z* and *-0* options prevent ill side-effects from filenames containing strange characters. Note this command adds ignored files. You may want to use the `-x` or `-X` option.
131 === My Commit Is Too Big! ===
133 Have you neglected to commit for too long? Been coding furiously and forgotten
134 about source control until now? Made a series of unrelated changes, because
135 that's your style?
137 No worries. Run:
139  $ git add -p
141 For each edit you made, Git will show you the hunk of code that was changed,
142 and ask if it should be part of the next commit. Answer with "y" or "n". You
143 have other options, such as postponing the decision; type "?" to learn more.
145 Once you're satisfied, type
147  $ git commit
149 to commit precisely the changes you selected (the 'staged' changes). Make sure
150 you omit the *-a* option, otherwise Git will commit all the edits.
152 What if you've edited many files in many places? Reviewing each change one by
153 one becomes frustratingly mind-numbing. In this case, use *git add -i*, whose
154 interface is less straightforward, but more flexible. With a few keystrokes,
155 you can stage or unstage several files at a time, or review and select changes
156 in particular files only. Alternatively, run *git commit \--interactive* which
157 automatically commits after you're done.
159 ==== Staged Changes ====
161 So far I have deliberately avoided mentioning Git's famous 'index', but we must
162 now confront it to explain the above. The index is a temporary staging area.
163 Git seldom shuttles data directly between your project and its history. Rather,
164 Git first writes data to the index, and then copies the data in the index to
165 its final destination.
167 For example, *commit -a* is really a two-step process. The first step places a
168 snapshot of the current state of every tracked file into the index. The second
169 step permanently records the snapshot now in the index. Committing without the
170 *-a* option only performs the second step, and only makes sense after running
171 commands that somehow change the index, such as *git add*.
173 Usually we can ignore the index and pretend we are reading straight from and writing straight to the history. On this occasion, we want finer control on what gets written to history, and are forced to manipulate the index. We place a snapshot of some, but not all, of our changes into the index, and then permanently record this carefully rigged snapshot.
175 === Don't Lose Your HEAD ===
177 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:
179  $ git reset HEAD~3
181 will move the HEAD three commits back. 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 help page for some applications.
183 But how can you go back to the future? The past commits know nothing of the future.
185 If you have the SHA1 of the original HEAD then:
187  $ git reset SHA1
189 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:
191  $ git reset ORIG_HEAD
193 === HEAD-hunting ===
195 Perhaps ORIG_HEAD isn't enough. Perhaps you've just realized you made a monumental mistake and you need to go back to an ancient commit in a long-forgotten branch.
197 By default, Git keeps a commit for at least two weeks, even if you ordered
198 Git to destroy the branch containing it. The trouble is finding the appropriate
199 hash. You could look at all the hash values in `.git/objects` and use trial
200 and error to find the one you want. But there's a much easier way.
202 Git records every hash of a commit it computes in `.git/logs`. The subdirectory `refs` contains the history of all activity on all branches, while the file `HEAD` shows every hash value it has ever taken. The latter can be used to find hashes of commits on branches that have been accidentally lopped off.
204 The reflog command provides a friendly interface to these log files. Try
206   $ git reflog
208 Instead of cutting and pasting hashes from the reflog, try:
210  $ git checkout "@{10 minutes ago}"
212 Or checkout the 5th-last visited commit via:
214  $ git checkout "@{5}"
216 See the ``Specifying Revisions'' section of *git help rev-parse* for more.
218 You may wish to configure a longer grace period for doomed commits. For
219 example:
221   $ git config gc.pruneexpire "30 days"
223 means a deleted commit will only be permanently lost once 30 days have passed
224 and *git gc* is run.
226 You may also wish to disable automatic invocations of *git gc*:
228   $ git conifg gc.auto 0
230 in which case commits will only be deleted when you run *git gc* manually.
232 === Building On Git ===
234 In true UNIX fashion, Git's design allows it to be easily used as a low-level component of other programs, such as GUI and web interfaces, alternative command-line interfaces, patch managements tools, importing and conversion tools and so on. In fact, some Git commands are themselves scripts standing on the shoulders of giants. With a little tinkering, you can customize Git to suit your preferences.
236 One easy trick is to use built-in Git aliases to shorten your most frequently
237 used commands:
239   $ git config --global alias.co checkout
240   $ git config --global --get-regexp alias  # display current aliases
241   alias.co checkout
242   $ git co foo                              # same as 'git checkout foo'
244 Another is to print the current branch in the prompt, or window title.
245 Invoking
247   $ git symbolic-ref HEAD
249 shows the current branch name. In practice, you most likely want to remove
250 the "refs/heads/" and ignore errors:
252   $ git symbolic-ref HEAD 2> /dev/null | cut -b 12-
254 The +contrib+ subdirectory is a treasure trove of tools built on basic commands
255 (which in time may be promoted to official commands). On Debian and Ubuntu,
256 this directory lives at +/usr/share/doc/git-core/contrib+.
258 One popular resident is +workdir/git-new-workdir+. Via clever symlinking, this script creates a new working directory whose history is shared with the original respository:
260   $ git-new-workdir an/existing/repo new/directory
262 The new directory and files within can be thought of as a clone, except since the history is shared, the two trees automatically stay in sync. There's no need to merge, push or pull.
264 === Daring Stunts ===
266 Recent versions of Git make it difficult for the user to accidentally destroy
267 data. This is perhaps the most compelling reason to upgrade. Nonetheless, there
268 are times you truly want to destroy data. We show how to override the
269 safeguards for common commands. Only use them if you know what you are doing.
271 *Checkout*: Uncommitted changes cause checkout to fail. To destroy your changes, and checkout a given commit anyway, use the force flag:
273   $ git checkout -f COMMIT
275 On the other hand, if you specify particular paths for checkout, then there are no safety checks. The supplied paths are quietly overwritten. Take care if you use checkout in this manner.
277 *Reset*: Reset also fails in the presence of uncommitted changes. To force it through, run:
279   $ git reset --hard [COMMIT]
281 *Branch*: Deleting branches fails if this causes changes to be lost. To force a deletion, type:
283   $ git branch -D BRANCH  # instead of -d
285 Similarly, attempting to overwrite a branch via a move fails if data loss would ensue. To force a branch move, type:
287   $ git branch -M [SOURCE] TARGET  # instead of -m
289 Unlike checkout and reset, these two commands defer data destruction. The
290 changes are still stored in the .git subdirectory, and can be retrieved by
291 recovering the appropriate hash from `.git/logs` (see "HEAD-hunting" above).
292 By default, they will be kept for at least two weeks.
294 *Clean*: Some git commands refuse to proceed because they're worried about
295 clobbering untracked files. If you're certain that all untracked files and
296 directories are expendable, then delete them mercilessly with:
298   $ git clean -f -d
300 Next time, that pesky command will work!