|
| 1 | +Commits für ein sauberes Log ändern |
| 2 | +=================================== |
| 3 | + |
| 4 | +Mit ``git commit --fixup`` und ``git rebase --autosquash`` könnt ihr relativ |
| 5 | +einfach eine Reihe von Commits korrigieren. Um dies an einem Beispiel zu |
| 6 | +demonstrieren, stelle ich euch folgendes Szenario vor: |
| 7 | + |
| 8 | +#. Wir haben in unserem ``my-feature``-Branch zwei Commits: den einen für die |
| 9 | + eigentliche Funktion, den anderen für die zugehörigen 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 | +#. Beim *Merge-* oder *Pull-Request* erhalten wir sowohl zu unserer Funktion wie |
| 18 | + auch zu unseren Tests Feedback, das wir gerne in unsere bestehenden Commits |
| 19 | + integrieren würden. Hierfür erstellen wir nun zunächst temporäre 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 | +… mit ``git rebase`` |
| 32 | +-------------------- |
| 33 | + |
| 34 | +3. Mit ``git rebase -i`` können wir interaktiv die ``pick``-Zeilen neu |
| 35 | + anordnen: |
| 36 | + |
| 37 | + .. code-block:: console |
| 38 | +
|
| 39 | + $ git rebase -i origin/main |
| 40 | +
|
| 41 | + Dies öffnet unseren Editor: |
| 42 | + |
| 43 | + .. code-block:: console |
| 44 | +
|
| 45 | + pick 56e34e9 Add new feature |
| 46 | + pick a4587fa Add test for my new feature |
| 47 | + pick 8780db6 Feedback on the tests from my function |
| 48 | + pick 556c1e8 Feedback on my function |
| 49 | +
|
| 50 | + Anschließend können wir die Zeilen umändern, :abbr:`z.B. (zum Beispiel)` in: |
| 51 | + |
| 52 | + .. code-block:: console |
| 53 | +
|
| 54 | + pick 56e34e9 Add new feature |
| 55 | + squash 556c1e8 Feedback on my function |
| 56 | + pick a4587fa Add test for my new feature |
| 57 | + squash 8780db6 Feedback on the tests from my function |
| 58 | +
|
| 59 | + Nun haben wir erneut zwei Commits: |
| 60 | + |
| 61 | + .. code-block:: console |
| 62 | +
|
| 63 | + $ git log --oneline my-feature ^origin/main |
| 64 | + 31a140a (my-feature) Add test for my new feature |
| 65 | + 132ae9b Add new feature |
| 66 | +
|
| 67 | +#. Die Änderungen können nun mit ``git push -f`` an unseren entfernten Zweig |
| 68 | + gesendet werden. |
| 69 | + |
| 70 | +…mit ``git commit --fixup`` und ``git rebase --autosquash`` |
| 71 | +------------------------------------------------------------ |
| 72 | + |
| 73 | +In Git gibt es jedoch noch einen einfacheren Weg, einen vorherigen Commit zu |
| 74 | +korrigieren: mit ``git commit--fixup`` und ``git rebase --autosquash``. |
| 75 | + |
| 76 | +5. Wir erstellen erneut zwei temporäre Commits, diesmal jedoch mit ``git |
| 77 | + commit--fixup``: |
| 78 | + |
| 79 | + .. code-block:: console |
| 80 | +
|
| 81 | + # Further changes to the tests |
| 82 | + $ git commit --fixup=31a140a |
| 83 | + [my-feature dd0c0d1] fixup! Add test for my new feature |
| 84 | + 1 file changed, 1 insertion(+) |
| 85 | + # Further changes to my function |
| 86 | + $ git commit --fixup=132ae9b |
| 87 | + [my-function bc2298a] fixup! Add new feature |
| 88 | + 1 file changed, 1 insertion(+) |
| 89 | + $ git log --oneline my-feature ^origin/main |
| 90 | + bc2298a (my-feature) fixup! Add new feature |
| 91 | + dd0c0d1 fixup! Add test for my new feature |
| 92 | + 31a140a Add test for my new feature |
| 93 | + 132ae9b Add new feature |
| 94 | +
|
| 95 | + Bei Commits mit der Option :samp:`--fixup={SHA}` schreibt Git eine speziell |
| 96 | + formatierte Commit-Nachricht, die als *dieser Commit korrigiert jenen* |
| 97 | + gelesen werden kann. |
| 98 | + |
| 99 | +#. Anstatt nun mit ``git rebase -i`` die ``Pick``/``Squash``-Zeilen manuell |
| 100 | + festzulegen, können wir nun einfach ``git rebase --autosquash`` ausführen: |
| 101 | + |
| 102 | + .. code-block:: console |
| 103 | +
|
| 104 | + $ git rebase --autosquash origin/main |
| 105 | + Successfully rebased and updated refs/heads/my-feature. |
| 106 | + $ git log --oneline my-feature ^origin/main |
| 107 | + 694cb48 (my-feature) Add test for my new feature |
| 108 | + 55cbe9b Add new feature |
| 109 | +
|
| 110 | + ``git rebase --autosquash`` automatisiert das, was wir gerade manuell mit |
| 111 | + ``git rebase -i`` getan haben – es öffnete sich jedoch kein Editor, in dem |
| 112 | + wir die Commits manuell verschieben mussten. |
| 113 | + |
| 114 | + .. tip:: |
| 115 | + Die Option ``--fixup`` enthält auch die Optionen ``amend`` und ``reword``, |
| 116 | + um die Commit-Nachricht umzuformulieren, :abbr:`z.B. (tum Beispiel)` |
| 117 | + :samp:`git commit --fixup:amend={SHA}`. |
| 118 | + |
| 119 | + Weitere Optionen findet ihr in der `Git Commit-Dokumentation |
| 120 | + <https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---fixupamendrewordltcommitgt>`_. |
0 commit comments