Refresh local branches (automatically remove local branches that were deleted on remote)

lucasncolombo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 15, 2018

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

4 answers

1 accepted

13 votes
Answer accepted
Mikael Sandberg
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2018

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.

Johannes Sebastian
Contributor
September 18, 2018

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?

Like # people like this
Mikael Sandberg
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 18, 2018

@Johannes Sebastian 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.

Like # people like this
Johannes Sebastian
Contributor
September 18, 2018

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 :)

Like # people like this
Michael Dinder
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 5, 2022

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.

Like # people like this
Steve Letch
Contributor
April 6, 2022

Thanks, this is what I was looking for :)

5 votes
Radek Janata
Contributor
October 22, 2021

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:

Nachum Shmilovitz
Contributor
October 27, 2021

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

they are not there when looking on the web

gitweb.jpg

close and open source tree 

Like atj393 likes this
Radek Janata
Contributor
October 31, 2021

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

1 vote
Shelton Han
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 19, 2021

As @Mikael Sandberg 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 :) 

0 votes
nawazibrahim
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 8, 2021
git branch | grep -v develop | xargs git branch -d

This Works 

Radek Janata
Contributor
October 21, 2021

Don't ever run this command, you might delete ALL your local branches!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events