Doing It Wrong

learn in public

Site Navigation

  • Home
  • Books
  • Work & Play

Site Search

You are here: Home / Development / Useful Git Commands

Useful Git Commands

posted on December 10, 2021

Git Commands that I Find Useful

Many of these are initially sourced from Stack Overflow or great articles that have titles like “5 Must-Know Git Commands” then modified to fit my workflow (here’s one great example for popular git configs). Some are actually put together by reading the docs.

Delete Branches that have been Remove from Remote

For this to delete branches, local must know that remote has removed branches (which git branch -v lists as [gone]). One way to accomplish this is to configure your fetch to always prune.

git branch -v | awk '/ \[gone\] /{ print $1 }' | xargs git branch -dCode language: JavaScript (javascript)

Always Prune on Fetch

From the docs, “…remove any remote-tracking references that no longer exist on the remote.” git pull and git fetch would both have the following setting applied, since git pull is (in my mind) like running git fetch and git merge.

git config --global fetch.prune trueCode language: PHP (php)

Viewing the global git config (cat ~/.gitconfig) should now show

[fetch]
    prune = trueCode language: JavaScript (javascript)

Always Rebase on Pull

git config --global pull.rebase trueCode language: PHP (php)

Viewing the global git config (cat ~/.gitconfig) should now show

[pull]
    rebase = trueCode language: JavaScript (javascript)

List Subtrees

git log | awk '/git-subtree-dir: / { print $2 }' | uniqCode language: JavaScript (javascript)

As an alias (global .gitconfig):

[alias]
    subtrees = !git log | awk '/git-subtree-dir: / { print $2 }' | uniqCode language: JavaScript (javascript)

Delete Orphaned Local Branches

Prerequisite to this your local branches must have references removed to non-existent remote branches.

git branch -v | awk '/ \[gone\] /{ print $1 }' | xargs git branch -dCode language: JavaScript (javascript)

As an alias (global .gitconfig):

[alias]
    prune-delete = !git branch -v | awk '/ \\[gone\\] /{ print  }' | xargs git branch -dCode language: JavaScript (javascript)

Filed Under: Development Tagged With: git, Reference

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Profile Links

  • GitHub
  • Buy Me a Coffee?

Recent Posts

  • Event Listeners
  • A Philosophy of Software Design
  • The Programmer’s Brain
  • Thoughts on Microservices
  • API Design Patterns

Recent Comments

No comments to show.

Archives

  • May 2025
  • September 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • December 2022
  • December 2021

Categories

  • Development