问题描述
在我的dotfiles中,我有以下的函数: function undelete {
git checkout $(git rev-list -n 1 HEAD - $ 1)^ - $ 1
}
$ undelete /path/to/deleted/file.txt
我想对这个命令加以限制,因为它是一个git命令。
如何创建一个git别名,以便我可以使用这个git alias命令?
$ git undelete / path /to/deleted/file.txt
以下是我尝试不了的两个尝试:
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 可以用别名做到这一点(见 jthill的评论): ($ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ $ 1)^ - $ 1; }; f'
git config --global alias.undelete'!sh -cgit checkout $(git rev-list -n 1 HEAD - $ 1)^ - $ 1 - '
我建议你写一些复杂的shell脚本:
#! / bin / sh
#
#git-undelete:在近期历史中查找路径并提取
。 git-sh-setup#请参阅$(git --exec-path)/ git-sh-setup
...更多内容if if / as appropriate ...
for path do
rev = $(git rev-list -n 1 HEAD - $ path)||退出1
git checkout $ {rev} ^ - $ path||出口1
完成
( for $ c $
将脚本命名为 git-undelete $ c $把它放在你的
$ PATH
中(我把脚本放在 $ HOME / scripts
中),并且任何时候你运行 git undelete
,Git会找到你的 git-undelete
脚本并运行它(使用 $ PATH
修改为前面有 git --exec-path
,这样 .git -sh-setup
作品)。
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).
这篇关于如何使用带参数的嵌套命令创建Git别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!