2 Title: Collecting information about git
5 Author: Oleg Broytman <phd@phdru.name>
8 Content-Type: text/x-rst
10 Post-History: 12-Sep-2015
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.
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.
24 The author of the PEP doesn't currently plan to write a Process PEP on
25 migration Python development from Mercurial to git.
31 Git is accompanied with a lot of documentation, both online and
35 Documentation for starters
36 --------------------------
39 <https://www.kernel.org/pub/software/scm/git/docs/gittutorial.html>`_,
41 <https://www.kernel.org/pub/software/scm/git/docs/gittutorial-2.html>`_.
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>`_.
48 <https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html>`_.
51 Advanced documentation
52 ----------------------
55 <http://www-cs-students.stanford.edu/~blynn/gitmagic/index.html>`_,
56 with a number of translations.
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>`_.
63 `Git Wiki <https://git.wiki.kernel.org/index.php/Main_Page>`_.
65 `Git Buch <http://gitbu.ch/index.html>`_ (German).
71 Git has builtin help: run ``git help $TOPIC``. For example, run
72 ``git help git`` or ``git help help``.
78 Download and installation
79 -------------------------
81 Unix users: `download and install using your package manager
82 <https://git-scm.com/download/linux>`_.
84 Microsoft Windows: download `git-for-windows
85 <https://github.com/git-for-windows/git/releases>`_.
87 MacOS X: use git installed with `XCode
88 <https://developer.apple.com/xcode/downloads/>`_ or download from
89 `MacPorts <https://www.macports.org/ports.php?by=name&substr=git>`_ or
91 <http://sourceforge.net/projects/git-osx-installer/files/>`_ or
92 install git with `Homebrew <http://brew.sh/>`_: ``brew install git``.
94 `git-cola <https://git-cola.github.io/index.html>`_ is a Git GUI
95 written in Python and GPL licensed. Linux, Windows, MacOS X.
97 `TortoiseGit <https://tortoisegit.org/>`_ is a Windows Shell Interface
98 to Git based on TortoiseSVN; open source.
101 Initial configuration
102 ---------------------
104 This simple code is often appears in documentation, but it is
105 important so let repeat it here. Git stores author and committer
106 names/emails in every commit, so configure your real name and
109 $ git config --global user.name "User Name"
110 $ git config --global user.email user.name@example.org
116 Examples of git commands in this PEP use the following approach. It is
117 supposed that you, the user, works with a local repository named
118 ``python`` that has an upstream remote repo named ``origin``. Your
119 local repo has two branches ``v1`` and ``master``. For most examples
120 the currently checked out branch is ``master``. That is, it's assumed
121 you have done something like that::
123 $ git clone https://git.python.org/python.git
125 $ git branch v1 origin/v1
127 The first command clones remote repository into local directory
128 `python``, creates a new local branch master, sets
129 remotes/origin/master as its upstream remote-tracking branch and
130 checks it out into the working directory.
132 The last command creates a new local branch v1 and sets
133 remotes/origin/v1 as its upstream remote-tracking branch.
135 The same result can be achieved with commands::
137 $ git clone -b v1 https://git.python.org/python.git
139 $ git checkout --track origin/master
141 The last command creates a new local branch master, sets
142 remotes/origin/master as its upstream remote-tracking branch and
143 checks it out into the working directory.
146 Branches and branches
147 =====================
149 Git terminology can be a bit misleading. Take, for example, the term
150 "branch". In git it has two meanings. A branch is a directed line of
151 commits (possibly with merges). And a branch is a label or a pointer
152 assigned to a line of commits. It is important to distinguish when you
153 talk about commits and when about their labels. Lines of commits are
154 by itself unnamed and are usually only lengthening and merging.
155 Labels, on the other hand, can be created, moved, renamed and deleted
159 Remote repositories and remote branches
160 =======================================
162 Remote-tracking branches are branches (pointers to commits) in your
163 local repository. They are there for git (and for you) to remember
164 what branches and commits have been pulled from and pushed to what
165 remote repos (you can pull from and push to many remotes).
166 Remote-tracking branches live under ``remotes/$REMOTE`` namespaces,
167 e.g. ``remotes/origin/master``.
169 To see the status of remote-tracking branches run::
173 To see local and remote-tracking branches (and tags) pointing to
178 You never do your own development on remote-tracking branches. You
179 create a local branch that has a remote branch as upstream and do
180 development on that local branch. On push git pushes commits to the
181 remote repo and updates remote-tracking branches, on pull git fetches
182 commits from the remote repo, updates remote-tracking branches and
183 fast-forwards, merges or rebases local branches.
185 When you do an initial clone like this::
187 $ git clone -b v1 https://git.python.org/python.git
189 git clones remote repository ``https://git.python.org/python.git`` to
190 directory ``python``, creates a remote named ``origin``, creates
191 remote-tracking branches, creates a local branch ``v1``, configure it
192 to track upstream remotes/origin/v1 branch and checks out ``v1`` into
193 the working directory.
195 Some commands, like ``git status --branch`` and ``git branch --verbose``,
196 report the difference between local and remote branches.
197 Please remember they only do comparison with remote-tracking branches
198 in your local repository, and the state of those remote-tracking
199 branches can be outdated. To update remote-tracking branches you
200 either fetch and merge (or rebase) commits from the remote repository
201 or update remote-tracking branches without updating local branches.
204 Updating local and remote-tracking branches
205 -------------------------------------------
207 To update remote-tracking branches without updating local branches run
208 ``git remote update [$REMOTE...]``. For example::
211 $ git remote update origin
217 There is a major difference between
221 $ git fetch $REMOTE $BRANCH
227 $ git fetch $REMOTE $BRANCH:$BRANCH
229 The first command fetches commits from the named $BRANCH in the
230 $REMOTE repository that are not in your repository, updates
231 remote-tracking branch and leaves the id (the hash) of the head commit
232 in file .git/FETCH_HEAD.
234 The second command fetches commits from the named $BRANCH in the
235 $REMOTE repository that are not in your repository and updates both
236 the local branch $BRANCH and its upstream remote-tracking branch. But
237 it refuses to update branches in case of non-fast-forward. And it
238 refuses to update the current branch (currently checked out branch,
239 where HEAD is pointing to).
241 The first command is used internally by ``git pull``.
245 $ git pull $REMOTE $BRANCH
251 $ git fetch $REMOTE $BRANCH
252 $ git merge FETCH_HEAD
254 Certainly, $BRANCH in that case should be your current branch. If you
255 want to merge a different branch into your current branch first update
256 that non-current branch and then merge::
258 $ git fetch origin v1:v1 # Update v1
259 $ git pull --rebase origin master # Update the current branch master
260 # using rebase instead of merge
263 If you have not yet pushed commits on ``v1``, though, the scenario has
264 to become a bit more complex. Git refuses to update
265 non-fast-forwardable branch, and you don't want to do force-pull
266 because that would remove your non-pushed commits and you would need
267 to recover. So you want to rebase ``v1`` but you cannot rebase
268 non-current branch. Hence, checkout ``v1`` and rebase it before
272 $ git pull --rebase origin v1
273 $ git checkout master
274 $ git pull --rebase origin master
277 It is possible to configure git to make it fetch/pull a few branches
278 or all branches at once, so you can simply run
290 Default remote repository for fetching/pulling is ``origin``. Default
291 set of references to fetch is calculated using matching algorithm: git
292 fetches all branches having the same name on both ends.
298 Pushing is a bit simpler. There is only one command ``push``. When you
303 $ git push origin v1 master
305 git pushes local v1 to remote v1 and local master to remote master.
308 $ git push origin v1:v1 master:master
310 Git pushes commits to the remote repo and updates remote-tracking
311 branches. Git refuses to push commits that aren't fast-forwardable.
312 You can force-push anyway, but please remember - you can force-push to
313 your own repositories but don't force-push to public or shared repos.
314 If you find git refuses to push commits that aren't fast-forwardable,
315 better fetch and merge commits from the remote repo (or rebase your
316 commits on top of the fetched commits), then push. Only force-push if
317 you know what you do and why you do it. See the section `Commit
318 editing and caveats`_ below.
320 It is possible to configure git to make it push a few branches or all
321 branches at once, so you can simply run
333 Default remote repository for pushing is ``origin``. Default set of
334 references to push in git before 2.0 is calculated using matching
335 algorithm: git pushes all branches having the same name on both ends.
336 Default set of references to push in git 2.0+ is calculated using
337 simple algorithm: git pushes the current branch back to its
340 To configure git before 2.0 to the new behaviour run::
342 $ git config push.default simple
344 To configure git 2.0+ to the old behaviour run::
346 $ git config push.default matching
348 Git doesn't allow to push a branch if it's the current branch in the
349 remote non-bare repository: git refuses to update remote working
350 directory. You really should push only to bare repositories. For
351 non-bare repositories git prefers pull-based workflow.
353 When you want to deploy code on a remote host and can only use push
354 (because your workstation is behind a firewall and you cannot pull
355 from it) you do that in two steps using two repositories: you push
356 from the workstation to a bare repo on the remote host, ssh to the
357 remote host and pull from the bare repo to a non-bare deployment repo.
359 That changed in git 2.3, but see `the blog post
360 <https://github.com/blog/1957-git-2-3-has-been-released#push-to-deploy>`_
361 for caveats; in 2.4 the push-to-deploy feature was `further improved
362 <https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more#push-to-deploy-improvements>`_.
368 Git automatically fetches tags that point to commits being fetched
369 during fetch/pull. To fetch all tags (and commits they point to) run
370 ``git fetch --tags origin``. To fetch some specific tags fetch them
373 $ git fetch origin tag $TAG1 tag $TAG2...
377 $ git fetch origin tag 1.4.2
378 $ git fetch origin v1:v1 tag 2.1.7
380 Git doesn't automatically pushes tags. That allows you to have private
381 tags. To push tags list them explicitly::
383 $ git push origin tag 1.4.2
384 $ git push origin v1 master tag 2.1.7
386 Or push all tags at once::
388 $ git push --tags origin
390 Don't move tags with ``git tag -f`` or remove tags with ``git tag -d``
391 after they have been published.
397 When cloning/fetching/pulling/pushing git copies only database objects
398 (commits, trees, files and tags) and symbolic references (branches and
399 lightweight tags). Everything else is private to the repository and
400 never cloned, updated or pushed. It's your config, your hooks, your
401 private exclude file.
403 If you want to distribute hooks, copy them to the working tree, add,
404 commit, push and instruct the team to update and install the hooks
408 Commit editing and caveats
409 ==========================
411 A warning not to edit published (pushed) commits also appears in
412 documentation but it's repeated here anyway as it's very important.
414 It is possible to recover from a forced push but it's PITA for the
415 entire team. Please avoid it.
417 To see what commits have not been published yet compare the head of the
418 branch with its upstream remote-tracking branch::
420 $ git log origin/master.. # from origin/master to HEAD (of master)
421 $ git log origin/v1..v1 # from origin/v1 to the head of v1
423 For every branch that has an upstream remote-tracking branch git
424 maintains an alias @{upstream} (short version @{u}), so the commands
425 above can be given as::
430 To see the status of all branches::
434 To compare the status of local branches with a remote repo::
436 $ git remote show origin
438 Read `how to recover from upstream rebase
439 <https://git-scm.com/docs/git-rebase#_recovering_from_upstream_rebase>`_.
440 It is in ``git help rebase``.
442 On the other hand don't be too afraid about commit editing. You can
443 safely edit, reorder, remove, combine and split commits that haven't
444 been pushed yet. You can even push commits to your own (backup) repo,
445 edit them later and force-push edited commits to replace what have
446 already been pushed. Not a problem until commits are in a public
447 or shared repository.
453 Whatever you do, don't panic. Almost anything in git can be undone.
456 git checkout: restore file's content
457 ------------------------------------
459 ``git checkout``, for example, can be used to restore the content of
460 file(s) to that one of a commit. Like this::
462 git checkout HEAD~ README
464 The commands restores the contents of README file to the last but one
465 commit in the current branch. By default the commit ID is simply HEAD;
466 i.e. ``git checkout README`` restores README to the latest commit.
468 (Do not use ``git checkout`` to view a content of a file in a commit,
469 use ``git cat-file -p``; e.g. ``git cat-file -p HEAD~:path/to/README``).
472 git reset: remove (non-pushed) commits
473 --------------------------------------
475 ``git reset`` moves the head of the current branch. The head can be
476 moved to point to any commit but it's often used to remove a commit or
477 a few (preferably, non-pushed ones) from the top of the branch - that
478 is, to move the branch backward in order to undo a few (non-pushed)
481 ``git reset`` has three modes of operation - soft, hard and mixed.
482 Default is mixed. ProGit `explains
483 <https://git-scm.com/book/en/Git-Tools-Reset-Demystified>`_ the
484 difference very clearly. Bare repositories don't have indices or
485 working trees so in a bare repo only soft reset is possible.
491 Mixed mode reset with a path or paths can be used to unstage changes -
492 that is, to remove from index changes added with ``git add`` for
493 committing. See `The Book
494 <https://git-scm.com/book/en/Git-Basics-Undoing-Things>`_ for details
495 about unstaging and other undo tricks.
498 git reflog: reference log
499 -------------------------
501 Removing commits with ``git reset`` or moving the head of a branch
502 sounds dangerous and it is. But there is a way to undo: another
503 reset back to the original commit. Git doesn't remove commits
504 immediately; unreferenced commits (in git terminology they are called
505 "dangling commits") stay in the database for some time (default is two
506 weeks) so you can reset back to it or create a new branch pointing to
509 For every move of a branch's head - with ``git commit``, ``git
510 checkout``, ``git fetch``, ``git pull``, ``git rebase``, ``git reset``
511 and so on - git stores a reference log (reflog for short). For every
512 move git stores where the head was. Command ``git reflog`` can be used
513 to view (and manipulate) the log.
515 In addition to the moves of the head of every branch git stores the
516 moves of the HEAD - a symbolic reference that (usually) names the
517 current branch. HEAD is changed with ``git checkout $BRANCH``.
519 By default ``git reflog`` shows the moves of the HEAD, i.e. the
520 command is equivalent to ``git reflog HEAD``. To show the moves of the
521 head of a branch use the command ``git reflog $BRANCH``.
523 So to undo a ``git reset`` lookup the original commit in ``git
524 reflog``, verify it with ``git show`` or ``git log`` and run ``git
525 reset $COMMIT_ID``. Git stores the move of the branch's head in
526 reflog, so you can undo that undo later again.
528 In a more complex situation you'd want to move some commits along with
529 resetting the head of the branch. Cherry-pick them to the new branch.
530 For example, if you want to reset the branch ``master`` back to the
531 original commit but preserve two commits created in the current branch
534 $ git branch save-master # create a new branch saving master
535 $ git reflog # find the original place of master
536 $ git reset $COMMIT_ID
537 $ git cherry-pick save-master~ save-master
538 $ git branch -D save-master # remove temporary branch
541 git revert: revert a commit
542 ---------------------------
544 ``git revert`` reverts a commit or commits, that is, it creates a new
545 commit or commits that revert(s) the effects of the given commits.
546 It's the only way to undo published commits (``git commit --amend``,
547 ``git rebase`` and ``git reset`` change the branch in
548 non-fast-forwardable ways so they should only be used for non-pushed
551 There is a problem with reverting a merge commit. ``git revert`` can
552 undo the code created by the merge commit but it cannot undo the fact
553 of merge. See the discussion `How to revert a faulty merge
554 <https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html>`_.
557 One thing that cannot be undone
558 -------------------------------
560 Whatever you undo, there is one thing that cannot be undone -
561 overwritten uncommitted changes. Uncommitted changes don't belong to
562 git so git cannot help preserving them.
564 Most of the time git warns you when you're going to execute a command
565 that overwrites uncommitted changes. Git doesn't allow you to switch
566 branches with ``git checkout``. It stops you when you're going to
567 rebase with non-clean working tree. It refuses to pull new commits
568 over non-committed files.
570 But there are commands that do exactly that - overwrite files in the
571 working tree. Commands like ``git checkout $PATHs`` or ``git reset
572 --hard`` silently overwrite files including your uncommitted changes.
574 With that in mind you can understand the stance "commit early, commit
575 often". Commit as often as possible. Commit on every save in your
576 editor or IDE. You can edit your commits before pushing - edit commit
577 messages, change commits, reorder, combine, split, remove. But save
578 your changes in git database, either commit changes or at least stash
579 them with ``git stash``.
585 Internet is full of heated discussions on the topic: "merge or
586 rebase?" Most of them are meaningless. When a DVCS is being used in a
587 big team with a big and complex project with many branches there is
588 simply no way to avoid merges. So the question's diminished to
589 "whether to use rebase, and if yes - when to use rebase?" Considering
590 that it is very much recommended not to rebase published commits the
591 question's diminished even further: "whether to use rebase on
594 That small question is for the team to decide. To preserve the beauty
595 of linear history it's recommended to use rebase when pulling, i.e. do
596 ``git pull --rebase`` or even configure automatic setup of rebase for
599 $ git config branch.autosetuprebase always
601 and configure rebase for existing branches::
603 $ git config branch.$NAME.rebase true
607 $ git config branch.v1.rebase true
608 $ git config branch.master.rebase true
610 After that ``git pull origin master`` becomes equivalent to ``git pull
611 --rebase origin master``.
613 It is recommended to create new commits in a separate feature or topic
614 branch while using rebase to update the mainline branch. When the
615 topic branch is ready merge it into mainline. To avoid a tedious task
616 of resolving large number of conflicts at once you can merge the topic
617 branch to the mainline from time to time and switch back to the topic
618 branch to continue working on it. The entire workflow would be
621 $ git checkout -b issue-42 # create a new issue branch and switch to it
622 ...edit/test/commit...
623 $ git checkout master
624 $ git pull --rebase origin master # update master from the upstream
626 $ git branch -d issue-42 # delete the topic branch
627 $ git push origin master
629 When the topic branch is deleted only the label is removed, commits
630 are stayed in the database, they are now merged into master::
632 o--o--o--o--o--M--< master - the mainline branch
634 --*--*--* - the topic branch, now unnamed
636 The topic branch is deleted to avoid cluttering branch namespace with
637 small topic branches. Information on what issue was fixed or what
638 feature was implemented should be in the commit messages.
640 But even that small amount of rebasing could be too big in case of
641 long-lived merged branches. Imagine you're doing work in both ``v1``
642 and ``master`` branches, regularly merging ``v1`` into ``master``.
643 After some time you will have a lot of merge and non-merge commits in
644 ``master``. Then you want to push your finished work to a shared
645 repository and find someone has pushed a few commits to ``v1``. Now
646 you have a choice of two equally bad alternatives: either you fetch
647 and rebase ``v1`` and then have to recreate all you work in ``master``
648 (reset ``master`` to the origin, merge ``v1`` and cherry-pick all
649 non-merge commits from the old master); or merge the new ``v1`` and
650 loose the beauty of linear history.
656 Git has a builtin merge strategy for what Python core developers call
659 $ git merge -s ours v1 # null-merge v1 into master
665 Git doesn't assume any particular development model regarding
666 branching and merging. Some projects prefer to graduate patches from
667 the oldest branch to the newest, some prefer to cherry-pick commits
668 backwards, some use squashing (combining a number of commits into
669 one). Anything is possible.
671 There are a few examples to start with. `git help workflows
672 <https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html>`_
673 describes how the very git authors develop git.
675 ProGit book has a few chapters devoted to branch management in
676 different projects: `Git Branching - Branching Workflows
677 <https://git-scm.com/book/en/Git-Branching-Branching-Workflows>`_ and
678 `Distributed Git - Contributing to a Project
679 <https://git-scm.com/book/en/Distributed-Git-Contributing-to-a-Project>`_.
681 There is also a well-known article `A successful Git branching model
682 <http://nvie.com/posts/a-successful-git-branching-model/>`_ by Vincent
683 Driessen. It recommends a set of very detailed rules on creating and
684 managing mainline, topic and bugfix branches. To support the model the
685 author implemented `git flow <https://github.com/nvie/gitflow>`_
689 Advanced configuration
690 ======================
695 Git has builtin mechanisms to handle line endings between platforms
696 with different end-of-line styles. To allow git to do CRLF conversion
697 assign ``text`` attribute to files using `.gitattributes
698 <https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html>`_.
699 For files that have to have specific line endings assign ``eol``
700 attribute. For binary files the attribute is, naturally, ``binary``.
710 To check what attributes git uses for files use ``git check-attr``
711 command. For example::
713 $ git check-attr -a -- \*.py
719 `GitAlias <http://gitalias.com/>`_ (`repository
720 <https://github.com/GitAlias/gitalias>`_) is a big collection of
721 aliases. A careful selection of aliases for frequently used commands
722 could save you a lot of keystrokes!
724 `GitIgnore <https://www.gitignore.io/>`_ and
725 https://github.com/github/gitignore are collections of ``.gitignore``
726 files for all kinds of IDEs and programming languages. Python
729 `pre-commit <http://pre-commit.com/>`_ (`repositories
730 <https://github.com/pre-commit>`_) is a framework for managing and
731 maintaining multi-language pre-commit hooks. The framework is written
732 in Python and has a lot of plugins for many programming languages.
741 Staging area aka index aka cache is a distinguishing feature of git.
742 Staging area is where git collects patches before committing them.
743 Separation between collecting patches and commit phases provides a
744 very useful feature of git: you can review collected patches before
745 commit and even edit them - remove some hunks, add new hunks and
748 To add files to the index use ``git add``. Collecting patches before
749 committing means you need to do that for every change, not only to add
750 new (untracked) files. To simplify committing in case you just want to
751 commit everything without reviewing run ``git commit --all`` (or just
752 ``-a``) - the command adds every changed tracked file to the index and
753 then commit. To commit a file or files regardless of patches collected
754 in the index run ``git commit [--only|-o] -- $FILE...``.
756 To add hunks of patches to the index use ``git add --patch`` (or just
757 ``-p``). To remove collected files from the index use ``git reset HEAD
758 -- $FILE...`` To add/inspect/remove collected hunks use ``git add
759 --interactive`` (``-i``).
761 To see the diff between the index and the last commit (i.e., collected
762 patches) use ``git diff --cached``. To see the diff between the
763 working tree and the index (i.e., uncollected patches) use just ``git
764 diff``. To see the diff between the working tree and the last commit
765 (i.e., both collected and uncollected patches) run ``git diff HEAD``.
768 <https://git.wiki.kernel.org/index.php/WhatIsTheIndex>`_ and
769 `IndexCommandQuickref
770 <https://git.wiki.kernel.org/index.php/IndexCommandQuickref>`_ in Git
777 Git switches to the root (top-level directory of the project where
778 ``.git`` subdirectory exists) before running any command. Git
779 remembers though the directory that was current before the switch.
780 Some programs take into account the current directory. E.g., ``git
781 status`` shows file paths of changed and unknown files relative to the
782 current directory; ``git grep`` searches below the current directory;
783 ``git apply`` applies only those hunks from the patch that touch files
784 below the current directory.
786 But most commands run from the root and ignore the current directory.
787 Imagine, for example, that you have two work trees, one for the branch
788 ``v1`` and the other for ``master``. If you want to merge ``v1`` from
789 a subdirectory inside the second work tree you must write commands as
790 if you're in the top-level dir. Let take two work trees,
791 ``project-v1`` and ``project``, for example::
793 $ cd project/subdirectory
794 $ git fetch ../project-v1 v1:v1
797 Please note the path in ``git fetch ../project-v1 v1:v1`` is
798 ``../project-v1`` and not ``../../project-v1`` despite the fact that
799 we run the commands from a subdirectory, not from the root.
805 Rerere is a mechanism that helps to resolve repeated merge conflicts.
806 The most frequent source of recurring merge conflicts are topic
807 branches that are merged into mainline and then the merge commits are
808 removed; that's often performed to test the topic branches and train
809 rerere; merge commits are removed to have clean linear history and
810 finish the topic branch with only one last merge commit.
812 Rerere works by remembering the states of tree before and after a
813 successful commit. That way rerere can automatically resolve conflicts
814 if they appear in the same files.
816 Rerere can be used manually with ``git rerere`` command but most often
817 it's used automatically. Enable rerere with these commands in a
820 $ git config rerere.enabled true
821 $ git config rerere.autoupdate true
823 You don't need to turn rerere on globally - you don't want rerere in
824 bare repositories or single-branch repositories; you only need rerere
825 in repos where you often perform merges and resolve merge conflicts.
827 See `Rerere <https://git-scm.com/book/en/Git-Tools-Rerere>`_ in The
834 Git object database and other files/directories under ``.git`` require
835 periodic maintenance and cleanup. For example, commit editing left
836 unreferenced objects (dangling objects, in git terminology) and these
837 objects should be pruned to avoid collecting cruft in the DB. The
838 command ``git gc`` is used for maintenance. Git automatically runs
839 ``git gc --auto`` as a part of some commands to do quick maintenance.
840 Users are recommended to run ``git gc --aggressive`` from time to
841 time; ``git help gc`` recommends to run it every few hundred
842 changesets; for more intensive projects it should be something like
843 once a week and less frequently (biweekly or monthly) for lesser
846 ``git gc --aggressive`` not only removes dangling objects, it also
847 repacks object database into indexed and better optimized pack(s); it
848 also packs symbolic references (branches and tags). Another way to do
849 it is to run ``git repack``.
851 There is a well-known `message
852 <https://gcc.gnu.org/ml/gcc/2007-12/msg00165.html>`_ from Linus
853 Torvalds regarding "stupidity" of ``git gc --aggressive``. The message
854 can safely be ignored now. It is old and outdated, ``git gc
855 --aggressive`` became much better since that time.
857 For those who still prefer ``git repack`` over ``git gc --aggressive``
858 the recommended parameters are ``git repack -a -d -f --depth=20
859 --window=250``. See `this detailed experiment
860 <http://vcscompare.blogspot.ru/2008/06/git-repack-parameters.html>`_
861 for explanation of the effects of these parameters.
863 From time to time run ``git fsck [--strict]`` to verify integrity of
864 the database. ``git fsck`` may produce a list of dangling objects;
865 that's not an error, just a reminder to perform regular maintenance.
871 Command-line options and arguments
872 ----------------------------------
875 <https://www.kernel.org/pub/software/scm/git/docs/gitcli.html>`_
876 recommends not to combine short options/flags. Most of the times
877 combining works: ``git commit -av`` works perfectly, but there are
878 situations when it doesn't. E.g., ``git log -p -5`` cannot be combined
881 Some options have arguments, some even have default arguments. In that
882 case the argument for such option must be spelled in a sticky way:
883 ``-Oarg``, never ``-O arg`` because for an option that has a default
884 argument the latter means "use default value for option ``-O`` and
885 pass ``arg`` further to the option parser". For example, ``git grep``
886 has an option ``-O`` that passes a list of names of the found files to
887 a program; default program for ``-O`` is a pager (usually ``less``),
888 but you can use your editor::
890 $ git grep -Ovim # but not -O vim
892 BTW, if git is instructed to use ``less`` as the pager (i.e., if pager
893 is not configured in git at all it uses ``less`` by default, or if it
894 gets ``less`` from GIT_PAGER or PAGER environment variables, or if it
895 was configured with ``git config [--global] core.pager less``, or
896 ``less`` is used in the command ``git grep -Oless``) ``git grep``
897 passes ``+/$pattern`` option to ``less`` which is quite convenient.
898 Unfortunately, ``git grep`` doesn't pass the pattern if the pager is
899 not exactly ``less``, even if it's ``less`` with parameters (something
900 like ``git config [--global] core.pager less -FRSXgimq``); fortunately,
901 ``git grep -Oless`` always passes the pattern.
907 It's a bit hard to type ``git rebase --interactive --preserve-merges
908 HEAD~5`` manually even for those who are happy to use command-line,
909 and this is where shell completion is of great help. Bash/zsh come
910 with programmable completion, often automatically installed and
911 enabled, so if you have bash/zsh and git installed, chances are you
912 are already done - just go and use it at the command-line.
914 If you don't have necessary bits installed, install and enable
915 bash_completion package. If you want to upgrade your git completion to
916 the latest and greatest download necessary file from `git contrib
917 <https://git.kernel.org/cgit/git/git.git/tree/contrib/completion>`_.
919 Git-for-windows comes with git-bash for which bash completion is
920 installed and enabled.
926 For command-line lovers shell prompt can carry a lot of useful
927 information. To include git information in the prompt use
929 <https://git.kernel.org/cgit/git/git.git/tree/contrib/completion/git-prompt.sh>`_.
930 Read the detailed instructions in the file.
932 Search the Net for "git prompt" to find other prompt variants.
938 The simplest way to publish a repository or a group of repositories is
939 ``git daemon``. The daemon provides anonymous access, by default it is
940 read-only. The repositories are accessible by git protocol (git://
941 URLs). Write access can be enabled but the protocol lacks any
942 authentication means, so it should be enabled only within a trusted
943 LAN. See ``git help daemon`` for details.
945 Git over ssh provides authentication and repo-level authorisation as
946 repositories can be made user- or group-writeable (see parameter
947 ``core.sharedRepository`` in ``git help config``). If that's too
948 permissive or too restrictive for some project's needs there is a
949 wrapper `gitolite <http://gitolite.com/gitolite/index.html>`_ that can
950 be configured to allow access with great granularity; gitolite is
951 written in Perl and has a lot of documentation.
953 Web interface to browse repositories can be created using `gitweb
954 <https://git.kernel.org/cgit/git/git.git/tree/gitweb>`_ or `cgit
955 <http://git.zx2c4.com/cgit/about/>`_. Both are CGI scripts (written in
956 Perl and C). In addition to web interface both provide read-only dumb
957 http access for git (http(s):// URLs). `Klaus
958 <https://pypi.python.org/pypi/klaus>`_ is a small and simple WSGI web
959 server that implements both web interface and git smart HTTP
960 transport; supports Python 2 and Python 3, performs syntax
963 There are also more advanced web-based development environments that
964 include ability to manage users, groups and projects; private,
965 group-accessible and public repositories; they often include issue
966 trackers, wiki pages, pull requests and other tools for development
967 and communication. Among these environments are `Kallithea
968 <https://kallithea-scm.org/>`_ and `pagure <https://pagure.io/>`_,
969 both are written in Python; pagure was written by Fedora developers
970 and is being used to develop some Fedora projects. `GitPrep
971 <http://gitprep.yukikimoto.com/>`_ is yet another Github clone,
972 written in Perl. `Gogs <https://gogs.io/>`_ is written in Go.
973 `GitBucket <https://takezoe.github.io/gitbucket/about/>`_ is written
976 And last but not least, `Gitlab <https://about.gitlab.com/>`_. It's
977 perhaps the most advanced web-based development environment for git.
978 Written in Ruby, community edition is free and open source (MIT
982 From Mercurial to git
983 =====================
985 There are many tools to convert Mercurial repositories to git. The
986 most famous are, probably, `hg-git <https://hg-git.github.io/>`_ and
987 `fast-export <http://repo.or.cz/w/fast-export.git>`_ (many years ago
988 it was known under the name ``hg2git``).
990 But a better tool, perhaps the best, is `git-remote-hg
991 <https://github.com/felipec/git-remote-hg>`_. It provides transparent
992 bidirectional (pull and push) access to Mercurial repositories from
993 git. Its author wrote a `comparison of alternatives
994 <https://github.com/felipec/git/wiki/Comparison-of-git-remote-hg-alternatives>`_
995 that seems to be mostly objective.
997 To use git-remote-hg, install or clone it, add to your PATH (or copy
998 script ``git-remote-hg`` to a directory that's already in PATH) and
999 prepend ``hg::`` to Mercurial URLs. For example::
1001 $ git clone https://github.com/felipec/git-remote-hg.git
1002 $ PATH=$PATH:"`pwd`"/git-remote-hg
1003 $ git clone hg::https://hg.python.org/peps/ PEPs
1005 To work with the repository just use regular git commands including
1006 ``git fetch/pull/push``.
1008 To start converting your Mercurial habits to git see the page
1009 `Mercurial for Git users
1010 <https://mercurial.selenic.com/wiki/GitConcepts>`_ at Mercurial wiki.
1011 At the second half of the page there is a table that lists
1012 corresponding Mercurial and git commands. Should work perfectly in
1015 Python Developer's Guide also has a chapter `Mercurial for git
1016 developers <https://docs.python.org/devguide/gitdevs.html>`_ that
1017 documents a few differences between git and hg.
1023 This document has been placed in the public domain.
1030 indent-tabs-mode: nil
1031 sentence-end-double-space: t
1035 vim: set fenc=us-ascii tw=70 :