1

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?
Sidewinder
  • 369
  • 3
  • 13
  • Git only cares about commits. Branch names are just lightweight labels; changing the name of a branch, even a major long lived branch like master, is not disruptive. Even deleting a long lived branch is not a big deal. You're overthinking this. – matt Aug 03 '22 at 14:47
  • Also, the phrase `git pull` is a bad smell; you should retrain everyone to avoid it. – matt Aug 03 '22 at 14:49
  • 1
    Don't delete the `feature` branch. Instead, delete all of the files in it except for a single README that says: "All work is now on the beta branch" or similar. – William Pursell Aug 03 '22 at 14:53
  • @matt to be fair, overthinking is better than underthinking it, where I once used `git reset --hard HEAD` thinking wHAt's tHe WorST tHat CaN hAPpEn -- that was the day I learned about reflog :') until I feel more comfortable with this part of git, better to ask and be safe with more knowledge than less haha. – Sidewinder Aug 03 '22 at 15:34
  • @matt I looked up why `git pull` by itself can be bad, does the accepted answer on this sum up why, or is there anything else I should know about? https://stackoverflow.com/questions/15316601/in-what-cases-could-git-pull-be-harmful Thanks for pointing this out. – Sidewinder Aug 03 '22 at 15:35
  • @WilliamPursell thanks for the suggestion - this is just a small hobby thing where I've got one other person collaborating, but that's a useful way to tell larger groups. – Sidewinder Aug 03 '22 at 15:37
  • Well, I didn't say Git doesn't have really dangerous commands. It most certainly does. – matt Aug 03 '22 at 17:07

1 Answers1

2

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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Your first command worked fine (it said `[deleted] (none) -> origin/feature`) however the second command says `error: option \`set-upstream' takes no value`. What does that mean? – Sidewinder Aug 03 '22 at 14:14
  • Just corrected. It is `--set-upstream-to`. – eftshift0 Aug 03 '22 at 14:20
  • Thanks! So what they ended up doing was changing the upstream of their local feature branch `git branch --set-upstream-to=origin/beta feature` and then renaming `feature` to `beta` so it matches the remote branch name. Then `git pull` got them the latest changes from me. So while we're good for now, I still have a questions, if you don't mind! Is there a "correct" way to do this in the future? Any insight on questions 2 and 3 in my OP? – Sidewinder Aug 03 '22 at 14:33
  • Let me add that to the original answer, – eftshift0 Aug 03 '22 at 14:36
  • Thank you for clarifying @eftshift0, I'll read more into it with the info you've given me. Have an excellent day :) – Sidewinder Aug 03 '22 at 15:39