6 This page (it was PEP 103) collects information about git. There is, of
7 course, a lot of documentation for git, so the PEP concentrates on
8 more complex (and more related to Python development) issues,
9 scenarios and examples.
15 Git is accompanied with a lot of documentation, both online and
19 Documentation for starters
20 --------------------------
23 <https://www.kernel.org/pub/software/scm/git/docs/gittutorial.html>`_,
25 <https://www.kernel.org/pub/software/scm/git/docs/gittutorial-2.html>`_.
28 <https://www.kernel.org/pub/software/scm/git/docs/user-manual.html>`_.
29 `Everyday GIT With 20 Commands Or So
30 <https://www.kernel.org/pub/software/scm/git/docs/giteveryday.html>`_.
32 <https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html>`_.
35 Advanced documentation
36 ----------------------
39 <http://www-cs-students.stanford.edu/~blynn/gitmagic/index.html>`_,
40 with a number of translations.
42 `Pro Git <https://git-scm.com/book>`_. The Book about git. Buy it at
43 Amazon or download in PDF, mobi, or ePub form. It has translations to
44 many different languages. Download Russian translation from `GArik
45 <https://github.com/GArik/progit/wiki>`_.
47 `Git Wiki <https://git.wiki.kernel.org/index.php/Main_Page>`_.
49 `Git Buch <http://gitbu.ch/index.html>`_ (German).
55 Run ``git help $TOPIC``. For example, run ``git help git`` or
56 ``git help help``. Run ``git help -a`` to list help topics for all
57 available commands; ``git help -g`` to list help guides, i.e. help topics
64 Download and installation
65 -------------------------
67 Unix users: `download and install using your package manager
68 <https://git-scm.com/download/linux>`_.
70 Microsoft Windows: download `git-for-windows
71 <https://github.com/git-for-windows/git/releases>`_.
73 MacOS X: use git installed with `XCode
74 <https://developer.apple.com/xcode/>`_ or download from `MacPorts
75 <https://www.macports.org/ports.php?by=name&substr=git>`_ or
77 <http://sourceforge.net/projects/git-osx-installer/files/>`_ or
78 install git with `Homebrew <http://brew.sh/>`_: ``brew install git``.
80 `git-cola <https://git-cola.github.io/index.html>`_ (`repository
81 <https://github.com/git-cola/git-cola>`__) is a Git GUI written in
82 Python and GPL licensed. Linux, Windows, MacOS X.
84 `TortoiseGit <https://tortoisegit.org/>`_ is a Windows Shell Interface
85 to Git based on TortoiseSVN; open source.
91 This simple code is often appears in documentation, but it is
92 important so let repeat it here. Git stores author and committer
93 names/emails in every commit, so configure your real name and
96 $ git config --global user.name "User Name"
97 $ git config --global user.email user.name@example.org
103 Examples of git commands in this PEP use the following approach. It is
104 supposed that you, the user, works with a local repository named
105 ``python`` that has an upstream remote repo named ``origin``. Your
106 local repo has two branches ``v1`` and ``master``. For most examples
107 the currently checked out branch is ``master``. That is, it's assumed
108 you have done something like that::
110 $ git clone https://git.python.org/python.git
112 $ git branch v1 origin/v1
114 The first command clones remote repository into local directory
115 `python``, creates a new local branch master, sets
116 remotes/origin/master as its upstream remote-tracking branch and
117 checks it out into the working directory.
119 The last command creates a new local branch v1 and sets
120 remotes/origin/v1 as its upstream remote-tracking branch.
122 The same result can be achieved with commands::
124 $ git clone -b v1 https://git.python.org/python.git
126 $ git checkout --track origin/master
128 The last command 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.
133 Branches and branches
134 =====================
136 Git terminology can be a bit misleading. Take, for example, the term
137 "branch". In git it has two meanings. A branch is a directed line of
138 commits (possibly with merges). And a branch is a label or a pointer
139 assigned to a line of commits. It is important to distinguish when you
140 talk about commits and when about their labels. Lines of commits are
141 by itself unnamed and are usually only lengthening and merging.
142 Labels, on the other hand, can be created, moved, renamed and deleted
146 Remote repositories and remote branches
147 =======================================
149 Remote-tracking branches are branches (pointers to commits) in your
150 local repository. They are there for git (and for you) to remember
151 what branches and commits have been pulled from and pushed to what
152 remote repos (you can pull from and push to many remotes).
153 Remote-tracking branches live under ``remotes/$REMOTE`` namespaces,
154 e.g. ``remotes/origin/master``.
156 To see the status of remote-tracking branches run::
160 To see local and remote-tracking branches (and tags) pointing to
165 You never do your own development on remote-tracking branches. You
166 create a local branch that has a remote branch as upstream and do
167 development on that local branch. On push git pushes commits to the
168 remote repo and updates remote-tracking branches, on pull git fetches
169 commits from the remote repo, updates remote-tracking branches and
170 fast-forwards, merges or rebases local branches.
172 When you do an initial clone like this::
174 $ git clone -b v1 https://git.python.org/python.git
176 git clones remote repository ``https://git.python.org/python.git`` to
177 directory ``python``, creates a remote named ``origin``, creates
178 remote-tracking branches, creates a local branch ``v1``, configure it
179 to track upstream remotes/origin/v1 branch and checks out ``v1`` into
180 the working directory.
182 Some commands, like ``git status --branch`` and ``git branch --verbose``,
183 report the difference between local and remote branches.
184 Please remember they only do comparison with remote-tracking branches
185 in your local repository, and the state of those remote-tracking
186 branches can be outdated. To update remote-tracking branches you
187 either fetch and merge (or rebase) commits from the remote repository
188 or update remote-tracking branches without updating local branches.
191 Updating local and remote-tracking branches
192 -------------------------------------------
194 To update remote-tracking branches without updating local branches run
195 ``git remote update [$REMOTE...]``. For example::
198 $ git remote update origin
204 There is a major difference between
208 $ git fetch $REMOTE $BRANCH
214 $ git fetch $REMOTE $BRANCH:$BRANCH
216 The first command fetches commits from the named $BRANCH in the
217 $REMOTE repository that are not in your repository, updates
218 remote-tracking branch and leaves the id (the hash) of the head commit
219 in file .git/FETCH_HEAD.
221 The second command fetches commits from the named $BRANCH in the
222 $REMOTE repository that are not in your repository and updates both
223 the local branch $BRANCH and its upstream remote-tracking branch. But
224 it refuses to update branches in case of non-fast-forward. And it
225 refuses to update the current branch (currently checked out branch,
226 where HEAD is pointing to).
228 The first command is used internally by ``git pull``.
232 $ git pull $REMOTE $BRANCH
238 $ git fetch $REMOTE $BRANCH
239 $ git merge FETCH_HEAD
241 Certainly, $BRANCH in that case should be your current branch. If you
242 want to merge a different branch into your current branch first update
243 that non-current branch and then merge::
245 $ git fetch origin v1:v1 # Update v1
246 $ git pull --rebase origin master # Update the current branch master
247 # using rebase instead of merge
250 If you have not yet pushed commits on ``v1``, though, the scenario has
251 to become a bit more complex. Git refuses to update
252 non-fast-forwardable branch, and you don't want to do force-pull
253 because that would remove your non-pushed commits and you would need
254 to recover. So you want to rebase ``v1`` but you cannot rebase
255 non-current branch. Hence, checkout ``v1`` and rebase it before
259 $ git pull --rebase origin v1
260 $ git checkout master
261 $ git pull --rebase origin master
264 It is possible to configure git to make it fetch/pull a few branches
265 or all branches at once, so you can simply run
277 Default remote repository for fetching/pulling is ``origin``. Default
278 set of references to fetch is calculated using matching algorithm: git
279 fetches all branches having the same name on both ends.
285 Pushing is a bit simpler. There is only one command ``push``. When you
290 $ git push origin v1 master
292 git pushes local v1 to remote v1 and local master to remote master.
295 $ git push origin v1:v1 master:master
297 Git pushes commits to the remote repo and updates remote-tracking
298 branches. Git refuses to push commits that aren't fast-forwardable.
299 You can force-push anyway, but please remember - you can force-push to
300 your own repositories but don't force-push to public or shared repos.
301 If you find git refuses to push commits that aren't fast-forwardable,
302 better fetch and merge commits from the remote repo (or rebase your
303 commits on top of the fetched commits), then push. Only force-push if
304 you know what you do and why you do it. See the section `Commit
305 editing and caveats`_ below.
307 It is possible to configure git to make it push a few branches or all
308 branches at once, so you can simply run
320 Default remote repository for pushing is ``origin``. Default set of
321 references to push in git before 2.0 is calculated using matching
322 algorithm: git pushes all branches having the same name on both ends.
323 Default set of references to push in git 2.0+ is calculated using
324 simple algorithm: git pushes the current branch back to its
327 To configure git before 2.0 to the new behaviour run::
329 $ git config push.default simple
331 To configure git 2.0+ to the old behaviour run::
333 $ git config push.default matching
335 Git doesn't allow to push a branch if it's the current branch in the
336 remote non-bare repository: git refuses to update remote working
337 directory. You really should push only to bare repositories. For
338 non-bare repositories git prefers pull-based workflow.
340 When you want to deploy code on a remote host and can only use push
341 (because your workstation is behind a firewall and you cannot pull
342 from it) you do that in two steps using two repositories: you push
343 from the workstation to a bare repo on the remote host, ssh to the
344 remote host and pull from the bare repo to a non-bare deployment repo.
346 That changed in git 2.3, but see `the blog post
347 <https://github.com/blog/1957-git-2-3-has-been-released#push-to-deploy>`_
348 for caveats; in 2.4 the push-to-deploy feature was `further improved
349 <https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more#push-to-deploy-improvements>`_.
355 Git automatically fetches tags that point to commits being fetched
356 during fetch/pull. To fetch all tags (and commits they point to) run
357 ``git fetch --tags origin``. To fetch some specific tags fetch them
360 $ git fetch origin tag $TAG1 tag $TAG2...
364 $ git fetch origin tag 1.4.2
365 $ git fetch origin v1:v1 tag 2.1.7
367 Git doesn't automatically pushes tags. That allows you to have private
368 tags. To push tags list them explicitly::
370 $ git push origin tag 1.4.2
371 $ git push origin v1 master tag 2.1.7
373 Or push all tags at once::
375 $ git push --tags origin
377 Don't move tags with ``git tag -f`` or remove tags with ``git tag -d``
378 after they have been published.
384 When cloning/fetching/pulling/pushing git copies only database objects
385 (commits, trees, files and tags) and symbolic references (branches and
386 lightweight tags). Everything else is private to the repository and
387 never cloned, updated or pushed. It's your config, your hooks, your
388 private exclude file.
390 If you want to distribute hooks, copy them to the working tree, add,
391 commit, push and instruct the team to update and install the hooks
395 Commit editing and caveats
396 ==========================
398 A warning not to edit published (pushed) commits also appears in
399 documentation but it's repeated here anyway as it's very important.
401 It is possible to recover from a forced push but it's PITA for the
402 entire team. Please avoid it.
404 To see what commits have not been published yet compare the head of the
405 branch with its upstream remote-tracking branch::
407 $ git log origin/master.. # from origin/master to HEAD (of master)
408 $ git log origin/v1..v1 # from origin/v1 to the head of v1
410 For every branch that has an upstream remote-tracking branch git
411 maintains an alias @{upstream} (short version @{u}), so the commands
412 above can be given as::
417 To see the status of all branches::
421 To compare the status of local branches with a remote repo::
423 $ git remote show origin
425 Read `how to recover from upstream rebase
426 <https://git-scm.com/docs/git-rebase#_recovering_from_upstream_rebase>`_.
427 It is in ``git help rebase``.
429 On the other hand, don't be too afraid about commit editing. You can
430 safely edit, reorder, remove, combine and split commits that haven't
431 been pushed yet. You can even push commits to your own (backup) repo,
432 edit them later and force-push edited commits to replace what have
433 already been pushed. Not a problem until commits are in a public
434 or shared repository.
440 Whatever you do, don't panic. Almost anything in git can be undone.
443 git checkout: restore file's content
444 ------------------------------------
446 ``git checkout``, for example, can be used to restore the content of
447 file(s) to that one of a commit. Like this::
449 git checkout HEAD~ README
451 The commands restores the contents of README file to the last but one
452 commit in the current branch. By default the commit ID is simply HEAD;
453 i.e. ``git checkout README`` restores README to the latest commit.
455 (Do not use ``git checkout`` to view a content of a file in a commit,
456 use ``git cat-file -p``; e.g. ``git cat-file -p HEAD~:path/to/README``).
459 git reset: remove (non-pushed) commits
460 --------------------------------------
462 ``git reset`` moves the head of the current branch. The head can be
463 moved to point to any commit but it's often used to remove a commit or
464 a few (preferably, non-pushed ones) from the top of the branch - that
465 is, to move the branch backward in order to undo a few (non-pushed)
468 ``git reset`` has three modes of operation - soft, hard and mixed.
469 Default is mixed. ProGit `explains
470 <https://git-scm.com/book/en/Git-Tools-Reset-Demystified>`_ the
471 difference very clearly. Bare repositories don't have indices or
472 working trees so in a bare repo only soft reset is possible.
478 Mixed mode reset with a path or paths can be used to unstage changes -
479 that is, to remove from index changes added with ``git add`` for
480 committing. See `The Book
481 <https://git-scm.com/book/en/Git-Basics-Undoing-Things>`_ for details
482 about unstaging and other undo tricks.
485 git reflog: reference log
486 -------------------------
488 Removing commits with ``git reset`` or moving the head of a branch
489 sounds dangerous and it is. But there is a way to undo: another
490 reset back to the original commit. Git doesn't remove commits
491 immediately; unreferenced commits (in git terminology they are called
492 "dangling commits") stay in the database for some time (default is two
493 weeks) so you can reset back to it or create a new branch pointing to
496 For every move of a branch's head - with ``git commit``, ``git
497 checkout``, ``git fetch``, ``git pull``, ``git rebase``, ``git reset``
498 and so on - git stores a reference log (reflog for short). For every
499 move git stores where the head was. Command ``git reflog`` can be used
500 to view (and manipulate) the log.
502 In addition to the moves of the head of every branch git stores the
503 moves of the HEAD - a symbolic reference that (usually) names the
504 current branch. HEAD is changed with ``git checkout $BRANCH``.
506 By default ``git reflog`` shows the moves of the HEAD, i.e. the
507 command is equivalent to ``git reflog HEAD``. To show the moves of the
508 head of a branch use the command ``git reflog $BRANCH``.
510 So to undo a ``git reset`` lookup the original commit in ``git
511 reflog``, verify it with ``git show`` or ``git log`` and run ``git
512 reset $COMMIT_ID``. Git stores the move of the branch's head in
513 reflog, so you can undo that undo later again.
515 In a more complex situation you'd want to move some commits along with
516 resetting the head of the branch. Cherry-pick them to the new branch.
517 For example, if you want to reset the branch ``master`` back to the
518 original commit but preserve two commits created in the current branch
521 $ git branch save-master # create a new branch saving master
522 $ git reflog # find the original place of master
523 $ git reset $COMMIT_ID
524 $ git cherry-pick save-master~ save-master
525 $ git branch -D save-master # remove temporary branch
528 git revert: revert a commit
529 ---------------------------
531 ``git revert`` reverts a commit or commits, that is, it creates a new
532 commit or commits that revert(s) the effects of the given commits.
533 It's the only way to undo published commits (``git commit --amend``,
534 ``git rebase`` and ``git reset`` change the branch in
535 non-fast-forwardable ways so they should only be used for non-pushed
538 There is a problem with reverting a merge commit. ``git revert`` can
539 undo the code created by the merge commit but it cannot undo the fact
540 of merge. See the discussion `How to revert a faulty merge
541 <https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html>`_.
544 One thing that cannot be undone
545 -------------------------------
547 Whatever you undo, there is one thing that cannot be undone -
548 overwritten uncommitted changes. Uncommitted changes don't belong to
549 git so git cannot help preserving them.
551 Most of the time git warns you when you're going to execute a command
552 that overwrites uncommitted changes. Git doesn't allow you to switch
553 branches with ``git checkout``. It stops you when you're going to
554 rebase with non-clean working tree. It refuses to pull new commits
555 over non-committed files.
557 But there are commands that do exactly that - overwrite files in the
558 working tree. Commands like ``git checkout $PATHs`` or ``git reset
559 --hard`` silently overwrite files including your uncommitted changes.
561 With that in mind you can understand the stance "commit early, commit
562 often". Commit as often as possible. Commit on every save in your
563 editor or IDE. You can edit your commits before pushing - edit commit
564 messages, change commits, reorder, combine, split, remove. But save
565 your changes in git database, either commit changes or at least stash
566 them with ``git stash``.
572 Internet is full of heated discussions on the topic: "merge or
573 rebase?" Most of them are meaningless. When a DVCS is being used in a
574 big team with a big and complex project with many branches there is
575 simply no way to avoid merges. So the question's diminished to
576 "whether to use rebase, and if yes - when to use rebase?" Considering
577 that it is very much recommended not to rebase published commits the
578 question's diminished even further: "whether to use rebase on
581 That small question is for the team to decide. To preserve the beauty
582 of linear history it's recommended to use rebase when pulling, i.e. do
583 ``git pull --rebase`` or even configure automatic setup of rebase for
586 $ git config branch.autosetuprebase always
588 and configure rebase for existing branches::
590 $ git config branch.$NAME.rebase true
594 $ git config branch.v1.rebase true
595 $ git config branch.master.rebase true
597 After that ``git pull origin master`` becomes equivalent to ``git pull
598 --rebase origin master``.
600 It is recommended to create new commits in a separate feature or topic
601 branch while using rebase to update the mainline branch. When the
602 topic branch is ready merge it into mainline. To avoid a tedious task
603 of resolving large number of conflicts at once you can merge the topic
604 branch to the mainline from time to time and switch back to the topic
605 branch to continue working on it. The entire workflow would be
608 $ git checkout -b issue-42 # create a new issue branch and switch to it
609 ...edit/test/commit...
610 $ git checkout master
611 $ git pull --rebase origin master # update master from the upstream
613 $ git branch -d issue-42 # delete the topic branch
614 $ git push origin master
616 When the topic branch is deleted only the label is removed, commits
617 are stayed in the database, they are now merged into master::
619 o--o--o--o--o--M--< master - the mainline branch
621 --*--*--* - the topic branch, now unnamed
623 The topic branch is deleted to avoid cluttering branch namespace with
624 small topic branches. Information on what issue was fixed or what
625 feature was implemented should be in the commit messages.
627 But even that small amount of rebasing could be too big in case of
628 long-lived merged branches. Imagine you're doing work in both ``v1``
629 and ``master`` branches, regularly merging ``v1`` into ``master``.
630 After some time you will have a lot of merge and non-merge commits in
631 ``master``. Then you want to push your finished work to a shared
632 repository and find someone has pushed a few commits to ``v1``. Now
633 you have a choice of two equally bad alternatives: either you fetch
634 and rebase ``v1`` and then have to recreate all you work in ``master``
635 (reset ``master`` to the origin, merge ``v1`` and cherry-pick all
636 non-merge commits from the old master); or merge the new ``v1`` and
637 loose the beauty of linear history.
643 Git has a builtin merge strategy for what Python core developers call
646 $ git merge -s ours v1 # null-merge v1 into master
652 Git doesn't assume any particular development model regarding
653 branching and merging. Some projects prefer to graduate patches from
654 the oldest branch to the newest, some prefer to cherry-pick commits
655 backwards, some use squashing (combining a number of commits into
656 one). Anything is possible.
658 There are a few examples to start with. `git help workflows
659 <https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html>`_
660 describes how the very git authors develop git.
662 ProGit book has a few chapters devoted to branch management in
663 different projects: `Git Branching - Branching Workflows
664 <https://git-scm.com/book/en/Git-Branching-Branching-Workflows>`_ and
665 `Distributed Git - Contributing to a Project
666 <https://git-scm.com/book/en/Distributed-Git-Contributing-to-a-Project>`_.
668 There is also a well-known article `A successful Git branching model
669 <http://nvie.com/posts/a-successful-git-branching-model/>`_ by Vincent
670 Driessen. It recommends a set of very detailed rules on creating and
671 managing mainline, topic and bugfix branches. To support the model the
672 author implemented `git flow <https://github.com/nvie/gitflow>`_
676 Advanced configuration
677 ======================
682 Git has builtin mechanisms to handle line endings between platforms
683 with different end-of-line styles. To allow git to do CRLF conversion
684 assign ``text`` attribute to files using `.gitattributes
685 <https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html>`_.
686 For files that have to have specific line endings assign ``eol``
687 attribute. For binary files the attribute is, naturally, ``binary``.
697 To check what attributes git uses for files use ``git check-attr``
698 command. For example::
700 $ git check-attr -a -- \*.py
706 `GitAlias <http://gitalias.com/>`_ (`repository
707 <https://github.com/GitAlias/gitalias>`_) is a big collection of
708 aliases. A careful selection of aliases for frequently used commands
709 could save you a lot of keystrokes!
711 `GitIgnore <https://www.gitignore.io/>`_ and
712 https://github.com/github/gitignore are collections of ``.gitignore``
713 files for all kinds of IDEs and programming languages. Python
716 `pre-commit <http://pre-commit.com/>`_ (`repositories
717 <https://github.com/pre-commit>`_) is a framework for managing and
718 maintaining multi-language pre-commit hooks. The framework is written
719 in Python and has a lot of plugins for many programming languages.
728 Staging area aka index aka cache is a distinguishing feature of git.
729 Staging area is where git collects patches before committing them.
730 Separation between collecting patches and commit phases provides a
731 very useful feature of git: you can review collected patches before
732 commit and even edit them - remove some hunks, add new hunks and
735 To add files to the index use ``git add``. Collecting patches before
736 committing means you need to do that for every change, not only to add
737 new (untracked) files. To simplify committing in case you just want to
738 commit everything without reviewing run ``git commit --all`` (or just
739 ``-a``) - the command adds every changed tracked file to the index and
740 then commit. To commit a file or files regardless of patches collected
741 in the index run ``git commit [--only|-o] -- $FILE...``.
743 To add hunks of patches to the index use ``git add --patch`` (or just
744 ``-p``). To remove collected files from the index use ``git reset HEAD
745 -- $FILE...`` To add/inspect/remove collected hunks use ``git add
746 --interactive`` (``-i``).
748 To see the diff between the index and the last commit (i.e., collected
749 patches) use ``git diff --cached``. To see the diff between the
750 working tree and the index (i.e., uncollected patches) use just ``git
751 diff``. To see the diff between the working tree and the last commit
752 (i.e., both collected and uncollected patches) run ``git diff HEAD``.
755 <https://git.wiki.kernel.org/index.php/WhatIsTheIndex>`_ and
756 `IndexCommandQuickref
757 <https://git.wiki.kernel.org/index.php/IndexCommandQuickref>`_ in Git
767 Git doesn't allow recursive aliases. The expansion of an alias is
768 interpreted by git as a builtin command. I.e., in the following example
769 alias ``com`` doesn't work because there is no builtin command ``git
776 You can trick git by using shell. The following example works albeit a
787 Git interprets aliases literally. I.e., when expanding an alias git just
788 does simple textual substitution. That could be a surprise if an alias
789 is passed parameters on the command line. For example, the following
790 alias works without parameters -- it pushes configured branch(es) to all
794 push-to-all-remotes = !git remote | xargs -n1 git push
796 But it doesn't work if a user wants to provide a list of branches to
797 push: the command ``git push-to-all-remotes master`` is expanded by git
798 as ``!git remote | xargs -n1 git push master`` which is certainly not
799 what the user wants -- remote's name must comes first, before branches.
803 push-to-all-remotes = !git remote | xargs -I% -n1 git push %
805 Then the command ``git push-to-all-remotes master`` is expanded by git
806 as ``!git remote | xargs -I% -n1 git push % master``; xargs substitutes
807 ``%`` with remote's name.
813 Git switches to the root (top-level directory of the project where
814 ``.git`` subdirectory exists) before running any command. Git
815 remembers though the directory that was current before the switch.
816 Some programs take into account the current directory. E.g., ``git
817 status`` shows file paths of changed and unknown files relative to the
818 current directory; ``git grep`` searches below the current directory;
819 ``git apply`` applies only those hunks from the patch that touch files
820 below the current directory.
822 But most commands run from the root and ignore the current directory.
823 Imagine, for example, that you have two work trees, one for the branch
824 ``v1`` and the other for ``master``. If you want to merge ``v1`` from
825 a subdirectory inside the second work tree you must write commands as
826 if you're in the top-level dir. Let take two work trees,
827 ``project-v1`` and ``project``, for example::
829 $ cd project/subdirectory
830 $ git fetch ../project-v1 v1:v1
833 Please note the path in ``git fetch ../project-v1 v1:v1`` is
834 ``../project-v1`` and not ``../../project-v1`` despite the fact that
835 we run the commands from a subdirectory, not from the root.
841 Rerere is a mechanism that helps to resolve repeated merge conflicts.
842 The most frequent source of recurring merge conflicts are topic
843 branches that are merged into mainline and then the merge commits are
844 removed; that's often performed to test the topic branches and train
845 rerere; merge commits are removed to have clean linear history and
846 finish the topic branch with only one last merge commit.
848 Rerere works by remembering the states of tree before and after a
849 successful commit. That way rerere can automatically resolve conflicts
850 if they appear in the same files.
852 Rerere can be used manually with ``git rerere`` command but most often
853 it's used automatically. Enable rerere with these commands in a
856 $ git config rerere.enabled true
857 $ git config rerere.autoupdate true
859 You don't need to turn rerere on globally - you don't want rerere in
860 bare repositories or single-branch repositories; you only need rerere
861 in repos where you often perform merges and resolve merge conflicts.
863 See `Rerere <https://git-scm.com/book/en/Git-Tools-Rerere>`_ in The
870 Git object database and other files/directories under ``.git`` require
871 periodic maintenance and cleanup. For example, commit editing left
872 unreferenced objects (dangling objects, in git terminology) and these
873 objects should be pruned to avoid collecting cruft in the DB. The
874 command ``git gc`` is used for maintenance. Git automatically runs
875 ``git gc --auto`` as a part of some commands to do quick maintenance.
876 Users are recommended to run ``git gc --aggressive`` from time to
877 time; ``git help gc`` recommends to run it every few hundred
878 changesets; for more intensive projects it should be something like
879 once a week and less frequently (biweekly or monthly) for lesser
882 ``git gc --aggressive`` not only removes dangling objects, it also
883 repacks object database into indexed and better optimized pack(s); it
884 also packs symbolic references (branches and tags). Another way to do
885 it is to run ``git repack``.
887 There is a well-known `message
888 <https://gcc.gnu.org/ml/gcc/2007-12/msg00165.html>`_ from Linus
889 Torvalds regarding "stupidity" of ``git gc --aggressive``. The message
890 can safely be ignored now. It is old and outdated, ``git gc
891 --aggressive`` became much better since that time.
893 For those who still prefer ``git repack`` over ``git gc --aggressive``
894 the recommended parameters are ``git repack -a -d -f --depth=20
895 --window=250``. See `this detailed experiment
896 <http://vcscompare.blogspot.ru/2008/06/git-repack-parameters.html>`_
897 for explanation of the effects of these parameters.
899 From time to time run ``git fsck [--strict]`` to verify integrity of
900 the database. ``git fsck`` may produce a list of dangling objects;
901 that's not an error, just a reminder to perform regular maintenance.
907 Command-line options and arguments
908 ----------------------------------
911 <https://www.kernel.org/pub/software/scm/git/docs/gitcli.html>`_
912 recommends not to combine short options/flags. Most of the times
913 combining works: ``git commit -av`` works perfectly, but there are
914 situations when it doesn't. E.g., ``git log -p -5`` cannot be combined
917 Some options have arguments, some even have default arguments. In that
918 case the argument for such option must be spelled in a sticky way:
919 ``-Oarg``, never ``-O arg`` because for an option that has a default
920 argument the latter means "use default value for option ``-O`` and
921 pass ``arg`` further to the option parser". For example, ``git grep``
922 has an option ``-O`` that passes a list of names of the found files to
923 a program; default program for ``-O`` is a pager (usually ``less``),
924 but you can use your editor::
926 $ git grep -Ovim # but not -O vim
928 BTW, if git is instructed to use ``less`` as the pager (i.e., if pager
929 is not configured in git at all it uses ``less`` by default, or if it
930 gets ``less`` from GIT_PAGER or PAGER environment variables, or if it
931 was configured with ``git config [--global] core.pager less``, or
932 ``less`` is used in the command ``git grep -Oless``) ``git grep``
933 passes ``+/$pattern`` option to ``less`` which is quite convenient.
934 Unfortunately, ``git grep`` doesn't pass the pattern if the pager is
935 not exactly ``less``, even if it's ``less`` with parameters (something
936 like ``git config [--global] core.pager less -FRSXgimq``); fortunately,
937 ``git grep -Oless`` always passes the pattern.
943 It's a bit hard to type ``git rebase --interactive --preserve-merges
944 HEAD~5`` manually even for those who are happy to use command-line,
945 and this is where shell completion is of great help. Bash/zsh come
946 with programmable completion, often automatically installed and
947 enabled, so if you have bash/zsh and git installed, chances are you
948 are already done - just go and use it at the command-line.
950 If you don't have necessary bits installed, install and enable
951 bash_completion package. If you want to upgrade your git completion to
952 the latest and greatest download necessary file from `git contrib
953 <https://git.kernel.org/cgit/git/git.git/tree/contrib/completion>`_.
955 Git-for-windows comes with git-bash for which bash completion is
956 installed and enabled.
962 For command-line lovers shell prompt can carry a lot of useful
963 information. To include git information in the prompt use
965 <https://git.kernel.org/cgit/git/git.git/tree/contrib/completion/git-prompt.sh>`_.
966 Read the detailed instructions in the file.
968 Search the Net for "git prompt" to find other prompt variants.
971 SSH connection sharing
972 ----------------------
974 SSH connection sharing is a feature of OpenSSH and perhaps derivatives
975 like PuTTY. SSH connection sharing is a way to decrease ssh client
976 startup time by establishing one connection and reusing it for all
977 subsequent clients connecting to the same server. SSH connection
978 sharing can be used to speedup a lot of short ssh sessions like scp,
979 sftp, rsync and of course git over ssh. If you regularly
980 fetch/pull/push from/to remote repositories accessible over ssh then
981 using ssh connection sharing is recommended.
983 To turn on ssh connection sharing add something like this to your
988 ControlPath ~/.ssh/mux-%r@%h:%p
991 See `OpenSSH wikibook
992 <https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing>`_ and
993 `search <https://www.google.com/search?q=ssh+connection+sharing>`_ for
996 SSH connection sharing can be used at GitHub, GitLab and SourceForge
997 repositories, but please be advised that BitBucket doesn't allow it
998 and forcibly closes master connection after a short inactivity period
999 so you will see errors like this from ssh: "Connection to bitbucket.org
1000 closed by remote host."
1006 The simplest way to publish a repository or a group of repositories is
1007 ``git daemon``. The daemon provides anonymous access, by default it is
1008 read-only. The repositories are accessible by git protocol (git://
1009 URLs). Write access can be enabled but the protocol lacks any
1010 authentication means, so it should be enabled only within a trusted
1011 LAN. See ``git help daemon`` for details.
1013 Git over ssh provides authentication and repo-level authorisation as
1014 repositories can be made user- or group-writeable (see parameter
1015 ``core.sharedRepository`` in ``git help config``). If that's too
1016 permissive or too restrictive for some project's needs there is a
1017 wrapper `gitolite <http://gitolite.com/gitolite/index.html>`_ that can
1018 be configured to allow access with great granularity; gitolite is
1019 written in Perl and has a lot of documentation.
1021 Web interface to browse repositories can be created using `gitweb
1022 <https://git.kernel.org/cgit/git/git.git/tree/gitweb>`_ or `cgit
1023 <http://git.zx2c4.com/cgit/about/>`_. Both are CGI scripts (written in
1024 Perl and C). In addition to web interface both provide read-only dumb
1025 http access for git (http(s):// URLs). `Klaus
1026 <https://pypi.org/project/klaus/>`_ is a small and simple WSGI web
1027 server that implements both web interface and git smart HTTP
1028 transport; supports Python 2 and Python 3, performs syntax
1031 There are also more advanced web-based development environments that
1032 include ability to manage users, groups and projects; private,
1033 group-accessible and public repositories; they often include issue
1034 trackers, wiki pages, pull requests and other tools for development
1035 and communication. Among these environments are `Kallithea
1036 <https://kallithea-scm.org/>`_ and `pagure <https://pagure.io/>`_,
1037 both are written in Python; pagure was written by Fedora developers
1038 and is being used to develop some Fedora projects. `GitPrep
1039 <http://gitprep.yukikimoto.com/>`_ is yet another GitHub clone,
1040 written in Perl. `Gogs <https://gogs.io/>`_ is written in Go. `GitBucket
1041 <https://gitbucket.github.io/gitbucket-news/about/>`_ is written in
1042 Scala. `RocketGit <https://rocketgit.com/>`_ is AGPL-licensed
1043 development environment written in PHP (there are plans to rewrite
1044 critical parts in C).
1046 And last but not least, `GitLab <https://about.gitlab.com/>`_. It's
1047 perhaps the most advanced web-based development environment for git.
1048 Written in Ruby, community edition is free and open source (MIT
1052 From Mercurial to git
1053 =====================
1055 There are many tools to convert Mercurial repositories to git. The
1056 most famous are, probably, `hg-git <https://hg-git.github.io/>`_ and
1057 `fast-export <http://repo.or.cz/w/fast-export.git>`_ (many years ago
1058 it was known under the name ``hg2git``).
1060 But a better tool, perhaps the best, is `git-remote-hg
1061 <https://github.com/felipec/git-remote-hg>`_. It provides transparent
1062 bidirectional (pull and push) access to Mercurial repositories from
1063 git. Its author wrote a `comparison of alternatives
1064 <https://github.com/felipec/git/wiki/Comparison-of-git-remote-hg-alternatives>`_
1065 that seems to be mostly objective.
1067 To use git-remote-hg, install or clone it, add to your PATH (or copy
1068 script ``git-remote-hg`` to a directory that's already in PATH) and
1069 prepend ``hg::`` to Mercurial URLs. For example::
1071 $ git clone https://github.com/felipec/git-remote-hg.git
1072 $ PATH=$PATH:"`pwd`"/git-remote-hg
1073 $ git clone hg::https://hg.python.org/peps/ PEPs
1075 To work with the repository just use regular git commands including
1076 ``git fetch/pull/push``.
1078 To start converting your Mercurial habits to git see the page
1079 `Mercurial for Git users
1080 <https://www.mercurial-scm.org/wiki/GitConcepts>`_ at Mercurial wiki.
1081 At the second half of the page there is a table that lists
1082 corresponding Mercurial and git commands. Should work perfectly in
1085 Python Developer's Guide also has a chapter `Mercurial for git
1086 developers <https://docs.python.org/devguide/gitdevs.html>`_ that
1087 documents a few differences between git and hg.
1093 `gitsome <https://github.com/donnemartin/gitsome>`_ - Git/GitHub
1094 command line interface (CLI). Written in Python, work on MacOS, Unix,
1095 Windows. Git/GitHub CLI with autocomplete, includes many GitHub
1096 integrated commands that work with all shells, builtin xonsh with
1097 Python REPL to run Python commands alongside shell commands, command
1098 history, customizable highlighting, thoroughly documented.