4

I am trying to add a function to my global gitconfig.

for example I made the following shortcuts.

com = checkout master
cod = checkout develop

Now I am trying to make a short command to make checking out features & release faster.

Want I basically want to create is the following

cof = checkout feature/

So i can run the following command

git cof feature-name

But because there shouldn't be a space in between the / and the name it's not working. Otherwise if I put no space in between it won't work either (which is logical)

git coffeature-name

Is there anyway I can fix this in a similar way.

Danny van Holten
  • 932
  • 5
  • 22

4 Answers4

2

You can define aliases that are interpreted by the shell, by prefixing a ! to the alias value. Your args will still be appended to the full command, but you can use trickery to avoid that and do your own thing instead. With my shell the following does the trick, though it's a little more magical than I'd like:

cof = !git checkout feature/$1; shift;
Jan Krüger
  • 17,870
  • 3
  • 59
  • 51
2

I believe the alias you are looking for is something like the following:

cof = "!f() { FEATURE=$1; shift; git checkout $@ feature/$FEATURE; }; f"

This gives you all of the functionality of git checkout with a shorthand for feature branches. This allows you to use the -b, -B, or --orphan flags for creating a new branch; -- for specifying a path; etc.

Nondescript Shorthand Explanation

  • !: Tells git the alias is an alias for the shell.
  • f(): define a function within the brackets
  • f: after the braces executes the function

Function Logic

  • FEATURE=$1; shift;: The function takes the first argument, assigns it to a variable, then removes it from the list of arguments.
  • git checkout $@ feature/$FEATURE: Pass all the arguments you passed (except the first one which is the feature name) to git checkout with an argument having feature/ prefixed to the feature name.
Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55
  • Nice! This helped. Thanx! – Danny van Holten Mar 09 '17 at 18:33
  • If I want to make this for release and hotfix. I just use the f? Or can i change to to r & h? Because down here I read something about copy pasting with the bt. – Danny van Holten Mar 21 '17 at 11:11
  • 1
    @DannyvanHolten `f` is just the name of the function that is declared inline and called in the alias. It can be anything you want, something more meaningful perhaps. But the definition only exists during the call to the alias, so there is no collision concern. I personally use `f` in all of mine, short for function, to keep the visual focus on the function itself. It's practically an anonymous function so your alias can do any shell code within the brackets as if you were calling a shell script. – Steve Buzonas Mar 21 '17 at 19:37
0

An alternative would be to add a sjell script (even on Windows) called:

git-cof

(no extension)

In it, you can write any bash script you need:

#!/bin/bash
git checkout feature/$1

And you can call it as git cof feature-name.

Again this works even on Windows, and any git-xxx script in the $PATH/%PATH% can be called with the command git xxx.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

After a little research, found this and this, based on Jan Krügers answer using shell scripts.

For your alias, the following is working for me:

cof = "!bt() { git checkout feature/$1;}; bt"

EDIT: You do just want to check out an existing branch, and not create a new one, do you? Else, you would have to add -b.

Community
  • 1
  • 1
kowsky
  • 12,647
  • 2
  • 28
  • 41
  • The `bt` here is a cpoy-paste relic and does not really make sense. It should be replaced with `cof` or something. It's just the name for the function defined in `{}`. – kowsky Mar 09 '17 at 10:20