|
1 | | -Change commits for a clean log |
2 | | -============================== |
| 1 | +Rewriting history |
| 2 | +================= |
3 | 3 |
|
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: |
7 | 14 |
|
8 | 15 | #. We have two commits in our ``my-feature`` branch: one for the actual |
9 | 16 | function, the other for the associated tests: |
@@ -64,8 +71,8 @@ present the following scenario: |
64 | 71 | 132ae9b Add new feature |
65 | 72 |
|
66 | 73 | #. 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. |
69 | 76 |
|
70 | 77 | …with ``git commit --fixup`` and ``git rebase --autosquash`` |
71 | 78 | ------------------------------------------------------------ |
@@ -118,3 +125,29 @@ In Git, however, there is an even easier way to correct a previous commit: with |
118 | 125 |
|
119 | 126 | Further options can be found in the `Git commit documentation |
120 | 127 | <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