There are two cases:

  • Rebase of the same branch (when a local commit needs to be pushed to the remote, but the commit of the branch is behind that of the remote branch) :
  1. You can use Git pull first and git rebase later, or even more simply, git pull –rebase, to keep branches and remote merges in a straight line.

  2. If a conflict occurs, modify the conflict first, run git add, and then run git rebase -continue

  • Merge branches that conflict but want to remain in a straight line:
  1. Go to the branch that you want to merge and run git rebase XXX. XXX indicates that you want to merge the old master branch. Because there is a conflict, modify the conflict first, then execute git add, and then git rebase — continue. The branch is now merged with the main branch and in a straight line.

  2. Switch to the master branch, retrace the history to the commit before the collision, and then perform git merge on the branch, where the master branch has merged the branches perfectly and in a straight line.

Note: If the nonlinear commit you merged into the remote side is already being used by someone else and a new commit is added to it, it is not recommended to change the branch, remember!