Home > Blockchain >  After a git branch rename, what's the best way to notify everyone and update their local machin
After a git branch rename, what's the best way to notify everyone and update their local machin

Time:08-04

I had a repo with the branches feature and master.

On my machine, I renamed feature with the following steps:

  1. Checked out my local branch git checkout feature

  2. Renamed the branch to beta with git branch -m beta

  3. Pushed the beta branch and reset the upstream git push origin -u beta

  4. Deleted the feature remote branch git push origin --delete feature

Now my local machine and GitHub are all synced up, but someone else working on their remote machine still has the feature branch. When they run git branch they see master and feature listed. When they run git pull, they're getting the message "Your configuration specifies to merge with the ref 'refs/heads/feature' from the remote, but no such ref was fetched."

Few questions:

  1. How do I resolve this?
  2. What's the best way to tell other people that a branch has been renamed and they should update it on their local machines as well?
  3. Luckily the person I'm working on this project with had no work in progress/staged changes on feature, but if he had, where would those have gone, considering his machine is unaware of the branch rename, and would have attempted to push it to remote on the old name?

CodePudding user response:

I hope your team was in sync about this branch renaming. Anyway.... It shouldn't be too difficult for them to correct the situation.

First, they should fetch with --prune so that the remote feature branch goes away from what they see about the remote:

git fetch --prune origin # or adjust to the remote that each one likes to name it

That won't delete their local feature branch, if they have one (I for one do not create local branches for shared branches). If they want to delete the local one, they need to run git branch -D feature.

Then, they need to set up all the branches that are using origin/feature to use origin/beta as their upstream. This can be easily done with

git branch --set-upstream-to=origin/beta some-local-branch

On each local branch that was using feature. This might be simpler to do by using a script that checks their upstream of all local branches. Perhaps something like:

git branch | for each branch; do
    upstream=$(git rev-parse --abbrev-ref "$branch"@{u})
    if [ "$upstream" == "origin/upstream" ]; then
        git branch --set-upstream-to=origin/beta $branch
    fi
done

That should be enough so that when the pull, they do not get the error about the missing branch.

About point 2: Any means necessary.... it's not like it is the end of the world if you are not in sync, but if you do not tell people about it, then seeing a new feature branch show up in the central repo should not come as a surprise... nor getting flooded with questions if you are the person in charge of the repo. So, plan ahead, let people know about it (usual means: mails, meetings, you name it).

About point 3, they go nowhere.... they remain where they are.. perhaps a little explanation about how branches work in git will help understand what is happening. A branch in git is just a pointer to a revision. You can have multiple branches pointing to the same revision... and those pointers can be moved around at will. So.... when you decided to rename feature to beta all that was created is a pointer to the same revision called beta, then you deleted feature... but that does not mess up with the underlying revisions of the history of the branches.

So.... if that rename had taken place when you were in the middle of work, that would not make a difference. You setup the new upstream for your branch (in case you had it set up origin/feature) and it will keep on working without issues.

  • Related