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

How to Search for a Lost File in the Git Log

How to Search for a Lost File in the Git Log

  • 2024-04-27
  • 4 minutes to read

I have lost a file in my Git repository. How can I find it by searching the git log?

Read More
No Such Shell Function 'Zle Line Init' in Zsh

No Such Shell Function 'Zle Line Init' in Zsh

  • 2024-04-25
  • 3 minutes to read

Troubleshooting the error message "no such shell function 'zle line init'" in zsh when using OhMyPosh.

Read More
Getting Started With Python in Vscode

Getting Started With Python in Vscode

  • 2024-04-05
  • 2 minutes to read

This post will help you get started with Python in Vscode and identify some of the useful extensions to install.

Read More