简体   繁体   中英

Git: How can I reconcile a locally rebased feature branch with its remote equivalent?

I've branched off of master and done work on feature-branch . Pushed feature-branch up the remote. Other people's work gets merged into master . I pull the changes to master , then rebase my local version of feature-branch off the updated master .

Next time I try to push feature-branch to the remote I can't because it's out of sync with the remote version, due to the rebase. My solution when working alone has been to force push, but is there a better solution? A coworker of mine, rather than force pushing, did a pull and then a push, but the results look pretty weird (other people's commits appear when doing a pull-request to master).

Don't ever rebase a branch you have pushed.

You should never alter the history after it became public (which it became by pushing). Rebase alters the history (among others, eg amend).

You might think: "I can just force it, right?" No: Everyone who pulled feature-branch will then need to force the next pull, too. Even worse: If there are commits based on the original feature-branch they will need to be changed, too. This will quickly become a nightmare.

The only solution at this point is to revert your rebase and merge master into feature-branch .

Generally I would suggest to never rebase and avoid fast-forward merges. There might come a time when you will be happy to have a meaningful history, even if it looks ugly. There is nothing gained by a linear history if it doesn't represent the actual history.

Rebasing something you already pushed is only really practical when it's so few developers that you can count them on one hand, and you can get in touch with them and have them all fix their repoes.

If I wanted to publish some work-in-progress but still keep open the option to rebase it later, I'd do one or both of:

  • push it to some repo that is not the "blessed" one and people know it's just for scratching around
  • clearly mark it as "use this at your peril, may be rebased later" via some sort of naming convention.

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