简体   繁体   中英

Local and remote branch has been diverged?

Here is my situation

  1. I checkout from main and make my feature branch “my branch “
  2. I pushed my changes into remote “my branch”
  3. Main got advanced with few merges from other people
  4. Now I have worked more on my local branch , and want to do following
  5. “ take all the changes of remote main , and put my local branch changes in top of them “
  6. I have tried doing git rebase and pull but doing that just overwrite my local files and They are left with errors due to weird mismatch ( from my local and remote ) . Luckily I have staged and committed so no changes are lost

For this, you cannot do a pull. You did have the right idea though:

git rebase main

Then when locally everything is in order, force push ( git push --force ) to overwrite the remote "my branch", telling it that your state (which includes the commits added from main) is the correct one.

Lets say we have master, branch_A (made from master) and branch_B (also made from master). I am working on branch_A and my friend is working on branch_B. My friends work in branch_B was merged to master. Now I am done and I want to merge my branch to master as well. The process I would follow is below:

  1. git checkout master
  2. git pull (update local master from with remote changes)
  3. git checkout branch_A
  4. git rebase master (this will pull in the changes from master and stack your branch commits on top)
  5. git push origin branch_A -f (need force since you rebased which changes branch HEAD)

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM