Cmder : how to use an alias in another alias?

守給你的承諾、 提交于 2019-12-07 12:03:48

问题


I would use an alias in another alias in Cmder.

I'm having a nice alias (the first) to a big command, and I would use it inside another one (the second) :

mergelocdist=git fetch origin --prune --verbose && git checkout $1 && git merge --ff-only --verbose origin/$2 || git rebase --preserve-merges --verbose origin/$2

mm=mergelocdist master master

The aliases are in the Cmder aliases file, and I'm on Windows 7 x64.

The topic has already been asked in this question,with no replies as now.

If you answer to me, please also answer to this guy :)

Found this, seems to have something to help but I don't figure how.


回答1:


Benj: I suspect that you've already found an answer by now, but I see that no one has answered this question, so I will.

If I may make an alternative suggestion to the aliases file. It looks like you're trying to solve this for git.

The best way I've found to do this in Windows is to edit the ~/.bashrc file. On most Windows machines it would be under C:\Users\<USERNAME>\.bashrc.

You can create a function such as:

function MergeLocDistFn {
    local co_branch=$1
    local merge_branch=$2

    echo ">> Debug Command: git checkout ${co_branch}"
    git checkout ${co_branch}

    echo ">> Debug Command: git merge --ff-only --verbose origin/${merge_branch} || git rebase --preserve-merges --verbose origin/${merge_branch}"
    git merge --ff-only --verbose origin/${merge_branch} || git rebase --preserve-merges --verbose origin/${merge_branch}
}

Then you can create aliases in the ~/.bashrc file, like the following:

alias mergelocdist=MergeLocDistFn
alias mm='MergeLocDistFn master master'

The advantage to this approach is that it will allow you to get really sophisticated with the function and as I'm showing here, debug it as well with echo statements.

See the attached picture of the example output. example output from bashrc

Note also, if you'd rather do this for a PowerShell prompt, it's even easier to modify the PowerShell profile, just let me know and I'll show you how.

Another note, you can also create a function in the .bashrc for the current branch. Here's an example function:

function current_branch() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}

Here's an example of using the current_branch function:

alias gpush='git push -u origin $(current_branch)'

I hope that his helps, just let me know if you need any clarifications on this.



来源:https://stackoverflow.com/questions/38351922/cmder-how-to-use-an-alias-in-another-alias

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!