A short list of Useful Git Commands

A short list of Useful Git Commands

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

Connecting Azure Data Studio to Postgres

Connecting Azure Data Studio to Postgres

  • 2024-05-09
  • 2 minutes to read

How to connect Azure Data Studio to a Postgres database.

Read More
Creating a Postgres docker container and connecting dbt

Creating a Postgres docker container and connecting dbt

  • 2024-05-09
  • 5 minutes to read

How to get started with dbt and the Postgres adapter.

Read More