|
| 1 | +Change commits for a clean log |
| 2 | +============================== |
| 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: |
| 7 | + |
| 8 | +#. We have two commits in our ``my-feature`` branch: one for the actual |
| 9 | + function, the other for the associated tests: |
| 10 | + |
| 11 | + .. code-block:: console |
| 12 | +
|
| 13 | + $ git log --oneline my-feature ^origin/main |
| 14 | + a4587fa (my-feature) Add test for my new feature |
| 15 | + 56e34e9 Add new feature |
| 16 | +
|
| 17 | +#. During the *merge* or *pull request*, we receive feedback on both our |
| 18 | + function and our tests, which we would like to integrate into our existing |
| 19 | + commits. To do this, we first create temporary commits: |
| 20 | + |
| 21 | + .. code-block:: console |
| 22 | +
|
| 23 | + $ git commit -m "Feedback on the tests from my function" |
| 24 | + $ git commit -m "Feedback on my function" |
| 25 | + $ git log --oneline my-feature ^origin/main |
| 26 | + 556c1e8 (my-feature) Feedback on my function |
| 27 | + 8780db6 Feedback on the tests from my function |
| 28 | + a4587fa Add test for my new feature |
| 29 | + 56e34e9 Add new feature |
| 30 | +
|
| 31 | +… with ``git rebase`` |
| 32 | +--------------------- |
| 33 | + |
| 34 | +3. With ``git rebase -i`` we can interactively rearrange the ``pick`` lines: |
| 35 | + |
| 36 | + .. code-block:: console |
| 37 | +
|
| 38 | + $ git rebase -i origin/main |
| 39 | +
|
| 40 | + This opens our editor: |
| 41 | + |
| 42 | + .. code-block:: console |
| 43 | +
|
| 44 | + pick 56e34e9 Add new feature |
| 45 | + pick a4587fa Add test for my new feature |
| 46 | + pick 8780db6 Feedback on the tests from my function |
| 47 | + pick 556c1e8 Feedback on my function |
| 48 | +
|
| 49 | + We can then change the lines, for example to: |
| 50 | + |
| 51 | + .. code-block:: console |
| 52 | +
|
| 53 | + pick 56e34e9 Add new feature |
| 54 | + squash 556c1e8 Feedback on my function |
| 55 | + pick a4587fa Add test for my new feature |
| 56 | + squash 8780db6 Feedback on the tests from my function |
| 57 | +
|
| 58 | + Now we have two commits again: |
| 59 | + |
| 60 | + .. code-block:: console |
| 61 | +
|
| 62 | + $ git log --oneline my-feature ^origin/main |
| 63 | + 31a140a (my-feature) Add test for my new feature |
| 64 | + 132ae9b Add new feature |
| 65 | +
|
| 66 | +#. The changes can now be sent to our remote branch with ``git push -f``. |
| 67 | + |
| 68 | +…with ``git commit --fixup`` and ``git rebase --autosquash`` |
| 69 | +------------------------------------------------------------ |
| 70 | + |
| 71 | +In Git, however, there is an even easier way to correct a previous commit: with |
| 72 | +``git commit--fixup`` and ``git rebase --autosquash``. |
| 73 | + |
| 74 | +5. We create two temporary commits again, but this time with ``git |
| 75 | + commit--fixup``: |
| 76 | + |
| 77 | + .. code-block:: console |
| 78 | +
|
| 79 | + # Further changes to the tests |
| 80 | + $ git commit --fixup=31a140a |
| 81 | + [my-feature dd0c0d1] fixup! Add test for my new feature |
| 82 | + 1 file changed, 1 insertion(+) |
| 83 | + # Further changes to my function |
| 84 | + $ git commit --fixup=132ae9b |
| 85 | + [my-function bc2298a] fixup! Add new feature |
| 86 | + 1 file changed, 1 insertion(+) |
| 87 | + $ git log --oneline my-feature ^origin/main |
| 88 | + bc2298a (my-feature) fixup! Add new feature |
| 89 | + dd0c0d1 fixup! Add test for my new feature |
| 90 | + 31a140a Add test for my new feature |
| 91 | + 132ae9b Add new feature |
| 92 | +
|
| 93 | + For commits with the :samp:`--fixup={SHA}` option, Git writes a specially |
| 94 | + formatted commit message that can be read as *this commit corrects that |
| 95 | + commit*. |
| 96 | + |
| 97 | +#. Instead of using ``git rebase -i`` to manually specify the |
| 98 | + ``Pick``/``Squash`` lines, we can now simply run ``git rebase --autosquash``: |
| 99 | + |
| 100 | + .. code-block:: console |
| 101 | +
|
| 102 | + $ git rebase --autosquash origin/main |
| 103 | + Successfully rebased and updated refs/heads/my-feature. |
| 104 | + $ git log --oneline my-feature ^origin/main |
| 105 | + 694cb48 (my-feature) Add test for my new feature |
| 106 | + 55cbe9b Add new feature |
| 107 | +
|
| 108 | + ``git rebase --autosquash`` automates what we have just done manually with |
| 109 | + ``git rebase -i`` – but it does not open an editor in which we have to move |
| 110 | + the commits manually. |
| 111 | + |
| 112 | + .. tip:: |
| 113 | + The ``--fixup`` option also contains the ``amend`` and ``reword`` options |
| 114 | + to reformulate the commit message, for example :samp:`git commit |
| 115 | + --fixup:amend={SHA}`. |
| 116 | + |
| 117 | + Further options can be found in the `Git commit documentation |
| 118 | + <https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---fixupamendrewordltcommitgt>`_. |
0 commit comments