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