Joeri Timmermans
Joeri Timmermans
PHP/Symfony Developer with
a passion for food and drinks
Developer @ Intracto

How to create custom git aliases for a project

Wednesday July 27, 2016 - Permalink - Tags: setup, git, aliases, composer

This guide will help you create custom git aliases and share them with everyone who clones your project.

Assumptions

  • You know how to work with git (More info)
  • You know how to work with composer (More info)

STEP 1: Create a custom git aliases file

In order to create aliases that are versioned in your git setup you need to create a .gitalias file inside your project

[alias]
    branch-name = !git rev-parse --abbrev-ref HEAD #print current branch name

    publish = !git push -u origin $(git branch-name) #push current branch to remote server

    unpublish = !git push origin :$(git branch-name) #delete current branch on remote server

    remove = "!f() { git branch -D $1; }; f" #remove a branch localy

    feature = "!f() { git checkout -b feature/$1; }; f" #create branch with prefix feature

    myscript = "!f() { bash myscript.sh; }; f" #run a custom script

STEP 2: Load the file in your git settings

This file contains your aliases but is not autoloaded into your git config. In order to do this we must run the following command:

git config include.path '../.gitalias'

STEP 3 (OPTIONAL): Auto include our aliases via composer

To help new users install our aliases we can install the aliases for them by adding the following lines to our composer.json:

"scripts": {
    "post-install-cmd": [
        "git config include.path '../.gitalias'"
    ],
    "post-update-cmd": [
        "git config include.path '../.gitalias'"
    ]
},

We can run this config command each time we do composer install or update since git is smart enough to only add this line once.

STEP 4: Profit

You can now use all your custom aliases inside your project, and if you create new aliases just add them to your .gitalias file and commit them as you would do with any other code change. Git wil load this file each time your run a git command.

Example:

git feature myfeature
Switched to a new branch 'feature/myfeature'

git publish
[new branch]      feature/myfeature -> feature/myfeature
Branch feature/myfeature set up to track remote branch feature/myfeature from origin by rebasing.

git unpublish
[deleted]         feature/myfeature

git remove feature/myfeature
Deleted branch feature/myfeature (was xxxxxx).
comments powered by Disqus