Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

6 votes
Answer accepted
Mikael Sandberg Community Leader Jan 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.

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 Sep 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

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
1 vote
Radek Janata Community Leader Oct 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:

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 

Radek Janata Community Leader Oct 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

git branch | grep -v develop | xargs git branch -d

This Works 

Radek Janata Community Leader Oct 21, 2021

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

0 votes

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Sourcetree

Tip from the team: configure your repos for hosting goodness!

Supported Platforms macOS Windows We recently introduced support for additional hosting services such as GitHub Enterprise, GitLab (Cloud, Community Edition, Enterprise Edition), and...

7,033 views 5 13
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you