]> git.phdru.name Git - git-wiki.git/blob - pep-git.txt
1c78dd490d0ee14893f4f0fdfc2caba5aa51d6c0
[git-wiki.git] / pep-git.txt
1 PEP: XXX
2 Title: Collecting information about git
3 Version: $Revision$
4 Last-Modified: $Date$
5 Author: Oleg Broytman <phd@phdru.name>
6 Status: Draft
7 Type: Informational
8 Content-Type: text/x-rst
9 Created: 01-Jun-2015
10 Post-History: 
11
12 Abstract
13 ========
14
15 This Informational PEP collects information about git. There is, of
16 course, a lot of documentation for git, so the PEP concentrates on
17 more complex (and more related to Python development) issues,
18 scenarios and examples.
19
20 The plan is to extend the PEP in the future collecting information
21 about equivalence of Mercurial and git scenarios to help migrating
22 Python development from Mercurial to git.
23
24 The author of the PEP doesn't currently plan to write a Process PEP on
25 migration Python development from Mercurial to git.
26
27
28 Documentation
29 =============
30
31 Git is accompanied with a lot of documentation, both online and
32 offline.
33
34
35 Documentation for starters
36 --------------------------
37
38 Git Tutorial: `part 1
39 <https://www.kernel.org/pub/software/scm/git/docs/gittutorial.html>`_,
40 `part 2
41 <https://www.kernel.org/pub/software/scm/git/docs/gittutorial-2.html>`_.
42
43 `Git User's manual
44 <https://www.kernel.org/pub/software/scm/git/docs/user-manual.html>`_.
45 `Everyday GIT With 20 Commands Or So
46 <https://www.kernel.org/pub/software/scm/git/docs/everyday.html>`_.
47 `Git workflows
48 <https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html>`_.
49
50
51 Advanced documentation
52 ----------------------
53
54 `Git Magic
55 <http://www-cs-students.stanford.edu/~blynn/gitmagic/index.html>`_,
56 with a number of translations.
57
58 `Pro Git <https://git-scm.com/book>`_. The Book about git. Buy it at
59 Amazon or download in PDF, mobi, or ePub form. It has translations to
60 many different languages. Download Russian translation from `GArik
61 <https://github.com/GArik/progit/wiki>`_.
62
63 `Git Wiki <https://git.wiki.kernel.org/index.php/Main_Page>`_.
64
65
66 Offline documentation
67 ---------------------
68
69 Git has builtin help: run ``git help $TOPIC``. For example, run
70 ``git help git`` or ``git help help``.
71
72
73 Quick start
74 ===========
75
76 Download and installation
77 -------------------------
78
79 Unix users: `download and install using your package manager
80 <https://git-scm.com/download/linux>`_.
81
82 Microsoft Windows: download `git-for-windows
83 <https://github.com/git-for-windows/git/releases>`_ or `msysGit
84 <https://github.com/msysgit/msysgit/releases>`_.
85
86 MacOS X: use git installed with `XCode
87 <https://developer.apple.com/xcode/downloads/>`_ or download from
88 `MacPorts <https://www.macports.org/ports.php?by=name&substr=git>`_ or
89 `git-osx-installer
90 <http://sourceforge.net/projects/git-osx-installer/files/>`_ or
91 install git with `Homebrew <http://brew.sh/>`_: ``brew install git``.
92
93 `git-cola <https://git-cola.github.io/index.html>`_ is a Git GUI
94 written in Python and GPL licensed. Linux, Windows, MacOS X.
95
96 `TortoiseGit <https://tortoisegit.org/>`_ is a Windows Shell Interface
97 to Git based on TortoiseSVN; open source.
98
99
100 Initial configuration
101 ---------------------
102
103 This simple code is often appears in documentation, but it is
104 important so let repeat it here. Git stores author and committer
105 names/emails in every commit, so configure your real name and
106 preferred email::
107
108     $ git config --global user.name "User Name"
109     $ git config --global user.email user.name@example.org
110
111
112 Examples in this PEP
113 ====================
114
115 Examples of git commands in this PEP use the following approach. It is
116 supposed that you, the user, works with a local repository named
117 ``python`` that has an upstream remote repo named ``origin``. Your
118 local repo has two branches ``v1`` and ``master``. For most examples
119 the currently checked out branch is ``master``. That is, it's assumed
120 you have done something like that::
121
122     $ git clone https://git.python.org/python.git
123     $ cd python
124     $ git branch v1 origin/v1
125
126 The first command clones remote repository into local directory
127 `python``, creates a new local branch master, sets
128 remotes/origin/master as its upstream remote-tracking branch and
129 checks it out into the working directory.
130
131 The last command creates a new local branch v1 and sets
132 remotes/origin/v1 as its upstream remote-tracking branch.
133
134 The same result can be achieved with commands::
135
136     $ git clone -b v1 https://git.python.org/python.git
137     $ cd python
138     $ git checkout --track origin/master
139
140 The last command creates a new local branch master, sets
141 remotes/origin/master as its upstream remote-tracking branch and
142 checks it out into the working directory.
143
144
145 Branches and branches
146 =====================
147
148 Git terminology can be a bit misleading. Take, for example, the term
149 "branch". In git it has two meanings. A branch is a directed line of
150 commits (possibly with merges). And a branch is a label or a pointer
151 assigned to a line of commits. It is important to distinguish when you
152 talk about commits and when about their labels. Lines of commits are
153 by itself unnamed and are usually only lengthening and merging.
154 Labels, on the other hand, can be created, moved, renamed and deleted
155 freely.
156
157
158 Remote repositories and remote branches
159 =======================================
160
161 Remote-tracking branches are branches (pointers to commits) in your
162 local repository. They are there for git (and for you) to remember
163 what branches and commits have been pulled from and pushed to what
164 remote repos (you can pull from and push to many remotes).
165 Remote-tracking branches live under ``remotes/$REMOTE`` namespaces,
166 e.g. ``remotes/origin/master``.
167
168 To see the status of remote-tracking branches run::
169
170     $ git branch -rv
171
172 To see local and remote-tracking branches (and tags) pointing to
173 commits::
174
175     $ git log --decorate
176
177 You never do your own development on remote-tracking branches. You
178 create a local branch that has a remote branch as upstream and do
179 development on that local branch. On push git pushes commits to the
180 remote repo and updates remote-tracking branches, on pull git fetches
181 commits from the remote repo, updates remote-tracking branches and
182 fast-forwards, merges or rebases local branches.
183
184 When you do an initial clone like this::
185
186     $ git clone -b v1 https://git.python.org/python.git
187
188 git clones remote repository ``https://git.python.org/python.git`` to
189 directory ``python``, creates a remote named ``origin``, creates
190 remote-tracking branches, creates a local branch ``v1``, configure it
191 to track upstream remotes/origin/v1 branch and checks out ``v1`` into
192 the working directory.
193
194
195 Updating local and remote-tracking branches
196 -------------------------------------------
197
198 There is a major difference between
199
200 ::
201
202     $ git fetch $REMOTE $BRANCH
203
204 and
205
206 ::
207
208     $ git fetch $REMOTE $BRANCH:$BRANCH
209
210 The first command fetches commits from the named $BRANCH in the
211 $REMOTE repository that are not in your repository, updates
212 remote-tracking branch and leaves the id (the hash) of the head commit
213 in file .git/FETCH_HEAD.
214
215 The second command fetches commits from the named $BRANCH in the
216 $REMOTE repository that are not in your repository and updates both
217 the local branch $BRANCH and its upstream remote-tracking branch. But
218 it refuses to update branches in case of non-fast-forward. And it
219 refuses to update the current branch (currently checked out branch,
220 where HEAD is pointing to).
221
222 The first command is used internally by ``git pull``.
223
224 ::
225
226     $ git pull $REMOTE $BRANCH
227
228 is equivalent to
229
230 ::
231
232     $ git fetch $REMOTE $BRANCH
233     $ git merge FETCH_HEAD
234
235 Certainly, $BRANCH in that case should be your current branch. If you
236 want to merge a different branch into your current branch first update
237 that non-current branch and then merge::
238
239     $ git fetch origin v1:v1  # Update v1
240     $ git pull --rebase origin master  # Update the current branch master
241                                        # using rebase instead of merge
242     $ git merge v1
243
244 If you have not yet pushed commits on ``v1``, though, the scenario has
245 to become a bit more complex. Git refuses to update
246 non-fast-forwardable branch, and you don't want to do force-pull
247 because that would remove your non-pushed commits and you would need
248 to recover. So you want to rebase ``v1`` but you cannot rebase
249 non-current branch. Hence, checkout ``v1`` and rebase it before
250 merging::
251
252     $ git checkout v1
253     $ git pull --rebase origin v1
254     $ git checkout master
255     $ git pull --rebase origin master
256     $ git merge v1
257
258 It is possible to configure git to make it fetch/pull a few branches
259 or all branches at once, so you can simply run
260
261 ::
262
263     $ git pull origin
264
265 or even
266
267 ::
268
269     $ git pull
270
271 Default remote repository for fetching/pulling is ``origin``. Default
272 set of references to fetch is calculated using matching algorithm: git
273 fetches all branches having the same name on both ends.
274
275
276 Push
277 ''''
278
279 Pushing is a bit simpler. There is only one command ``push``. When you
280 run
281
282 ::
283
284     $ git push origin v1 master
285
286 git pushes local v1 to remote v1 and local master to remote master.
287 The same as::
288
289     $ git push origin v1:v1 master:master
290
291 Git pushes commits to the remote repo and updates remote-tracking
292 branches. Git refuses to push commits that aren't fast-forwardable.
293 You can force-push anyway, but please remember - you can force-push to
294 your own repositories but don't force-push to public or shared repos.
295 If you find git refuses to push commits that aren't fast-forwardable,
296 better fetch and merge commits from the remote repo (or rebase your
297 commits on top of the fetched commits), then push. Only force-push if
298 you know what you do and why you do it. See the section `Commit
299 editing and caveats`_ below.
300
301 It is possible to configure git to make it push a few branches or all
302 branches at once, so you can simply run
303
304 ::
305
306     $ git push origin
307
308 or even
309
310 ::
311
312     $ git push
313
314 Default remote repository for pushing is ``origin``. Default set of
315 references to push in git before 2.0 is calculated using matching
316 algorithm: git pushes all branches having the same name on both ends.
317 Default set of references to push in git 2.0+ is calculated using
318 simple algorithm: git pushes the current branch back to its
319 @{upstream}.
320
321 To configure git before 2.0 to the new behaviour run::
322
323 $ git config push.default simple
324
325 To configure git 2.0+ to the old behaviour run::
326
327 $ git config push.default matching
328
329 Git doesn't allow to push a branch if it's the current branch in the
330 remote non-bare repository: git refuses to update remote working
331 directory. You really should push only to bare repositories. For
332 non-bare repositories git prefers pull-based workflow.
333
334 When you want to deploy code on a remote host and can only use push
335 (because your workstation is behind a firewall and you cannot pull
336 from it) you do that in two steps using two repositories: you push
337 from the workstation to a bare repo on the remote host, ssh to the
338 remote host and pull from the bare repo to a non-bare deployment repo.
339
340 That changed in git 2.3, but see `the blog post
341 <https://github.com/blog/1957-git-2-3-has-been-released#push-to-deploy>`_
342 for caveats; in 2.4 the push-to-deploy feature was `further improved
343 <https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more#push-to-deploy-improvements>`_.
344
345
346 Tags
347 ''''
348
349 Git automatically fetches tags that point to commits being fetched
350 during fetch/pull. To fetch all tags (and commits they point to) run
351 ``git fetch --tags origin``. To fetch some specific tags fetch them
352 explicitly::
353
354     $ git fetch origin tag $TAG1 tag $TAG2...
355
356 For example::
357
358     $ git fetch origin tag 1.4.2
359     $ git fetch origin v1:v1 tag 2.1.7
360
361 Git doesn't automatically pushes tags. That allows you to have private
362 tags. To push tags list them explicitly::
363
364     $ git push origin tag 1.4.2
365     $ git push origin v1 master tag 2.1.7
366
367 Or push all tags at once::
368
369     $ git push --tags origin
370
371 Don't move tags with ``git tag -f`` or remove tags with ``git tag -d``
372 after they have been published.
373
374
375 Private information
376 '''''''''''''''''''
377
378 When cloning/fetching/pulling/pushing git copies only database objects
379 (commits, trees, files and tags) and symbolic references (branches and
380 lightweight tags). Everything else is private to the repository and
381 never cloned, updated or pushed. It's your config, your hooks, your
382 private exclude file.
383
384 If you want to distribute hooks, copy them to the working tree, add,
385 commit, push and instruct the team to update and install the hooks
386 manually.
387
388
389 Commit editing and caveats
390 ==========================
391
392 A warning not to edit published (pushed) commits also appears in
393 documentation but it's repeated here anyway as it's very important.
394
395 It is possible to recover from a forced push but it's PITA for the
396 entire team. Please avoid it.
397
398 To see what commits have not been published yet compare the head of the
399 branch with its upstream remote-tracking branch::
400
401     $ git log origin/master..  # from origin/master to HEAD (of master)
402     $ git log origin/v1..v1  # from origin/v1 to the head of v1
403
404 For every branch that has an upstream remote-tracking branch git
405 maintains an alias @{upstream} (short version @{u}), so the commands
406 above can be given as::
407
408     $ git log @{u}..
409     $ git log v1@{u}..v1
410
411 To see the status of all branches::
412
413     $ git branch -avv
414
415 To compare the status of local branches with a remote repo::
416
417     $ git remote show origin
418
419 Read `how to recover from upstream rebase
420 <https://git-scm.com/docs/git-rebase#_recovering_from_upstream_rebase>`_.
421 It is in ``git help rebase``.
422
423 On the other hand don't be too afraid about commit editing. You can
424 safely edit, reorder, remove, combine and split commits that haven't
425 been pushed yet. You can even push commits to your own (backup) repo,
426 edit them later and force-push edited commits to replace what have
427 already been pushed. Not a problem until commits are in a public
428 or shared repository.
429
430
431 Undo
432 ====
433
434 Whatever you do, don't panic. Almost anything in git can be undone.
435
436
437 git checkout: restore file's content
438 ------------------------------------
439
440 ``git checkout``, for example, can be used to restore the content of
441 file(s) to that one of a commit. Like this::
442
443     git checkout HEAD~ README
444
445 The commands restores the contents of README file to the last but one
446 commit in the current branch. By default the commit ID is simply HEAD;
447 i.e. ``git checkout README`` restores README to the latest commit.
448
449 (Do not use ``git checkout`` to view a content of a file in a commit,
450 use ``git cat-file -p``; e.g. ``git cat-file -p HEAD~:path/to/README``).
451
452
453 git reset: remove (non-pushed) commits
454 --------------------------------------
455
456 ``git reset`` moves the head of the current branch. The head can be
457 moved to point to any commit but it's often used to remove a commit or
458 a few (preferably, non-pushed ones) from the top of the branch - that
459 is, to move the branch backward in order to undo a few (non-pushed)
460 commits.
461
462 ``git reset`` has three modes of operation - soft, hard and mixed.
463 Default is mixed. ProGit `explains
464 <https://git-scm.com/book/en/Git-Tools-Reset-Demystified>`_ the
465 difference very clearly. Bare repositories don't have indices or
466 working trees so in a bare repo only soft reset is possible.
467
468
469 Unstaging
470 '''''''''
471
472 Mixed mode reset with a path or paths can be used to unstage changes -
473 that is, to remove from index changes added with ``git add`` for
474 committing. See `The Book
475 <https://git-scm.com/book/en/Git-Basics-Undoing-Things>`_ for details
476 about unstaging and other undo tricks.
477
478
479 git reflog: reference log
480 -------------------------
481
482 Removing commits with ``git reset`` or moving the head of a branch
483 sounds dangerous and it is. But there is a way to undo: another
484 reset back to the original commit. Git doesn't remove commits
485 immediately; unreferenced commits (in git terminology they are called
486 "dangling commits") stay in the database for some time (default is two
487 weeks) so you can reset back to it or create a new branch pointing to
488 the original commit.
489
490 For every move of a branch's head - with ``git commit``, ``git
491 checkout``, ``git fetch``, ``git pull``, ``git rebase``, ``git reset``
492 and so on - git stores a reference log (reflog for short). For every
493 move git stores where the head was. Command ``git reflog`` can be used
494 to view (and manipulate) the log.
495
496 In addition to the moves of the head of every branch git stores the
497 moves of the HEAD - a symbolic reference that (usually) names the
498 current branch. HEAD is changed with ``git checkout $BRANCH``.
499
500 By default ``git reflog`` shows the moves of the HEAD, i.e. the
501 command is equivalent to ``git reflog HEAD``. To show the moves of the
502 head of a branch use the command ``git reflog $BRANCH``.
503
504 So to undo a ``git reset`` lookup the original commit in ``git
505 reflog``, verify it with ``git show`` or ``git log`` and run ``git
506 reset $COMMIT_ID``. Git stores the move of the branch's head in
507 reflog, so you can undo that undo later again.
508
509 In a more complex situation you'd want to move some commits along with
510 resetting the head of the branch. Cherry-pick them to the new branch.
511 For example, if you want to reset the branch ``master`` back to the
512 original commit but preserve two commits created in the current branch
513 do something like::
514
515     $ git branch save-master # create a new branch saving master
516     $ git reflog # find the original place of master
517     $ git reset $COMMIT_ID
518     $ git cherry-pick save-master~ save-master
519     $ git branch -D save-master # remove temporary branch
520
521
522 git revert: revert a commit
523 ---------------------------
524
525 ``git revert`` reverts a commit or commits, that is, it creates a new
526 commit or commits that revert(s) the effects of the given commits.
527 It's the only way to undo published commits (``git commit --amend``,
528 ``git rebase`` and ``git reset`` change the branch in
529 non-fast-forwardable ways so they should only be used for non-pushed
530 commits.)
531
532 There is a problem with reverting a merge commit. ``git revert`` can
533 undo the code created by the merge commit but it cannot undo the fact
534 of merge. See the discussion `How to revert a faulty merge
535 <https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html>`_.
536
537
538 One thing that cannot be undone
539 -------------------------------
540
541 Whatever you undo, there is one thing that cannot be undone -
542 overwritten uncommitted changes. Uncommitted changes don't belong to
543 git so git cannot help preserving them.
544
545 Most of the time git warns you when you're going to execute a command
546 that overwrites uncommitted changes. Git doesn't allow you to switch
547 branches with ``git checkout``. It stops you when you're going to
548 rebase with non-clean working tree. It refuses to pull new commits
549 over non-committed files.
550
551 But there are commands that do exactly that - overwrite files in the
552 working tree. Commands like ``git checkout $PATHs`` or ``git reset
553 --hard`` silently overwrite files including your uncommitted changes.
554
555 With that in mind you can understand the stance "commit early, commit
556 often". Commit as often as possible. Commit on every save in your
557 editor or IDE. You can edit your commits before pushing - edit commit
558 messages, change commits, reorder, combine, split, remove. But save
559 your changes in git database, either commit changes or at least stash
560 them with ``git stash``.
561
562
563 Merge or rebase?
564 ================
565
566 Internet is full of heated discussions on the topic: "merge or
567 rebase?" Most of them are meaningless. When a DVCS is being used in a
568 big team with a big and complex project with many branches there is
569 simply no way to avoid merges. So the question's diminished to
570 "whether to use rebase, and if yes - when to use rebase?" Considering
571 that it is very much recommended not to rebase published commits the
572 question's diminished even further: "whether to use rebase on
573 non-pushed commits?"
574
575 That small question is for the team to decide. The author of the PEP
576 recommends to use rebase when pulling, i.e. always do ``git pull
577 --rebase`` or even configure automatic setup of rebase for every new
578 branch::
579
580     $ git config branch.autosetuprebase always
581
582 and configure rebase for existing branches::
583
584     $ git config branch.$NAME.rebase true
585
586 For example::
587
588     $ git config branch.v1.rebase true
589     $ git config branch.master.rebase true
590
591 After that ``git pull origin master`` becomes equivalent to ``git pull
592 --rebase origin master``.
593
594 It is recommended to create new commits in a separate feature or topic
595 branch while using rebase to update the mainline branch. When the
596 topic branch is ready merge it into mainline. To avoid a tedious task
597 of resolving large number of conflicts at once you can merge the topic
598 branch to the mainline from time to time and switch back to the topic
599 branch to continue working on it. The entire workflow would be
600 something like::
601
602     $ git checkout -b issue-42  # create a new issue branch and switch to it
603         ...edit/test/commit...
604     $ git checkout master
605     $ git pull --rebase origin master  # update master from the upstream
606     $ git merge issue-42
607     $ git branch -d issue-42  # delete the topic branch
608     $ git push origin master
609
610 When the topic branch is deleted only the label is removed, commits
611 are stayed in the database, they are now merged into master::
612
613     o--o--o--o--o--M--< master - the mainline branch
614         \         /
615          --*--*--*             - the topic branch, now unnamed
616
617 The topic branch is deleted to avoid cluttering branch namespace with
618 small topic branches. Information on what issue was fixed or what
619 feature was implemented should be in the commit messages.
620
621
622 Null-merges
623 ===========
624
625 Git has a builtin merge strategy for what Python core developers call
626 "null-merge"::
627
628     $ git merge -s ours v1  # null-merge v1 into master
629
630
631 Advanced configuration
632 ======================
633
634 Line endings
635 ------------
636
637 Git has builtin mechanisms to handle line endings between platforms
638 with different end-of-line styles. To allow git to do CRLF conversion
639 assign ``text`` attribute to files using `.gitattributes
640 <https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html>`_.
641 For files that have to have specific line endings assign ``eol``
642 attribute. For binary files the attribute is, naturally, ``binary``.
643
644 For example::
645
646     $ cat .gitattributes
647     *.py text
648     *.txt text
649     *.png binary
650     /readme.txt eol=CRLF
651
652 To check what attributes git uses for files use ``git check-attr``
653 command. For example::
654
655 $ git check-attr -a -- \*.py
656
657
658 Advanced topics
659 ===============
660
661 Staging area
662 ------------
663
664 Staging area aka index aka cache is a distinguishing feature of git.
665 Staging area is where git collects patches before committing them.
666 Separation between collecting patches and commit phases provides a
667 very useful feature of git: you can review collected patches before
668 commit and even edit them - remove some hunks, add new hunks and
669 review again.
670
671 To add files to the index use ``git add``. Collecting patches before
672 committing means you need to do that for every change, not only to add
673 new (untracked) files. To simplify committing in case you just want to
674 commit everything without reviewing run ``git commit --all`` (or just
675 ``-a``) - the command adds every changed tracked file to the index and
676 then commit. To commit a file or files regardless of patches collected
677 in the index run ``git commit [--only|-o] -- $FILE...``.
678
679 To add hunks of patches to the index use ``git add --patch`` (or just
680 ``-p``). To remove collected files from the index use ``git reset HEAD
681 -- $FILE...`` To add/inspect/remove collected hunks use ``git add
682 --interactive`` (``-i``).
683
684 To see the diff between the index and the last commit (i.e., collected
685 patches) use ``git diff --cached``. To see the diff between the
686 working tree and the index (i.e., uncollected patches) use just ``git
687 diff``. To see the diff between the working tree and the last commit
688 (i.e., both collected and uncollected patches) run ``git diff HEAD``.
689
690 See `WhatIsTheIndex
691 <https://git.wiki.kernel.org/index.php/WhatIsTheIndex>`_ and
692 `IndexCommandQuickref
693 <https://git.wiki.kernel.org/index.php/IndexCommandQuickref>`_ in Git
694 Wiki.
695
696
697 ReReRe
698 ======
699
700 Rerere is a mechanism that helps to resolve repeated merge conflicts.
701 The most frequent source of recurring merge conflicts are topic
702 branches that are merged into mainline and then the merge commits are
703 removed; that's often performed to test the topic branches and train
704 rerere; merge commits are removed to have clean linear history and
705 finish the topic branch with only one last merge commit.
706
707 Rerere works by remembering the states of tree before and after a
708 successful commit. That way rerere can automatically resolve conflicts
709 if they appear in the same files.
710
711 Rerere can be used manually with ``git rerere`` command but most often
712 it's used automatically. Enable rerere with these commands in a
713 working tree::
714
715     $ git config rerere.enabled true
716     $ git config rerere.autoupdate true
717
718 You don't need to turn rerere on globally - you don't want rerere in
719 bare repositories or single-branche repositories; you only need rerere
720 in repos where you often perform merges and resolve merge conflicts.
721
722 See `Rerere <https://git-scm.com/book/en/Git-Tools-Rerere>`_ in The
723 Book.
724
725
726 Database maintenance
727 ====================
728
729 Git object database and other files/directories under ``.git`` require
730 periodic maintenance and cleanup. For example, commit editing left
731 unreferenced objects (dangling objects, in git terminology) and these
732 objects should be pruned to avoid collecting cruft in the DB. The
733 command ``git gc`` is used for maintenance. Git automatically runs
734 ``git gc --auto`` as a part of some commands to do quick maintenance.
735 Users are recommended to run ``git gc --aggressive`` from time to
736 time; ``git help gc`` recommends to run it  every few hundred
737 changesets; for more intensive projects it should be something like
738 once a week and less frequently (biweekly or monthly) for lesser
739 active projects.
740
741 ``git gc --aggressive`` not only removes dangling objects, it also
742 repacks object database into indexed and better optimized pack(s); it
743 also packs symbolic references (branches and tags). Another way to do
744 it is to run ``git repack``.
745
746 There is a well-known `message
747 <https://gcc.gnu.org/ml/gcc/2007-12/msg00165.html>`_ from Linus
748 Torvalds regarding "stupidity" of ``git gc --aggressive``. The message
749 can safely be ignored now. It is old and outdated, ``git gc
750 --aggressive`` became much better since that time.
751
752 For those who still prefer ``git repack`` over ``git gc --aggressive``
753 the recommended parameters are ``git repack -a -d -f --depth=20
754 --window=250``. See `this detailed experiment
755 <http://vcscompare.blogspot.ru/2008/06/git-repack-parameters.html>`_
756 for explanation of the effects of these parameters.
757
758 From time to time run ``git fsck [--strict]`` to verify integrity of
759 the database. ``git fsck`` may produce a list of dangling objects;
760 that's not an error, just a reminder to perform regular maintenance.
761
762
763 Tips and tricks
764 ===============
765
766 Command-line options and arguments
767 ----------------------------------
768
769 `git help cli
770 <https://www.kernel.org/pub/software/scm/git/docs/gitcli.html>`_
771 recommends not to combine short options/flags. Most of the times
772 combining works: ``git commit -av`` works perfectly, but there are
773 situations when it doesn't. E.g., ``git log -p -5`` cannot be combined
774 as ``git log -p5``.
775
776 Some options have arguments, some even have default arguments. In that
777 case the argument for such option must be spelled in a sticky way:
778 ``-Oarg``, never ``-O arg`` because for an option that has a default
779 argument the latter means "use default value for option ``-O`` and
780 pass ``arg`` further to the option parser". For example, ``git grep``
781 has an option ``-O`` that passes a list of names of the found files to
782 a program; default program for ``-O`` is a pager (usually ``less``),
783 but you can use your editor::
784
785     $ git grep -Ovim # but not -O vim
786
787 BTW, if git is instructed to use ``less`` as the pager (i.e., if pager
788 is not configured in git at all it uses ``less`` by default, or if it
789 gets ``less`` from GIT_PAGER or PAGER environment variables, or if it
790 was configured with ``git config --global core.pager less``, or
791 ``less`` is used in the command ``git grep -Oless``) ``git grep``
792 passes ``+/$pattern`` option to ``less`` which is quite convenient.
793 Unfortunately, ``git grep`` doesn't pass the pattern if the pager is
794 not exactly ``less``, even if it's ``less`` with parameters (something
795 like ``git config --global core.pager less -FRSXgimq``); fortunately,
796 ``git grep -Oless`` always passes the pattern.
797
798
799 bash/zsh completion
800 -------------------
801
802 It's a bit hard to type ``git rebase --interactive --preserve-merges
803 HEAD~5`` manually even for those who are happy to use command-line,
804 and this is where shell completion is of great help. Bash/zsh come
805 with programmable completion, often automatically installed and
806 enabled, so if you have bash/zsh and git installed, chances are you
807 are already done - just go and use it at the command-line.
808
809 If you don't have necessary bits installed, install and enable
810 bash_completion package. If you want to upgrade your git completion to
811 the latest and greatest download necessary file from `git contrib
812 <https://git.kernel.org/cgit/git/git.git/tree/contrib/completion>`_.
813
814 Git-for-windows comes with git-bash for which bash completion is
815 installed and enabled.
816
817
818 bash/zsh prompt
819 ---------------
820
821 For command-line lovers shell prompt can carry a lot of useful
822 information. To include git information in the prompt use
823 `git-prompt.sh
824 <https://git.kernel.org/cgit/git/git.git/tree/contrib/completion/git-prompt.sh>`_.
825 Read the detailed instructions in the file.
826
827 Search the Net for "git prompt" to find other prompt variants.
828
829
830 git on server
831 =============
832
833 The simplest way to publish a repository or a group of repositories is
834 ``git daemon``. The daemon provides anonymous access, by default it is
835 read-only. The repositories are accessible by git protocol (git://
836 URLs). Write access can be enabled but the protocol lacks any
837 authentication means, so it should be enabled only within a trusted
838 LAN. See ``git help daemon`` for details.
839
840 Git over ssh provides authentication and repo-level authorisation as
841 repositories can be made user- or group-writeable (see parameter
842 ``core.sharedRepository`` in ``git help config``). If that's too
843 permissive or too restrictive for some project's needs there is a
844 wrapper `gitolite <http://gitolite.com/gitolite/index.html>`_ that can
845 be configured to allow access with great granularity; gitolite is
846 written in Perl and has a lot of documentation.
847
848 Web interface to browse repositories can be created using `gitweb
849 <https://git.kernel.org/cgit/git/git.git/tree/gitweb>`_ and `cgit
850 <http://git.zx2c4.com/cgit/about/>`_. Both are CGI scripts (written in
851 Perl and C). In addition to web interface both provide read-only dumb
852 http access for git (http(s):// URLs).
853
854 There are also more advanced web-based development environments that
855 include ability to manage users, groups and projects; private, group
856 and public repositories; they often include issue trackers, wiki
857 pages, pull requests and other tools for development and
858 communication. Among these environments are `Kallithea
859 <https://kallithea-scm.org/>`_ and `pagure <https://pagure.io/>`_,
860 both are written in Python; pagure was written by Fedora developers
861 and is being used to develop some Fedora projects. `Gogs
862 <http://gogs.io/>`_ is written in Go; there is a fork `Gitea
863 <http://gitea.io/>`_.
864
865 And last but not least `Gitlab <https://about.gitlab.com/>`_. It's
866 perhaps the most advanced web-based development environment for git.
867 Written in Ruby, community edition is free and open source (MIT
868 license).
869
870
871 From Mercurial to git
872 =====================
873
874 There are many tools to convert Mercurial repositories to git. The
875 most famous are, perhaps, `hg-git <https://hg-git.github.io/>`_ and
876 `fast-export <http://repo.or.cz/w/fast-export.git>`_ (many years ago
877 it was known under the name ``hg2git``).
878
879 But a better tool, perhaps the best, is `git-remote-hg
880 <https://github.com/felipec/git-remote-hg>`_. It provides transparent
881 bidirectional access (pull and push) to Mercurial repositories from
882 git. The author wrote a `comparison of alternatives
883 <https://github.com/felipec/git/wiki/Comparison-of-git-remote-hg-alternatives>`_
884 that seems to be mostly unbiased.
885
886 To use git-remote-hg, install or clone it, add to your PATH (or copy
887 script ``git-remote-hg`` to a directory that's already in PATH) and
888 prepend ``hg::`` to Mercurial URLs. For example::
889
890     $ git clone https://github.com/felipec/git-remote-hg.git
891     $ PATH=$PATH:"`pwd`"/git-remote-hg
892     $ git clone hg::https://hg.python.org/peps/ PEPs
893
894 To work with the repository just use regular git commands including
895 ``git fetch/pull/push``.
896
897 To start converting your Mercurial habits to git see the page
898 `Mercurial for Git users
899 <https://mercurial.selenic.com/wiki/GitConcepts>`_ at Mercurial wiki.
900 At the second half of the page there is a table that lists
901 corresponding Mercurial and git commands. Should work perfectly in
902 both directions.
903
904
905 Copyright
906 =========
907
908 This document has been placed in the public domain.
909
910
911 \f
912 ..
913    Local Variables:
914    mode: indented-text
915    indent-tabs-mode: nil
916    sentence-end-double-space: t
917    fill-column: 70
918    coding: utf-8
919    End:
920    vim: set fenc=us-ascii tw=70 :