Git aliases with arguments and fish shell

Viewed 864

The Human Git aliases includes some functions which take arguments, for example

into = "!f() { B=$(git current-branch); git checkout $1; git pull; git merge $B --no-edit; }; f"

I'm having trouble getting similar things to work in fish.
I can create a fish alias, say called git-into, which works, but I'd rather have a git alias so I don't have to remember which commands use the dash and which use the space.

Using ! fish -c STUFF" or "! bash -c STUFF doesn't pass in the arguments properly.

How would I make it so that

git into repository

will do the equivalent of the above script?

2 Answers

Adding a leading "!" to an alias indeed means that git runs it in a shell, but that shell isn't fish here. It's the shell that was given at build time as SHELL_PATH, most likely (and defaulting to) "/bin/sh". And since git uses the same shell to run other things (e.g. the pager), it should definitely be a POSIX-compatible one, which rules out fish.

Your user's chosen login shell doesn't matter.

So you can simply use the original code and it should work.

Alternatively, you could put this into a script called git-into (no ending, proper shebang-line and executable bit) in $PATH, and git will find it when you call git into.

If you really want to use fish -c here, you'll have to deal with two slightly incompatible layers of quoting, which isn't something I'd recommend.

There isn't any git current-branch.

If you want the current branch name:

# extract the current branch name
git branch | grep \* | cut -d ' ' -f2

# or 
git rev-parse --abbrev-ref HEAD

# since you are already on the current branch all you need is pull & merge
# be aware that there can be conflicts
into = "!f() { git pull; git merge $B --no-edit; }; f"

If you want to pass parameter and checkout different branch you already have it in your alias

git checkout $1
Related