Skip to content

Commit 2203523

Browse files
committed
📝 Update git section
* Add git history and config-based hooks * Add links to Sourcegraph to find pre-commit hooks
1 parent 2e576c3 commit 2203523

3 files changed

Lines changed: 71 additions & 10 deletions

File tree

docs/productive/git/advanced/hooks/index.rst

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ in a Git repository, including:
2525
| | `push-to-checkout`_ |
2626
+---------------+-------------------------------------------------------+
2727

28-
They can be located either in local or server-side repositories. This allows Git
29-
repositories to be customised and user-defined actions to be triggered.
28+
They can be located in either local or server-side repositories, allowing you to
29+
customise Git repositories and trigger user-defined actions.
3030

3131
Git hooks are located in the :file:`.git/hooks/` directory. When a repository is
3232
created, some sample scripts are already created there:
@@ -57,11 +57,35 @@ The integrated scripts are shell and Perl scripts, but any scripting language
5757
can be used. The Shebang line (:samp:`#!/bin/sh`) determines how the file is to
5858
be interpreted.
5959

60-
However, the scripts cannot be copied into the server-side repository.
60+
However, the scripts are not copied to the Git server using `git push`. To be
61+
able to use scripts across multiple repositories, the :doc:`pre-commit` is
62+
therefore recommended.
6163

6264
.. seealso::
6365
* `Hooks <https://git-scm.com/docs/githooks#_hooks>`_
6466

67+
Configuration-based hooks
68+
-------------------------
69+
70+
.. version-added:: 2.54
71+
72+
Git 2.54 now introduces a new way to define hooks in your configuration
73+
files: instead of placing a script in :file:`.git/hooks/pre-commit`, you can
74+
now specify the following:
75+
76+
.. code-block:: ini
77+
78+
[hook "ruff check"]
79+
event = pre-commit
80+
command = ~/bin/ruff check --fix --exit-non-zero-on-fix
81+
82+
However, this configuration can be specified not only for each project, but
83+
also globally or system-wide in :file:`~/.gitconfig` or in
84+
:file:`/etc/gitconfig`.
85+
86+
Use ``git hook list pre-commit`` to find out which hooks are configured and
87+
where they come from.
88+
6589
.. toctree::
6690
:hidden:
6791

docs/productive/git/advanced/hooks/scripts.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ Miscellaneous
162162

163163
.. seealso::
164164
* `Supported hooks <https://pre-commit.com/hooks.html>`_
165+
* `Sourcegraph: file:^\.pre-commit-config\.yaml$
166+
<https://sourcegraph.com/search?q=context:global+file:%5E.pre-commit-hooks.yaml>`_
167+
* `Sourcegraph: file:^\.pre-commit-hooks\.yaml$ \"types: [python]\"
168+
<https://sourcegraph.com/search?q=context:global+file:%5E.pre-commit-hooks.yaml%24+%22types:+%5Bpython%5D%22>`_

docs/productive/git/workflows/clean-history.rst

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
Change commits for a clean log
2-
==============================
1+
Rewriting history
2+
=================
33

4-
With ``git commit --fixup`` and ``git rebase --autosquash`` you can correct a
5-
series of commits relatively easily. To demonstrate this with an example, I
6-
present the following scenario:
4+
There are several commands in Git for rewriting history. ``git rebase -i`` is
5+
the best known and most flexible: you can reorder, merge, edit and remove
6+
commits. However, this flexibility comes with a degree of complexity: your
7+
`working tree <https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-workingtree>`_ and `index <https://git-scm.com/docs/gitglossary#def_index>`_
8+
are updated, and conflicts may arise that need to be resolved before you can
9+
continue working.
10+
11+
With ``git commit --fixup`` and ``git rebase --autosquash``, on the other hand,
12+
you can correct a series of commits relatively easily. Let’s demonstrate this
13+
with an example below:
714

815
#. We have two commits in our ``my-feature`` branch: one for the actual
916
function, the other for the associated tests:
@@ -64,8 +71,8 @@ present the following scenario:
6471
132ae9b Add new feature
6572
6673
#. The changes can now be pushed to our remote branch using ``git push
67-
--force-with-lease``. ``--force-with-lease`` option ensures that any existing
68-
changes on the remote branch are not overwritten.
74+
--force-with-lease``. The ``--force-with-lease`` option ensures that any
75+
existing changes on the remote branch are not overwritten.
6976

7077
…with ``git commit --fixup`` and ``git rebase --autosquash``
7178
------------------------------------------------------------
@@ -118,3 +125,29 @@ In Git, however, there is an even easier way to correct a previous commit: with
118125

119126
Further options can be found in the `Git commit documentation
120127
<https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---fixupamendrewordltcommitgt>`_.
128+
129+
``git history``
130+
---------------
131+
132+
.. version-added:: 2.54
133+
134+
Git 2.54 introduces ``git history`` on an experimental basis, meaning that
135+
the interface is still subject to further development. ``git history`` makes
136+
it easier to correct typos in previous commit messages and to split commits
137+
into two parts:
138+
139+
:samp:`git history reword {SHA}`
140+
opens your editor with the message of the specified commit and rewrites
141+
it directly, updating all branches descended from that commit. Unlike
142+
:doc:`../rebase`, it does not access your working tree or your index.
143+
:samp:`git history split {SHA}`
144+
interactively splits a commit into two parts, allowing you to select
145+
which parts should be moved into a new parent commit. The interface is
146+
the same as that of ``git add --p``. After selecting the blocks, Git
147+
creates a new commit with these changes as a predecessor to the original
148+
commit, which retains all unselected blocks, and rewrites all downstream
149+
branches so that they point to the updated history.
150+
151+
.. warning::
152+
``history`` does not support histories containing merge commits, nor can
153+
it perform operations that would result in a merge conflict.

0 commit comments

Comments
 (0)