How to create a Git alias with nested commands with parameters?

In my dotfiles I have the following function which works:

function undelete {
  git checkout $(git rev-list -n 1 HEAD -- "$1")^ -- "$1"
}

…which I use like this:

$ undelete /path/to/deleted/file.txt

I'd like to scope this command since it's a git command.

How do I create a git alias so that I can use this git alias command?

$ git undelete /path/to/deleted/file.txt

Here are two, of my attempts which do not work:

git config --global alias.undelete "!f() { git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1; }; f"
git config --global alias.undelete "!sh -c 'git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1' -"

It is possible to do this with aliases (see jthill's comment):

git config --global alias.undelete '!f() { git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1; }; f'
git config --global alias.undelete '!sh -c "git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1" -'

I recommend writing anything complicated as a shell script:

#! /bin/sh
#
# git-undelete: find path in recent history and extract
. git-sh-setup # see $(git --exec-path)/git-sh-setup

... more stuff here if/as appropriate ...
for path do
    rev=$(git rev-list -n 1 HEAD -- "$path") || exit 1
    git checkout ${rev}^ -- "$path" || exit 1
done

(the for loop is intended to make it allow multiple path names to "undelete").

Name the script git-undelete , put it in your $PATH (I put scripts in $HOME/scripts ), and any time you run git undelete , Git will find your git-undelete script and run it (with $PATH modified to have git --exec-path up front, so that the . git-sh-setup works).

链接地址: http://www.djcxy.com/p/3162.html

上一篇: 修复Internet Explorer中的JavaScript数组函数(indexOf,forEach等)

下一篇: 如何使用带参数的嵌套命令创建Git别名?