Sunday, 26 February 2017

Sync Remote Deleted Branches

I finished a feature on Friday and deleted the feature_xxx branch on bitbucket (remote) and my office computer (local). But I want to continue working during the weekend at home with my home computer. I find that the feature_xxx branch is still shown on my home computer, remotely and locally, which means if I do: git branch -a, I get:

      develop
    * feature_xxx
      master
      remotes/origin/develop
      remotes/origin/feature_xxx
      remotes/origin/master

Of course, if I login my bitbucket with a webpage, I could see there are only develop and master left.
How could I keep up to date?

I tried git pull, things seem to be updated with a bunch of green +++++s and red ------s but still showing the feature_xxx branch. 

The reason is: the branches tracked by remotes/origin/* are only a cache of the remote server, and the deleted branch cannot be updated by git fetch.

///////////////////////////////////////////////////////////////////////////////////////////

Solution:

step 0:
git remote show origin

Got:
* remote origin
  Fetch URL: https://boris@bitbucket.org/xxx/yyy.git
  Push  URL: https://boris@bitbucket.org/xxx/yyy.git
  HEAD branch: master
  Remote branches:
    develop                                          tracked
    master                                           tracked
    refs/remotes/origin/feature_xxx stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    develop                      merges with remote develop
    feature_xxx merges with remote feature_xxx
    master                       merges with remote master
  Local refs configured for 'git push':
    develop pushes to develop (up to date)
    master  pushes to master  (up to date)

step 1:
git remote prune origin 

Got:
Pruning origin
URL: https://boris@bitbucket.org/xxx/yyy.git
 * [pruned] origin/feature_xxx

step 2:
Now that the deleted remote branch has been synchronized, we can delete the local branch which doesn't exist anymore.
git branch -d feature_xxx

Note that
(1) you might need to checkout to another branch before deleting it (eg develop).
git checkout develop

(2) you might also get:
       error: The branch 'feature_xxx' is not fully merged.
       If you are sure you want to delete it, run 'git branch -D feature_xxx'.
This is because the local didn't have the updated data yet. Just do:
git pull

Finally, check it out: git branch -a
Got:
    * develop
      master
      remotes/origin/develop
      remotes/origin/master


ref:
https://higoge.github.io/2015/07/07/git-remote05/

No comments:

Post a Comment