can't push to branch after rebase

前端 未结 6 656
走了就别回头了
走了就别回头了 2021-01-29 17:28

We use git and have a master branch and developer branches. I need to add a new feature and then rebase the commits to master, then push master to CI server.

The problem

6条回答
  •  没有蜡笔的小新
    2021-01-29 18:28

    The main thing to keep in mind here is what pull and rebase are doing behind the scenes.

    A pull will basically do two things: fetch and merge. When you include --rebase it will do a rebase instead of the merge.

    A rebase is pretty much like stashing all of your local changes since you branched, fast forwarding your branch to the latest commit on the target, and unstashing your changes in order on top.

    (This explains why you may get multiple conflict resolution prompts when doing a rebase vs the one conflict resolution you may get with a merge. You have the opportunity to resolve a conflict on EACH commit that is being rebased in order to otherwise preserve your commits.)

    You never want to push rebased changes to remote branches as this is rewritting history. Ofcoarse, never is a bit strong as there are almost always exceptions. The case that you need to maintain a remote version of your local repository to work on a specific environment for example.

    This will require you to push rebased changes at times either using force:

    git push -f origin newfeature
    

    Or in some cases your administrator may have removed the ability to force so you must delete and recreate:

    git push origin :newfeature
    git push origin newfeature
    

    In either case you must be absolutely sure you know what you are doing if someone else is collaborating with you on your remote branch. This may mean that you work together initially with merges and rebase those into a more manageable commit format just before going to master and removing your working branch.

    Remember you can almost always fallback on git's GC by taking advantage of:

    git reflog
    

    This is a HUGE life saver as you can reset back to a more stable state if you get lost in all of your rebase/conflict management.

提交回复
热议问题