Refresh local branches (automatically remove local branches that were deleted on remote)
Asked by lucasncolombo:

Hi!

I've deleted a branch in my remote repository, but sourcetree keeps showing it in the list of local branches. Is there any way Sourcetree automatically remove the local branch if it was already deleted in the remote repository?

Thanks


Answers:
author: Mikael Sandberg
body: You need to do a Fetch and check the box to prune tracking branches, that will remove branches that you have removed on the remote.

comments: author: Johannes Sebastian
body: This will only remove Branches under "Remote" and not my "Local" Checked out (now remotely deleted) Branches, yes?  

If so: How I can "Prune" my Local branches?


author: Mikael Sandberg
body: [@Johannes Sebastian](/t5/user/viewprofilepage/user-id/1192118) Correct, the fetch will only affect remote branches. To delete local branches you have to right-click on it and select Delete... Note that you can only delete branches that are not checked out.


author: Johannes Sebastian
body: Thank you for a quick response.

That is the answer I was expecting. Remote actions will not affect my local branches.

I am sure there is are good arguments for this....

However, that is what I need.

It is a small feature and I hope SourceTree will offer it in the future :)


author: Michael Dinder
body: Might I add, because I just spent about 15 minutes trying to figure out why I don't see a checkbox about pruning. If you are right clicking with the mouse in the left panel under remotes on the origin itself to perform the fetch operation, the popup for additional checkboxes does not appear. If you click the fetch button at the top menu of the app, then you do see the popup of additional checkboxes. So not a bug, just a useability thing.


author: Steve Letch
body: Thanks, this is what I was looking for :)


author: Radek Janata
body: This cannot be done automatically from within SourceTree UI but you can run the following command on git cmd:

```
git branch -vv | grep ': gone]' | grep -v "\*" | awk '{ print $1; }' | xargs -r git branch -D
```

You can create your own alias too:

```
git config --global alias.delete-gone-branches "! git branch -vv | grep ': gone]' | grep -v \"*\" | awk '{ print \$1; }' | xargs -r git branch -D"
```

And then run it `git delete-gone-branches`.

**Sources:**

* <https://medium.com/@kcmueller/delete-local-git-branches-that-were-deleted-on-remote-repository-b596b71b530c>
* <https://www.erikschierboom.com/2020/02/17/cleaning-up-local-git-branches-deleted-on-a-remote/>

comments: author: Nachum Shmilovitz
body: Mac, bitbucket  

open my project and open terminal  

run

```
git branch -vv | grep ': gone]' | grep -v "\*" | awk '{ print $1; }' | xargs -r git branch -D
```

the branches are still on remote when using source tree  

![gitsourcetree.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/166919i45C2C36102671C9F/image-size/large?v=v2&px=999 "gitsourcetree.jpg")  

they are not there when looking on the web  

![gitweb.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/166920i7CD450BE881A448D/image-size/large?v=v2&px=999 "gitweb.jpg")

close and open source tree


author: Radek Janata
body: My answer deletes LOCAL branches that are no longer present on remote.  
You're referring to REMOTE branches in your local SourceTree - that's different.

To delete these remote branches from your SourceTree you have to fetch remote repo with "Prune tracking branches no longer present on remote(s)".

![SourceTree.png](https://community.atlassian.com/t5/image/serverpage/image-id/167305i46A20CA1AF08F83B/image-size/large?v=v2&px=999 "SourceTree.png")


author: Shelton Han
body: As [@Mikael Sandberg](/t5/user/viewprofilepage/user-id/853579) mentioned, you can use "Fetch" and tick "Prune tracking branches no longer present on remotes" to sync remote branches.

For local branches, even though there is no automatic way, there is a way quicker than clicking through all unwanted branches.

Just click "Branch"

then select "Delete Branches"

then tick all unwanted local branches

and select delete branches

Voila, done.

Please note you can also delete remote branches here, even force delete them -- just be careful :)


author: nawazibrahim
body:     git branch | grep -v develop | xargs git branch -d

This Works

comments: author: Radek Janata
body: Don't ever run this command, you might delete ALL your local branches!


Tags: developer