8
bd = "!f() { git branch --merged | egrep -v '(^\*|master|dev)' | xargs git branch -d }; f"

I'm trying to alias a git command to remove all of my local merged branches.
When I put the bash command into my gitconfig as above, git complains about a bad config line:
fatal: bad config line 26 in file /Users/johnsona/.gitconfig

nhyne
  • 405
  • 1
  • 5
  • 18

2 Answers2

8

I'd recommend making this a bash script in your PATH instead, and then calling that script in your git alias instead (or if it's in your PATH anyway, just name the file git-bd).

For example, make the file ~/bin/git-bd

#!/usr/bin/env bash
git branch --merged | egrep -v '(^\*|master|dev)' | xargs git branch -d

Make the file executable with the command:

chmod +x ~/bin/git-bd

And make sure your .bashrc, .bash_profile or .bash_login file has the line:

export PATH="$HOME/bin:$PATH"

And you can either just call git-bd directly, or add the alias in your .gitconfig like so:

bd = "!git-bd"

To add to this answer, the reason you are getting a bad config error may be due to the back-slashes. The git-config will read them as is, so you need to escape them again with a second backslash.

Jack Bracken
  • 1,233
  • 12
  • 23
  • IMO you should put your last paragraph first because it would answer the question before making a recommendation. – Jeff Puckett Oct 21 '16 at 16:49
  • If you think that's a good idea feel free to edit. That does make sense to me, but I'm now on my phone and unable to make the edit myself – Jack Bracken Oct 21 '16 at 17:06
  • Would it be possible to write the function inside of my `.bash_profile` and then have the gitconfig reference the command from there? – nhyne Oct 21 '16 at 17:17
  • I'm not completely certain, but it's worth trying. My suspicion is that it won't work because git probably won't be aware of your bash functions – Jack Bracken Oct 21 '16 at 17:22
  • Ya it's complaining saying `git-db: command not found`. Gonna give your solution a go and then check it. – nhyne Oct 21 '16 at 17:23
  • 6
    It is, in fact, the lone backslash that leads to `fatal: bad config line 2 in file ...`. In my test, doubling it changes the error to `Syntax error: end of file unexpected (expecting "}")` (because the shell function needs a semicolon before the `}`). – torek Oct 21 '16 at 17:48
3

To make @torek's comment more visible and expand on it: there are a couple of issues with your command like this, when you want to put it in your gitconfig, and the result can be made a bit simpler.

Suppose you want to alias the following command:

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
  • It uses | so you have to prepend the command with ! (you already did this)
  • It uses " so you either have to escape those with \" or use single ' (also already fixed)
  • It uses \ so you have to escape it with \\.

Result:

[alias]
    delete-merged-branches = ! git branch --merged | egrep -v '(^\\*|master)' | xargs git branch -d 

You could also run in bash (not that git will escape \ for you, but you have to wrap in " so you need to escape those)

git config --global alias.delete-merged-branches "! git branch --merged | egrep -v '(^\*|master|dev)' | xargs git branch -d"
PHPirate
  • 7,023
  • 7
  • 48
  • 84