Cmder : how to use an alias in another alias?
Asked Answered
S

1

6

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.

Standstill answered 13/7, 2016 at 12:35 Comment(0)
B
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.

Brewer answered 31/1, 2017 at 2:59 Comment(3)
Thanks for helping ! I did not found any solution at this time, so thanks again ! I would kiss you if I could.Standstill
Nice shot for a first answer. I put a link to your answer in the comments of the question I linked in mine.Standstill
Great to hear that it is helpful. If you want to explore further, I have also blogged about other bashrc techniques at erpenbeck.io/2016/10/27/bashrc. For example, if your team uses Git upstream remotes, develop/master branches, etc, there is a handy method called a cloneIt that will configure it all in one command. Customize it to your team's Git conventions.Brewer

© 2022 - 2024 — McMap. All rights reserved.