Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete local Git branches if their remote-tracking references don't exist anymore [duplicate]

I have a local and a remote Git repository.

  • In my local repository I create and switch to a new branch: git checkout -b feature/niceNewFeature

  • I do my coding, git add ., git commit and git push my feature/niceNewFeature branch to the remote repository (e.g. GitHub).

  • After this I create a GitHub Pull Request to merge feature/niceNewFeature branch into master - after the code is reviewed, I will do so.

  • Since feature/niceNewFeature is now merged to master, I will delete it on GitHub.

  • However, in my local repository, feature/niceNewFeature branch is still listed if I execute git branch command.

How do I remove feature/niceNewFeature branch from my local repository, since it is already merged to master and deleted from the remote repository?

like image 297
martoncsukas Avatar asked Jun 10 '17 20:06

martoncsukas


People also ask

How do I delete local branches no longer on my remote?

Remove All Local Branches not on Remote First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.

Will deleting local branch affect remote?

Local branches are branches on your local machine and do not affect any remote branches. git branch is the command to delete a branch locally. -d is a flag, an option to the command, and it's an alias for --delete .

How do you clean up up local branches and references to non existing remote branches?

In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name. In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.

How do I delete old local branches?

Remove All Local Branches not on Remote First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.


1 Answers

Short answer:

git fetch --prune

git branch -lvv | cut -c3- | awk '/: gone]/ {print $1}' | xargs git branch -d

Detailed answer:

Firstly, remove any remote-tracking references that no longer exist on the remote:

git fetch --prune (or git fetch -p)

If you do this, and then you run git branch -vv (verbose), you will see that although feature/niceNewFeature branch is still listed, the upstream branch is now "gone":

feature/niceNewFeature  832f7a7 [origin/feature/niceNewFeature: gone] Some commit message.

Secondly, let's remove the local branches, with the following command:

git branch -lvv | cut -c3- | awk '/: gone]/ {print $1}' | xargs git branch -d

What it does, is basically the following:

  1. Lists all local (-l) branches in verbose mode (vv) (that's almost like our previous output)
  2. It cuts the first and second characters from the beginning of every line (which are spaces)
  3. With awk it filters the "gone" ones (/: gone]/), and gets their first column ({print $1}). Remember, the "gone" means that the remote-tracking reference doesn't exist anymore.
  4. The output (which is the name of the branch to be deleted, feature/niceNewFeature in our case) is piped to git branch -d (delete) with xargs command, so it will delete the branch. Note that the branch must be fully merged in its upstream branch, because only -d option is used, not -D (which is --delete --force).
like image 176
martoncsukas Avatar answered Oct 19 '22 20:10

martoncsukas