Search code examples
gitgit-branchgit-rebasegit-remoteremote-branch

Is it safe to rebase remote branch if nobody based work on it?


I want push my personal local branch for code review with my peer. My peer doesn't will merge this branch. After code review, I want rebase my personal branch in master branch, and push master branch. Is safe?

The Pro Git book says:

Do not rebase commits that exist outside your repository and that people may have based work on.

I expect this won't be a problem, since nobody based work on this branch. I am correct?


Solution

  • This sentence is a bit confusing, and you need to be very clear about it before you use git rebase:

    After code review, I want rebase my personal branch in master branch, and push master branch.

    The normal wording is "rebase branch A onto branch B". What this really means is "create a new version of branch A, as though you had developed it starting from branch B".

    Branch B is not changed by this action.

    The reason I'm going over this is to make clear what the warning means:

    Do not rebase commits that exist outside your repository and that people may have based work on.

    The commits you are rebasing are branch A - the branch that is going to change as result of the rebase.

    So there are two very different scenarios:

    • "I want to rebase some-personal-branch onto master, and then push some-personal-branch to GitHub/BitBucket/wherever" - this is probably OK: nobody will have based work on some-personal-branch
    • "I want to rebase master onto some-personal-branch, and then push master to GitHub/BitBucket/wherever" - this is almost certainly not OK: your colleague might have based work on master, and things will get confusing

    A smiple rule of thumb I like to use is "who owns this branch?"

    • A branch that I own is one for a task that I'm working on alone; I should be safe to do what I like with it - rebase, amend commits, etc
    • If this is a branch that someone else owns, I should not do anything to it other than look at it - I should not even base any work on it
    • If this is a shared branch that people will base work on, nobody owns it, and commands like git rebase, amending commits, etc, should only be used in rare "emergency" situations