Git Commands to Remove Merged or Nonexistent Local Branches

The following git commands will delete all local branches that have been removed from remote or merged into the main branch.

Over time, your local git branches list can become overwhelming, particularly if you develop on a single branch, generate a pull request, merge it into the main branch, and then remove the remote git branch once it has been merged. After the branch is removed from the remote repository, there is no need to keep it on your local machine.

The following git command will delete all local branches that have been merged into the main branch. If your git trunk branch is not named main or you wish to remove all branches that have been merged into a branch other than main, simply modify the two instances of the word main in the command to reflect the name of your branch.

List of git commands to remove local merged git branches

To remove all the local branches, which are merged into the main branch, navigate to the root of the repository and run the following git commands,

  • Fetch the latest updates from the git repository
git fetch
  • See the list of local branches available in the repository
git branch
  • Delete all local branches that have been merged to main branch
git branch --merged main | grep -v "^\* main" | xargs -n 1 -r git branch -d

Explanation:

This command is a combination of several Git commands and shell commands that work together to delete all the local Git branches that have been merged into the main branch, except for the main branch itself.

Here’s a breakdown of the individual commands and what they do:

  1. git branch --merged main: This command lists all the local branches that have been merged into the main branch. The --merged option tells Git to only list branches that have been fully merged into main.
  2. grep -v "^\* main": This command filters out the main branch from the list of branches. The -v option tells grep to invert the match, i.e., show only the lines that do not match the pattern. The pattern in this case is "^\* main", which matches lines that start with an asterisk (\*) followed by the text main.
  3. xargs -n 1 -r git branch -d: This command passes the list of merged branches (excluding main) to the git branch -d command, which deletes each branch. The -n 1 option tells xargs to pass one branch name at a time to the git branch -d command. The -r option tells xargs to only run the git branch -d command if there is at least one branch name to pass to it. The -d option tells Git to delete the branches, but only if they have been fully merged into the current branch (in this case, main). Note that this option will fail if the branch has unmerged changes, in which case the -D option could be used instead to force-delete the branch.

List of git commands to remove local nonexistent git branches

Similarly, run the following git commands to remove all the deleted branches from the local computer:

  • Fetch the latest updates from the git repository
git fetch
  • See the list of local branches available in the repository
git branch
  • Delete all local branches that have been merged to main branch
git branch -vv | grep ': gone]' | grep -v '\*' | awk '{ print $1; }' | xargs -r git branch -d

Explanation:

This command is a combination of several commands in a shell pipeline that work together to delete Git branches that are marked as "gone".

Here’s a breakdown of the individual commands and what they do:

  1. git branch -vv: This command lists all the Git branches in the local repository, with additional information about their upstream branches (if any). The -vv option adds more information about whether each branch is “up to date”, “behind”, or “ahead” of its upstream branch.
  2. grep ': gone]': This command filters the output of the git branch -vv command to only show the branches that are marked as "gone". The grep command searches for lines that contain the text ": gone]", which indicates that a branch is gone.
  3. grep -v '\*': This command further filters the output to exclude any branches that are currently checked out (indicated by the asterisk symbol). The -v option tells grep to invert the match, i.e., show only the lines that do not contain an asterisk.
  4. awk '{ print $1; }': This command extracts only the branch names from the filtered output. The awk command splits each line of input into fields and prints only the first field, which is the branch name.
  5. xargs -r git branch -d: This command passes the branch names to the git branch -d command, which deletes each branch. The -r option tells xargs to only run the git branch -d command if there is at least one branch name to pass to it. The -d option tells Git to delete the branches. Note that this option will fail if the branch has unmerged changes, in which case the -D option could be used instead to force-delete the branch.

Note: you can test these git commands by creating temporary repository on any git platform like, GitHub, GitLab or Bitbucket.

vi Editor: UNIX

vi is a command-line text editor originally created for the Unix operating system.

The name vi is derived from the shortest unambiguous abbreviation for the command visual; the command in question switches the line editor ex to visual mode.

Most of the network administrators are familiar with this little editor in Unix, because they use it regularly. But, for first timers, it’s most difficult editor. First timers have to remember all commands and keys to edit a simple file.

vi has two modes, Insert mode and Command mode. In insert mode, you can add/edit the texts in file. And in command mode, you can navigate and command the editor like save, exit, copy, paste, etc.

These are the commands and keys for those who want to get familiar with vi editor.

Command to open the vi editor:

vi filename

This command creates a new file if filename is not available in current directory. By default, vi begins in command mode.

To start the insert mode, you can use following keys:

Insert text at beginning of line:

I

Insert text at cursor:

i

append text after cursor:

a

Append text at line end:

A

Open line above cursor:

O

Open line below cursor:

o

To switching back, and start the Command mode, press [ESC]

Most commands execute as soon as typed except for “colon” commands which execute when you press the return key.

For cursor movement in command mode, you can use following commands/keys:

Go to beginning of line

0

Go to end of line

$

Go to line number ##

:##

Go to line n

nG

Go to last line

G

Left 6 chars

6h

Move left, down, up, right

h j k l

Move left, down, up, right

← ↓ ↑ →

Scroll Backward 1 screen

[ctrl] b

Scroll Forward 1 screen

[ctrl] f

Scroll by sentence forward/backward

( )

Scroll by word forward/backward

w b

Scroll by paragraph forward/backward

{ }

Scroll Up 1/2 screen

[ctrl] u

Scroll Down 1/2 screen

[ctrl] d

For deleting/changing text/character in command mode, you can use following commands/keys:

Change word

cw

Replace one character

r

Delete word

dw

Delete text at cursor

x

Delete entire line (to buffer)

dd

Delete (backspace) text at cursor

X

Delete 5 lines (to buffer)

5dd

Delete current to end of line

D

Delete lines 5-10

:5,10d

For editing content in command mode, you can use following commands/keys:

Copy line

yy

Copy n lines

nyy

Copy lines1-2 /paste after 3

:1,2t3

Move lines 4-5/paste after 6

:4,5m6

Paste above current line

P

Paste below current line

p

Undo all changes to line

U

Undo previous command

u

Join previous line

J

Find next string occurrence

n

Search backward for string

?string

Search forward forstring

/string

% (entire file) s (search and replace) /old text with new/ c (confirm) g (global – all)

:%s/oldstring/newstring/cg

Ignore case during search

:set ic

Repeat last command

.

For saving and quiting in command mode, you can use following commands/keys:

Save changes to buffer

:w

Save changes and quit vi

zz or :wq

Save file to new file

:w file

Quit without saving

:q!

Save lines to new file

:10,15w file

In all of the above commands, a number n will tell vi to repeat that command n times.

:syntax on Turn on syntax highlighting
:syntax off Turn off syntax highlighting
:set number Turn on Line numbering (shorthand :set nu)
:set nonumber Turn off Line numbering (shorthand :set nonu)

:set ignorecase Ignore case sensitivity when searching
:set noignorecase Restore case sensitivity (default)