|
1 | 1 | ## Interactive Rebasing ##
|
2 | 2 |
|
| 3 | +You can also rebase interactively. This is often used to re-write your |
| 4 | +own commit objects before pusing them somewhere. It is an easy way to |
| 5 | +split, merge or re-order commits before sharing them with others. You |
| 6 | +can also use it to clean up commits you've pulled from someone when |
| 7 | +applying them locally. |
| 8 | + |
| 9 | +If you have a number of commits that you would like to somehow modify |
| 10 | +during the rebase, you can invoke interactive mode by passing a '-i' or |
| 11 | +'--interactive' to the 'git rebase' command. |
| 12 | + |
| 13 | + $ git rebase -i origin/master |
| 14 | + |
| 15 | +This will invoke interactive rebase mode on all the commits you have made |
| 16 | +since the last time you have pushed (or merged from the origin repository). |
| 17 | + |
| 18 | +To see what commits those are beforehand, you can run log this way: |
| 19 | + |
| 20 | + $ git log github/master.. |
| 21 | + |
| 22 | +Once you run the 'rebase -i' command, you will be thrown into your editor |
| 23 | +of choice with something that looks like this: |
| 24 | + |
| 25 | + pick fc62e55 added file_size |
| 26 | + pick 9824bf4 fixed little thing |
| 27 | + pick 21d80a5 added number to log |
| 28 | + pick 76b9da6 added the apply command |
| 29 | + pick c264051 Revert "added file_size" - not implemented correctly |
| 30 | + |
| 31 | + # Rebase f408319..b04dc3d onto f408319 |
| 32 | + # |
| 33 | + # Commands: |
| 34 | + # p, pick = use commit |
| 35 | + # e, edit = use commit, but stop for amending |
| 36 | + # s, squash = use commit, but meld into previous commit |
| 37 | + # |
| 38 | + # If you remove a line here THAT COMMIT WILL BE LOST. |
| 39 | + # However, if you remove everything, the rebase will be aborted. |
| 40 | + # |
| 41 | + |
| 42 | +This means that there are 5 commits since you last pushed and it gives you |
| 43 | +one line per commit with the following format: |
| 44 | + |
| 45 | + (action) (partial-sha) (short commit message) |
| 46 | + |
| 47 | +Now, you can change the action (which is by default 'pick') to either 'edit' |
| 48 | +or 'squash', or just leave it as 'pick'. You can also reorder the commits |
| 49 | +just by moving the lines around however you want. Then, when you exit the |
| 50 | +editor, git will try to apply the commits however they are now arranged and |
| 51 | +do the action specified. |
| 52 | + |
| 53 | +If 'pick' is specified, it will simply try to apply the patch and save the |
| 54 | +commit with the same message as before. |
| 55 | + |
| 56 | +If 'squash' is specified, it will combine that commit with the previous one |
| 57 | +to create a new commit. This will drop you into your editor again to merge |
| 58 | +the commit messages of the two commits it is now squashing together. So, |
| 59 | +if you exit the editor with this: |
| 60 | + |
| 61 | + pick fc62e55 added file_size |
| 62 | + squash 9824bf4 fixed little thing |
| 63 | + squash 21d80a5 added number to log |
| 64 | + squash 76b9da6 added the apply command |
| 65 | + squash c264051 Revert "added file_size" - not implemented correctly |
| 66 | + |
| 67 | +Then you will have to create a single commit message from this: |
| 68 | + |
| 69 | + # This is a combination of 5 commits. |
| 70 | + # The first commit's message is: |
| 71 | + added file_size |
| 72 | + |
| 73 | + # This is the 2nd commit message: |
| 74 | + |
| 75 | + fixed little thing |
| 76 | + |
| 77 | + # This is the 3rd commit message: |
| 78 | + |
| 79 | + added number to log |
| 80 | + |
| 81 | + # This is the 4th commit message: |
| 82 | + |
| 83 | + added the apply command |
| 84 | + |
| 85 | + # This is the 5th commit message: |
| 86 | + |
| 87 | + Revert "added file_size" - not implemented correctly |
| 88 | + |
| 89 | + This reverts commit fc62e5543b195f18391886b9f663d5a7eca38e84. |
| 90 | + |
| 91 | +Once you have edited that down into once commit message and exit the editor, |
| 92 | +the commit will be saved with your new message. |
| 93 | + |
| 94 | +If 'edit' is specified, it will do the same thing, but then pause before |
| 95 | +moving on to the next one and drop you into the command line so you can |
| 96 | +amend the commit, or change the commit contents somehow. |
| 97 | + |
| 98 | +If you wanted to split a commit, for instance, you would specify 'edit' for |
| 99 | +that commit: |
| 100 | + |
| 101 | + pick fc62e55 added file_size |
| 102 | + pick 9824bf4 fixed little thing |
| 103 | + edit 21d80a5 added number to log |
| 104 | + pick 76b9da6 added the apply command |
| 105 | + pick c264051 Revert "added file_size" - not implemented correctly |
| 106 | + |
| 107 | +And then when you get to the command line, you revert that commit and create |
| 108 | +two (or more) new ones. Lets say 21d80a5 modified two files, file1 and file2, |
| 109 | +and you wanted to split them into seperate commits. You could do this after |
| 110 | +the rebase dropped you to the command line : |
| 111 | + |
| 112 | + $ git reset HEAD^ |
| 113 | + $ git add file1 |
| 114 | + $ git commit 'first part of split commit' |
| 115 | + $ git add file2 |
| 116 | + $ git commit 'second part of split commit' |
| 117 | + $ git rebase --continue |
| 118 | + |
| 119 | +And now instead of 5 commits, you would have 6. |
| 120 | + |
| 121 | +The last useful thing that interactive rebase can do is drop commits for you. |
| 122 | +If instead of choosing 'pick', 'squash' or 'edit' for the commit line, you |
| 123 | +simply remove the line, it will remove the commit from the history. |
0 commit comments