git overall commit record (rabase) and then merge (merge)

Original link: https://blog.frytea.com/archives/783/

75

⚠ Whether it is rebase or merge is a dangerous operation, please ensure that you fully understand the meaning of the command, and ensure that each line of code is correct before proceeding.
If you are not sure, please remember to make a data backup! ! !

In order to merge the dev branch into the master branch, while ensuring that the code is not lost and the commit record is regular, you can use the interactive git rebase command. This allows you to rearrange, amend, merge or split commits. Here are the steps:

  1. Make sure you are currently on the dev branch:
 git checkout dev
  1. Interactively rebase to the master branch:
 git rebase -i master

This will open an editor listing all commits in the dev branch relative to master. Each line in the editor represents a commit in the format: <action> <commit-hash> <commit-message> .

  1. Adjustment commit record:
    To adjust the commit order, simply move the lines around in the editor.
    To merge multiple commits, you can change the action of the corresponding line from pick to squash or fixup . squash will merge the commit into the previous commit and allow you to edit the commit message. fixup will merge the commit into the previous commit, but discard the current commit’s message.
    To amend a commit, change the action from pick to edit .
    When you’re done making adjustments, save and close the editor. Git will do what you specify.
  2. If you modify a commit, Git pauses the rebase process, allowing you to edit that commit. In this case, make the necessary changes, then execute the following command to continue the rebase:
 git add <changed-files> git commit --amend git rebase --continue
  1. When the rebase is complete, switch back to the master branch:
 git checkout master
  1. Merge the dev branch into the master branch:
 git merge dev
  1. Push the changes to the remote repository:
 git push

Through this process, you can ensure that when you merge, the commit record will be more organized, and no code changes will be lost. Note that this approach changes the commit history, so be careful when doing this and make sure other team members know what you’re doing.

This article is transferred from: https://blog.frytea.com/archives/783/
This site is only for collection, and the copyright belongs to the original author.