git rebase modifies intermediate commits

0. Preface

Today, when transplanting the latest version of kfence functions, a total of more than 40 patches need to be transplanted. Many of the patches have conflicts and need to be manually modified before they can be merged. When all the patches were merged and compiled, it was discovered that there was an error in the manual merge of one of the patches.

Suppose there are 40 patches in total, numbered 1 to 40. The problem now is that the 20th patch needs to be modified, and there are many manual modifications to the 21 to 40 patches. How can the 20th patch be modified quickly and effectively without reset? What about patch?

This is the git rebase command that needs to be explained in this article .

1. git rebase -i HEAD~n

Let’s take a look at option -i first:

-i
--interactive
Make a list of the commits which are about to be rebased. Let the user edit that list before rebasing. This mode can also be used to split commits (see SPLITTING COMMITS below).

The commit list format can be changed by setting the configuration option rebase.instructionFormat. A customized instruction format will automatically have the long commit hash prepended to the format.

See also INCOMPATIBLE OPTIONS below.

Through this option, a commit list to be rebased is generated, allowing the user to enter interactive mode before rebase.

HEAD~n will list the countdown n commit list starting from HEAD, for example, when n = 4:

When executing  the git rebase -i HEAD~4 command, the last 4 commit lists starting from HEAD will pop up, and the last one is HEAD commit.

When the command of the first commit line, that is, the fourth commit from the bottom, is changed from pick to edit, the code rebase stops at this commit for amend processing.

After saving the modification and exiting the interaction, you will find that the fourth commit from the bottom enters the stop state, waiting for amend.

The rest is simple:

  • Modify code;
  • git add
  • git commit --amend
  • git rebase --continue

2. Commands in interaction

In the interactive window, in addition to the edit command mentioned before, this article also adds other commands:

  • p/pick, keep commit;
  • r/reword, keep the commit, but need to modify the commit message;
  • e/edit, keep commit, but you need to stop and wait for amend;
  • s/squash, retain the commit, but merge the commit into the previous commit;
  • f/fixup , similar to squash, but the commit message will not be retained;
  • x/exec is equivalent to running commands in the shell;
  • d/drop, discard the commit;

Official documentation:

https://git-scm.com/docs/git-rebase/

Guess you like

Origin blog.csdn.net/jingerppp/article/details/133138361