| Start | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Local Branch Rebasing Rebasing (opens new window) reapplies a series of commits on top of another commit. To This would cause: To turn into: These operations can be combined into a single command that checks out the branch and immediately rebases it: Important: After the rebase, the applied commits will have a different hash. You should not rebase commits you have already pushed to a remote host. A consequence may be an inability to |
||||||||||||||||
| Rebase: ours and theirs, local and remote A rebase switches the meaning of "ours" and "theirs": Whatever HEAD's pointing to is "ours" The first thing a rebase does is resetting the With respect to terminologies used by merge tools (not to be confused with local ref or remote ref (opens new window)) That means a merge/diff tool will present the upstream branch as Inversion illustratedOn a merge:We don't change the current branch On a rebase:But on a rebase we switch sides because the first thing a rebase does is to checkout the upstream branch to replay the current commits on top of it! A The rebase will then replay 'their' commits on the new 'our' |
||||||||||||||||
| Interactive Rebase This example aims to describe how one can utilize Interactive rebase is initiated using following command: The Say you want to rearrange your last three commits. To do this you can run: After executing the above instruction, a file will be opened in your text editor where you will be able to select how your commits will be rebased. For the purpose of this example, just change the order of your commits, save the file, and close the editor. This will initiate a rebase with the order you've applied. If you check Rewording commit messagesNow, you've decided that one of the commit messages is vague and you want it to be more descriptive. Let's examine the last three commits using the same command. Instead of rearranging the order the commits will be rebased, this time we will change When you close the editor, the rebase will initiate and it will stop at the specific commit message that you wanted to reword. This will let you change the commit message to whichever you desire. After you've changed the message, simply close the editor to proceed. Changing the content of a commitBesides changing the commit message you can also adapt the changes done by the commit. To do so just change As soon as the staging area contains all changes you want in that commit, commit the changes. The old commit message will be shown and can be adapted to reflect the new commit. Splitting a single commit into multipleSay you've made a commit but decided at a later point this commit could be split into two or more commits instead. Using the same command as before, replace Now, git will stop at the commit you have marked for editing and place all of its content into the staging area. From that point you can run Squashing multiple commits into oneSay you have done some work and have multiple commits which you think could be a single commit instead. For that you can carry out This time replace |
||||||||||||||||
| Rebase down to the initial commit Since Git 1.7.12 (opens new window) it is possible to rebase down to the root commit. The root commit is the first commit ever made in a repository, and normally cannot be edited. Use the following command: |
||||||||||||||||
Rebasing before a code review SummaryThis goal is to reorganize all of your scattered commits into more meaningful commits for easier code reviews. If there are too many layers of changes across too many files at once, it is harder to do a code review. If you can reorganize your chronologically created commits into topical commits, then the code review process is easier (and possibly less bugs slip through the code review process). This overly-simplified example is not the only strategy for using git to do better code reviews. It is the way I do it, and it's something to inspire others to consider how to make code reviews and git history easier/better. This also pedagogically demonstrates the power of rebase in general. This example assumes you know about interactive rebasing. Assuming:
Strategy:
Example:This will be shown in text editor: Change it to this: Then git will apply one commit at a time. After each commit, it will show a prompt, and then you can do the following: Then you will repeat those steps for every commit. In the end, you have this: Now we run rebase one more time to reorder and squash: This will be shown in text editor: Change it to this: NOTICE: make sure that you tell git rebase to apply/squash the smaller topical commits in the order they were chronologically commited. Otherwise you might have false, needless merge conflicts to deal with. When that interactive rebase is all said and done, you get this: RecapYou have now rebased your chronological commits into topical commits. In real life, you may not need to do this every single time, but when you do want or need to do this, now you can. Plus, hopefully you learned more about git rebase. |
||||||||||||||||
| Testing all commits during rebase Before making a pull request, it is useful to make sure that compile is successful and tests are passing for each commit in the branch. We can do that automatically using For example:
will perform the interactive rebase and stop after each commit to execute |
||||||||||||||||
| Configuring autostash Autostash is a very useful configuration option when using rebase for local changes. Oftentimes, you may need to bring in commits from the upstream branch, but are not ready to commit just yet. However, Git does not allow a rebase to start if the working directory is not clean. Autostash to the rescue: The autostash will be applied whenever the rebase is finished. It does not matter whether the rebase finishes successfully, or if it is aborted. Either way, the autostash will be applied. If the rebase was successful, and the base commit therefore changed, then there may be a conflict between the autostash and the new commits. In this case, you will have to resolve the conflicts before committing. This is no different than if you would have manually stashed, and then applied, so there is no downside to doing it automatically. |
||||||||||||||||
| Aborting an Interactive Rebase You have started an interactive rebase. In the editor where you pick your commits, you decide that something is going wrong (for example a commit is missing, or you chose the wrong rebase destination), and you want to abort the rebase. To do this, simply delete all commits and actions (i.e. all lines not starting with the The help text in the editor actually provides this hint: |
||||||||||||||||
| Setup git-pull for automatically perform a rebase instead of a merge If your team is following a rebase-based workflow, it may be a advantageous to setup git so that each newly created branch will perform a rebase operation, instead of a merge operation, during a To setup every new branch to automatically rebase, add the following to your Command line: Alternatively, you can setup the Command line: |
||||||||||||||||
| Pushing after a rebase Sometimes you need rewrite history with a rebase, but This can be solved with a Note: Syntax
Parameters
RemarksPlease keep in mind that rebase effectively rewrites the repository history. Rebasing commits that exists in the remote repository could rewrite repository nodes used by other developers as base node for their developments. Unless you really know what you are doing, it is a best practice to rebase before pushing your changes. |
Read Article
https://develop.horje.com/learn/1434/reference