]> git.phdru.name Git - git-wiki.git/blobdiff - pep-git.txt
Add an example for ``git check-attr``
[git-wiki.git] / pep-git.txt
index af9b34d12402e3479204ee7089b25cc1171fcb3b..af8fc37292c30a62537df299c7cc746a9a5012a2 100644 (file)
@@ -62,7 +62,7 @@ many different languages. Download Russian translation from `GArik
 Offline documentation
 ---------------------
 
-Git has builtin help: run ``git help TOPIC``. For example, run
+Git has builtin help: run ``git help $TOPIC``. For example, run
 ``git help git`` or ``git help help``.
 
 
@@ -156,7 +156,7 @@ Remote-tracking branches are branches (pointers to commits) in your
 local repository. They are there for you to remember what branches and
 commits have been pulled from and pushed to what remote repos (you can
 pull from and push to many remotes). Remote-tracking branches live
-under ``remotes/REMOTE`` namespaces, e.g. ``remotes/origin/v2``.
+under ``remotes/$REMOTE`` namespaces, e.g. ``remotes/origin/v2``.
 
 To see the status of remote-tracking branches run::
 
@@ -190,39 +190,39 @@ There is a major difference between
 
 ::
 
-    $ git fetch REMOTE BRANCH
+    $ git fetch $REMOTE $BRANCH
 
 and
 
 ::
 
-    $ git fetch REMOTE BRANCH:BRANCH
+    $ git fetch $REMOTE $BRANCH:$BRANCH
 
-The first command fetches commits from the named BRANCH in the REMOTE
-repository that are not in your repository and leaves the id (the
-hash) of the head commit in file .git/FETCH_HEAD and
-updates remote-tracking branch.
+The first command fetches commits from the named $BRANCH in the
+$REMOTE repository that are not in your repository, updates
+remote-tracking branch and leaves the id (the hash) of the head commit
+in file .git/FETCH_HEAD.
 
-The second command fetches commits from the named BRANCH in the REMOTE
-repository that are not in your repository and updates both the local
-branch BRANCH and its upstream remote-tracking branch. But it refuses
-to update branches in case of non-fast-forward. And it refuses to
-update the current branch.
+The second command fetches commits from the named $BRANCH in the
+$REMOTE repository that are not in your repository and updates both
+the local branch $BRANCH and its upstream remote-tracking branch. But
+it refuses to update branches in case of non-fast-forward. And it
+refuses to update the current branch.
 
 The first command is used internally by ``git pull``.
 
 ::
 
-    $ git pull REMOTE BRANCH
+    $ git pull $REMOTE $BRANCH
 
 is equivalent to
 
 ::
 
-    $ git fetch REMOTE BRANCH
-    $ git merge FETCH_HEAD  # FETCH_HEAD is a literal here
+    $ git fetch $REMOTE $BRANCH
+    $ git merge FETCH_HEAD
 
-Certainly, BRANCH in that case should be your current branch. If you
+Certainly, $BRANCH in that case should be your current branch. If you
 want to merge a different branch into your current branch first update
 that non-current branch and then merge::
 
@@ -326,6 +326,11 @@ from it) you do that in two steps using two repositories: you push
 from the workstation to a bare repo on the remote host, ssh to the
 remote host and pull from the bare repo to a non-bare deployment repo.
 
+That changed in git 2.3, but see `the blog post
+<https://github.com/blog/1957-git-2-3-has-been-released#push-to-deploy>`_
+for caveats; in 2.4 the push-to-deploy feature was `further improved
+<https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more#push-to-deploy-improvements>`_.
+
 Tags
 ''''
 
@@ -334,11 +339,12 @@ during fetch/pull. To fetch all tags (and commits they point to) run
 ``git fetch --tags origin``. To fetch some specific tags fetch them
 explicitly::
 
-    $ git fetch origin tag TAG1 tag TAG2...
+    $ git fetch origin tag $TAG1 tag $TAG2...
 
 For example::
 
-    $ git fetch origin tag 1.4.2 tag 2.1.7
+    $ git fetch origin tag 1.4.2
+    $ git fetch origin v1:v1 tag 2.1.7
 
 Git doesn't automatically pushes tags. That allows you to have private
 tags (lightweight tags are also private for a repo, they cannot be
@@ -386,9 +392,9 @@ Read `how to recover from upstream rebase
 It is in ``git help rebase``.
 
 On the other hand don't be too afraid about commit editing. You can
-safely edit, remove, reorder, combine and split commits that hasn't
+safely edit, remove, reorder, combine and split commits that haven't
 been pushed yet. You can even push commits to your own (backup) repo,
-edit them later and force-push edited commits to replace what has
+edit them later and force-push edited commits to replace what have
 already been pushed. Not a problem until commits are in a public
 or shared repository.
 
@@ -397,18 +403,25 @@ Undo
 ====
 
 Whatever you do, don't panic. Almost anything in git can be undone.
+
+git checkout: restore file's content
+------------------------------------
+
 ``git checkout``, for example, can be used to restore the content of
 file(s) to that one of a commit. Like this::
 
     git checkout HEAD~ README
 
 The commands restores the contents of README file to the last but one
-commit in the current branch. By default a commit ID is simply HEAD;
+commit in the current branch. By default the commit ID is simply HEAD;
 i.e. ``git checkout README`` restores README to the latest commit.
 
 (Do not use ``git checkout`` to view a content of a file in a commit,
 use ``git cat-file -p``; e.g. ``git cat-file -p HEAD~:path/to/README``).
 
+git reset: remove (non-pushed) commits
+--------------------------------------
+
 ``git reset`` moves the head of the current branch. The head can be
 moved to point to any commit but it's often used to remove a commit or
 a few (preferably, non-pushed ones) from the top of the branch - that
@@ -421,16 +434,94 @@ Default is mixed. ProGit `explains
 difference very clearly. Bare repositories don't have indices or
 working trees so in a bare repo only soft reset is possible.
 
+Unstaging
+'''''''''
+
 Mixed mode reset with a path or paths can be used to unstage changes -
-that is, to remove changes added with ``git add`` for committing. See
-`The Book <https://git-scm.com/book/en/Git-Basics-Undoing-Things>`_
-for details about unstaging and other undo tricks.
+that is, to remove from index changes added with ``git add`` for
+committing. See `The Book
+<https://git-scm.com/book/en/Git-Basics-Undoing-Things>`_ for details
+about unstaging and other undo tricks.
 
-TODO: describe undo strategies: git reflog, git revert.
-"Commit early, commit often".
+git reflog: reference log
+-------------------------
 
-How to undo a merge
-https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html
+Removing commits with ``git reset`` or moving the head of a branch
+sounds dangerous and it is. But there is a way to undo: another
+reset back to the original commit. Git doesn't remove commits
+immediately; unreferenced commits (in git terminology they are called
+"dangling commits") stay in the database for some time (default is two
+weeks) so you can reset back to it or create a new branch pointing to
+the original commit.
+
+For every move of a branch's head - with ``git commit``, ``git
+checkout``, ``git fetch``, ``git pull``, ``git rebase``, ``git reset``
+and so on - git stores a reference log (reflog for short). For every
+move git stores where the head was. Command ``git reflog`` can be used
+to view (and manipulate) the log.
+
+In addition to the moves of the head of every branch git stores the
+moves of the HEAD - a symbolic reference that (usually) names the
+current branch. HEAD is changed with ``git checkout $BRANCH``.
+
+By default ``git reflog`` shows the moves of the HEAD, i.e. the
+command is equivalent to ``git reflog HEAD``. To show the moves of the
+head of a branch use the command ``git reflog $BRANCH``.
+
+So to undo a ``git reset`` lookup the original commit in ``git
+reflog``, verify it with ``git show`` or ``git log`` and run ``git
+reset $COMMIT_ID``. Git stores the move of the branch's head in
+reflog, so you can undo that undo later again.
+
+In a more complex situation you'd want to move some commits along with
+resetting the head of the branch. Cherry-pick them to the new branch.
+For example, if you want to reset the branch ``v2`` back to the
+original commit but preserve two commits created in the current branch
+do something like::
+
+    $ git branch save-v2 # create a new branch saving v2
+    $ git reflog # find the original place of v2
+    $ git reset $COMMIT_ID
+    $ git cherry-pick save-v2~ save-v2
+    $ git branch -D save-v2 # remove temporary branch
+
+git revert: revert a commit
+---------------------------
+
+``git revert`` reverts a commit or commits, that is, it creates a new
+commit or commits that revert(s) the effects of the given commits.
+It's the only way to undo published commits (``git commit --amend``,
+``git rebase`` and ``git reset`` change the branch in
+non-fast-forwardable ways so they should only be used for non-pushed
+commits.)
+
+There is a problem with reverting a merge commit. ``git revert`` can
+undo the code created by the merge commit but it cannot undo the fact
+of merge. See the discussion `How to revert a faulty merge
+<https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html>`_.
+
+One thing that cannot be undone
+-------------------------------
+
+Whatever you undo, there is one thing that cannot be undone -
+overwritten uncommitted changes. Uncommitted changes don't belong to
+git so git cannot help preserving them.
+
+Most of the time git warns you when you're going to execute a command
+that overwrites uncommitted changes. Git warns you when you try to
+switch branches with ``git checkout``. It warns you when you're going
+to rebase with non-clean working tree. It refuses to pull new commits
+over non-committed files.
+
+But there are commands that do exactly that - overwrite files in the
+working tree. Commands like ``git checkout $PATHs`` or ``git reset
+--hard`` silently overwrite files including your uncommitted changes.
+
+With that in mind you can understand the stance "commit early, commit
+often". Commit as often as possible. Commit on every save in your
+editor or IDE. You can edit your commits before pushing - change,
+reorder, combine, remove. But save your changes in git database,
+either commit changes or at least stash them with ``git stash``.
 
 
 Merge or rebase?
@@ -454,7 +545,7 @@ branch::
 
 and configure rebase for existing branches::
 
-    $ git config branch.NAME.rebase true
+    $ git config branch.$NAME.rebase true
 
 For example::
 
@@ -501,10 +592,31 @@ Git has a builtin merge strategy for what Python core developers call
     $ git merge -s ours v1  # null-merge v1 into v2
 
 
-ReReRe
-======
+Advanced configuration
+======================
 
-https://git-scm.com/book/en/Git-Tools-Rerere
+Line endings
+------------
+
+Git has builtin mechanisms to handle line endings between platforms
+with different EOL styles. To allow git to do CRLF conversion assign
+``text`` attribute to files using `.gitattributes
+<https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html>`_.
+For files that have to have specific line ending assign ``eol``
+attribute. For binary files the attribute is, naturally, ``binary``.
+
+For example::
+
+    $ cat .gitattributes
+    *.py text
+    *.txt text
+    *.png binary
+    /readme.txt eol=CRLF
+
+To check what attributes git uses for files use ``git check-attr``
+command. For example::
+
+$ git check-attr -a -- \*.py
 
 
 Advanced topics
@@ -521,15 +633,10 @@ Staging area aka index is a distinguishing feature of git. See
 Wiki.
 
 
-Advanced configuration
-======================
-
-Line endings
-------------
-
-Git has builtin mechanisms to handle line endings.
+ReReRe
+======
 
-TODO: describe crlf configuration and .gitattributes.
+https://git-scm.com/book/en/Git-Tools-Rerere
 
 
 Database maintenance