Macos 在git alias函数中使用和检查可选参数
我想用化名做些疯狂的吉特舞。具体来说,我希望git别名根据传入参数是否为空来读取这些参数,并以不同的方式处理命令;有点像重载函数。我已经从其他问题中获得了一些创建函数的好主意,比如,但是以一种智能的方式处理参数仍然让我难以理解 我最后做的是为不同的特殊性级别创建3个函数,并在一个带有默认值的链中调用它们。这是我的git配置中的一部分Macos 在git alias函数中使用和检查可选参数,macos,git,bash,Macos,Git,Bash,我想用化名做些疯狂的吉特舞。具体来说,我希望git别名根据传入参数是否为空来读取这些参数,并以不同的方式处理命令;有点像重载函数。我已经从其他问题中获得了一些创建函数的好主意,比如,但是以一种智能的方式处理参数仍然让我难以理解 我最后做的是为不同的特殊性级别创建3个函数,并在一个带有默认值的链中调用它们。这是我的git配置中的一部分 # get the current local branch current = symbolic-ref -q --short HEAD # Calls sy
# get the current local branch
current = symbolic-ref -q --short HEAD
# Calls sync-branch-with-origin-master using the current branch for the first property and origin as the remote name.
# ex. `git sync master` will checkout master from origin, pull the latest, checkout your current branch and rebase it with master.
sync = "!f() { currentBranch=$(git current); git sync-branch-with-master $currentBranch $1; }; f"
# get latest version of branch 2 from origin and rebase branch 1 onto it.
sync-branch-with-master = "!f() { git sync-branch-with-origin-master $1 origin $2; }; f"
# get latest version of branch $3 from origin $2 and rebase branch $1 onto it.
sync-branch-with-origin-master = "!f() { git checkout $3; git pull $2 $3; git checkout $1; git rebase $3; }; f"
这太棒了!但我真正想要的是检查3美元或2美元是否存在,并以不同方式处理这些情况
顺便说一下,我正在Mac上使用bash。您可以在shell函数中编写整个脚本(f()
,在每个别名中)。但这很快就变得不可读/无法维护。对于复杂的事情,我创建一个shell脚本并从别名调用它
#! /bin/sh
# git-foo.sh
... body of script goes here ...
然后:
git config alias.foo '!git-foo.sh'
或者别的什么
不过,在这种特殊情况下,我怀疑您的做法有些过头了:-)您可能只想使用git-pull--rebase
或git-fetch
,后跟git-rebase
。(并不是说这些与您拥有的完全相同,所以可能不是;但请记住,git pull
基本上与get fetch
后跟git merge
相同)