jayjays
jayjays

Reputation: 71

how to make this git alias?

I use this command to export all files in specify commit(s)

git archive --output=export.zip --format=zip HEAD $(git diff --name-only COMMIT1 COMMIT2)

I want to make an alias so I dont need type this long command everytime.

I tried:

git config --global alias.he "archive --output=\"$1\" --format=zip HEAD \$(git diff --name-only \"$2\" \"$3\")

However, when I run git he 1.zip COMMIT1 COMMIT2, it prompts

error: unknown option `name-only'

Could anyone help me for this?

Thanks

Upvotes: 2

Views: 843

Answers (4)

twalberg
twalberg

Reputation: 62499

Aliases don't generally take positional parameters. A better way to do this would be to write a short script - assuming you want to use the syntax you mention (git he 1.zip COMMIT1 COMMIT2), name the script git-he and put it somewhere in your path (e.g. ${HOME}/bin if you are the only expected user, or /usr/local/bin for broader accessibility):

#!/bin/sh
git archive --output="$1" --format=zip HEAD $(git diff --name-only "$2" "$3")

For subcommands it doesn't recognize, git will search your path for a script named git-<command>. It would be a good idea to put some sanity checking on the arguments in, and make sure to chmod +x <path>/<to>/git-he.

Upvotes: 1

Dahir Warsame
Dahir Warsame

Reputation: 143

Just open you terminal and type

pico ~/.gitconfig

and paste this under your credentials

[alias]
    ci = commit
    cm = commit -am
    br = branch
    newbr = checkout -b
    delbr = branch -d
    showbr = for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort
    co = checkout
    df = diff
    dfc = diff --staged
    changes=diff --name-status -r
    diffstat=diff --stat -r
    lg = log --graph --oneline --pretty=format:'%Cred%h%Creset - %C(yellow)%s%Creset %C(green)<%an<%Creset' --abbrev-commit
    new = !sh -c 'git log $1@{1}..$1@{0} "$@"'
    llog = log --date=local
    fork = remote add -f

And when ever you want to add or remove aliases just edit this.

Upvotes: 0

Niklas Berglund
Niklas Berglund

Reputation: 3583

Change your double quotes for the command itself to single quotes, like so:

git config --global alias.he "archive --output='$1' --format=zip HEAD \$(git diff --name-only '$2' '$3')

No need to escape single quotes within double quotes.

Upvotes: 0

M. Scott Ford
M. Scott Ford

Reputation: 2989

I suggest using a shell alias for this. I have several aliases defined in my bash profile.

alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '

I mostly just use gs instead of git status. But that should give you an idea.

Upvotes: 0

Related Questions