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