Work faster with Git aliases
Tags: beginners, productivity, discuss, git
Published: 2019-03-22 19:27:45

---
title: Work faster with Git aliases
published: true
description: Common Git aliases I use every day
tags: beginners, productivity, discuss, git
cover_image: https://images.unsplash.com/photo-1462045504115-6c1d931f07d1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1951&q=80
---

Back when I started with programming, there was no Git yet.
I tried using [Visual Sourcesafe](https://en.wikipedia.org/wiki/Microsoft_Visual_SourceSafe) a couple of times for some pet projects and the only thing I do remember about Visual Sourcesafe is that the database would become corrupted quite often. 

![Visual Sourcesafe user interface](https://www.codeproject.com/KB/cs/VSSClient/VSSClient.jpg)


If you're interested in some history about VSS and its issues, check out [this article](http://www.highprogrammer.com/alan/windev/sourcesafe.html) or this one [here](https://www.developsense.com/testing/VSSDefects.html). 

When I started my first job, the team I worked in was using a company-internal source code control system (I tried remembering the name of it, but I can't - I'll update the post as soon as I remember the name. UPDATE: [Source Depot](https://arstechnica.com/information-technology/2017/02/microsoft-hosts-the-windows-source-in-a-monstrous-300gb-git-repository/) was the name). Shortly after that, we moved the source code over to the TFS ([Team Foundation Server](https://en.wikipedia.org/wiki/Team_Foundation_Server)) that was using Team Foundation Version Control. 

![Team Foundation Server user interface](https://docs.microsoft.com/en-us/visualstudio/releasenotes/media/searchacrosscode-0.png)

Later, we would eventually move to Git. At the beginning of my career, I was using Visual Studio heavily, and all source code control commands were available from the user interface (they were also available from the console, but most of the work got  done in the user interface).

One feature I liked about TFS was the ability to [create shelvesets](https://visualstudiomagazine.com/articles/2011/03/23/wcivg_manage-in-progress-code-with-tfs-shelvesets.aspx) that could get easily shared with others. The equivalent of this in Git today is probably creating a branch and pushing it. For a little while, after I've started using Git, I'd still miss shelvesets, but once I got used to the way Git does things with branches the need for shelvesets went away. 

# Git Aliases

Ok, enough of history, let's talk about Git aliases. I have a bunch of them in my `.gitconfig` file, but I don't use all of them too often. I still like to use the GUI (I am using [Sourcetree](https://www.sourcetreeapp.com/), which is free for both Windows and Mac), especially for merges. 

Here's the link to my full [.gitconfig file](https://raw.githubusercontent.com/peterj/dotfiles/master/git/.gitconfig) - note that most of these probably came from the repos I based my dotfiles on ( https://github.com/jfrazelle/mac-dev-setup and https://github.com/mathiasbynens/dotfiles).

In addition to the aliases, I also have an alias set for the Git binary, so I don't have to type all three letters, and I can only type `g` (huge time/type savings, I know :)). 

I've grouped the aliases below - note that there are way more aliases in my config file (related to finding stuff, diffing, merging, etc.), however, I probably never used them, and that's why I haven't mentioned them. I do most of the searching and merging in the GUI.

## Basic aliases

These are mostly shortcuts, rather than more complicated commands.

```
# View the current working tree status using the short format
s = status -s

# Clone a repository including all submodules
c = clone --recursive

# List all tags
tags = tag -l

# Lists all remote and local branches
branches = branch -a

# List remotes with URLs
remotes = remote -v
```

Command below shows how you can clone a repo: 

```
g c [repo]
```

## Working with forks
This is something you need to do quite often. I have two different aliases to do this - one merges the upstream master into local (origin) fork (`fu`) and the second one makes the fork even with the upstream master, discarding all local changes (`fuf`).

```
# Merges upstream master into local (origin) fork
fu = !"git fetch upstream; git checkout master; git merge upstream/master"

# Makes the fork even with the upstream master, discarding all local changes
fuf = !"git fetch upstream; git checkout master; git reset --hard upstream/master; git push origin master --force"
```

## Branches
I use an alias called `go` to either switch to an existing branch, or create a new one if it doesn't exist. This is one of those commands I use fairly often:

```
# Switch to a branch, creating it if necessary
go = "!f() { git checkout -b \"$1\" 2> /dev/null || git checkout \"$1\"; }; f"

# Remove branches that have already been merged with master a.k.a. ‘delete merged’
dm = "!git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d"
```

## Commits
Amend alias is the one I use if I want to add current changes to the latest commit (instead of creating a new commit):

```
# Amend the currently staged files to the latest commit
amend = commit --amend --reuse-message=HEAD
```


Do you have any favorite Git aliases that you're frequently using?

UPDATE: The internal source code control we used was called Source Depot (found an article about it [here](https://arstechnica.com/information-technology/2017/02/microsoft-hosts-the-windows-source-in-a-monstrous-300gb-git-repository/))