Useful Git Commands that I use from time to time

Useful Git Commands that I use from time to time

Table of Contents

A long time ago in a galaxy far, far away…

This post is over 24 months old, that is an lifetime in tech! Please be mindful of that when reading this post, young Padawan, as it could be outdated. I try to keep things up to date as much as possible. If you think something needs updating, please let me know in the comments.

Introduction

There are a few advanced things that I want to do with git from time to time and I always forget the commands. So I thought I would write them down here. Here are my gitbits…

2025 Update

I needed to update some of these commands so that I can run them on a Windows machine when working with clients who supply their own kit. Where the commands differ, I have included both.

Gitbits

Aliases

git config --get-regexp ^alias

I have started to use aliases to make my life easier, saves me having to remember commands that I use regularly. Trouble then is remembering the alias :) This command will list all of the aliases that I have created. It also returns the command.

Remotes

git remote -v

This command will list all of the remotes that are configured for the repository. I use this to check that I have the correct remotes configured or to find out where I put the remote if I have forgotten :)

Branches

I have found that over time, keeping a local repository tidy can be a bit of a pain. I have a few commands that I use to help me keep things tidy.

git status

Not an uncommon command admittedly, but worth pointing out that if you run this on a feature branch it will inform you of the remote tracking branch that it is associated with, or if the upstream branch has been deleted.

git fetch --prune

Git fetch by default updates your local repository with all existing remote branches, but does not remove any that have been deleted. This command will remove any remote tracking branches that have been deleted on the remote repository.

git branch -d feature/my-new-feature
git push origin --delete feature/my-new-feature

Tread carefully with this one! This will remove a branch from your local repository then also remove it from the remote repository. This isn’t something you typically want to do, but there may be times you might want to do this.

git log --branches --not --remotes --no-walk --decorate --oneline

This lists any local branches that have not been pushed to the remote. I use this to check that I have pushed all of my changes. Note that if a branch has been merged remotely, then it’s local counterpart will get listed. I have aliased this to git unpushed.

# zsh
git fetch --all -p;git branch -vv | grep ': gone]' | awk '{ print $1 }' | xargs -n 1 git branch -D

Following on from the previous command, this will delete any local branches that have been merged remotely. I use this to keep my local repository tidy. This is super destructive and should be used with caution, I have a blog post to write on what this does and how to use it. I have aliased this to git deleteMerged albeit I had to jump through some hoops to get this working correctly in my git config file. There are multiple commands here, let me break them down:

  • git fetch --all -p fetches all of the branches from the remote repository.
  • git branch -vv lists all of the branches and shows the remote tracking branch that each branch is associated with.
  • grep ': gone]' filters the list to only show branches that have been deleted from the remote repository (by looking for the keyword gone).
  • awk '{ print $1 }' prints the first column of the output, which is the local branch name.
  • xargs -n 1 git branch -D deletes the branch that was printed by the previous command.

More to Follow

Will add more as and when I think of them. Any commands you find useful? Please let me know in the comments!

Comments

#mtfbwy