A short list of Useful Git Commands

A short list of Useful Git Commands

Table of Contents

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…

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.

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.

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!

Tags :

#mtfbwy



Recent Posts

Hugo Live Reload Keeps Reloading

Hugo Live Reload Keeps Reloading

  • 2024-05-18
  • 3 minutes to read

I have run into an issue from time to time where Hugo's live reload feature keeps reloading the page. This is how I fixed it.

Read More
First Steps with Mermaid in Vscode

First Steps with Mermaid in Vscode

  • 2024-05-17
  • 7 minutes to read

My blog theme supports mermaid, a visual diagram syntax, so I wanted to try it out in vscode.

Read More
T-SQL Tuesday #174 My favourite job interview question

T-SQL Tuesday #174 My favourite job interview question

  • 2024-05-14
  • 4 minutes to read

This is my 12th contribution to TSQL Tuesday, the data community blog party. Have a read, and why not join in next time!

Read More