Rebase is used for branch merging

1. General situation:

You open a feature branch on the main branch and work on the feature branch

A colleague pushed new content A2 on the main branch

If you want to obtain A2 changes in the feature branch, merge the main branch into the feature branch to generate B1. Git records the merge information.

Sometimes you don’t want Git to record this merge because it makes git’s history cumbersome. And it splits.

Before returning to merge, if the feature branch is rebase to the main branch, it looks like this. It is a straight line with no forks and is quite clean. After rebase, the old commit B is discarded and referred to the newly created commit B ‘.

2. Have the same commit situation:

For example, the first commit B of the feature branch is the same as the A2 of the main branch.

After git rebase main is executed, B is deleted.

3. Operating on multiple branches:

Git rebase — Onto Main Feature feature1

In a real project, you fix a bug on Develop and need to branch the bug to the Release branch:

Git rebase –onto Release Develop bug

(You can also switch to the bug branch and execute Git rebase Release.)

Git rebase git rebase

  1. Cancel each commit in the feature branch.
  2. Temporarily save the above operations as a patch file in the. Git /rebase directory.
  3. Update the feature branch to the latest main branch.
  4. Apply the patch file saved above to the feature branch.

Iii. Conflicts in rebase process:

  1. Git will stop rebase and let you resolve the conflict.
  2. After the conflict is resolved, usegit add <filename>Command to update the content.
  3. Then performgit rebase --continue.
  4. Git will then continue to apply the remaining patch files.
  5. It can be used during this periodGit rebase - abortTo stop the rebase operation. Return to the state before Rebase.

Git rebase is a dangerous command. Because it changed history.

If, prior to rebase, the feature branch was maintained not only by you, but also by other colleagues, then after you rebase the feature and push it to the remote repository, other colleagues will cause problems with pulling code. So when no commit that requires rebase has ever been pushed, rebase can be done safely.

Rebase is used in other ways (merge commit, delete commit, modify commit message, etc.).

  1. Switch to the branch you want to operate on
  2. performgit rebase -i HEAD~2, go to the VI window, and view nearly two commits

3. Press the keyboardaKey to enter edit mode 4. Follow the prompts in the picture to perform operations, such as I wantdropoff70787b8This commit is going to bepicktod.5. Press Esc to exit the VI window. Input::wqSave and exit. 6. View information on the CLISuccessfully rebased and updated refs/heads/feature.instructions70787b8The commit has been deleted.

6. Operations after the success of Rebase

  • After rebase is implemented, it is often necessary to force push:Git push -f branch name(in fact,git push -fJust now,
  • If you want to restore records before Rebase, you can do thisgit reflogLater, press the arrow down to find the commit you want to return to and copy.
  • To performgit reset commit_hash_id --hardYou can go back to some version.

Vii. Reference Documents:

git-scm.com/book/zh/v2

Git-scm.com/docs/git-re…

Cloud.tencent.com/developer/n…

Blog.csdn.net/gtlbtnq9mr3…

www.yiibai.com/git/git_reb…

www.liaoxuefeng.com/wiki/896043…

Jartto. Wang / 2018/12/11 /…