简体   繁体   中英

I want to setup git alias to change a directory in windows

I have the following git aliases setup

[alias]
    history = log --all --graph --decorate --oneline
    project1 = "cd /c/projects/test1"
    project2 = "cd c:/projects/test2"

But on running git project I get this error

Expansion of alias 'project1' failed; 'cd' is not a git command

在此处输入图像描述

I am trying this on a Windows 10 machine

Aliases to commands that are not git commands should begin with ':' :

[alias]
    foo1 = "cd foo1"    # will expand to "git cd foo1"
    foo2 = "! cd foo2"  # will expand to "cd foo2"

@phd is correct: I only thought of non git commands, but cd is specific and needs to modify the environment of the current shell .

I don't see an easy way to turn cd foo into an alias.

You should look at how to create an alias or function for the shell(s) you use.

Git aliases are useful for making aliases for long git commands, like your history command. However, for non-git commands , I wouldn't use git aliases.

The reason for the error you're getting is that your aliases are expanded to

git cd /c/projects/test1
git cd c:/projects/test2

But, cd is not a git command, what you want is

cd /c/projects/test1
cd c:/projects/test2

Since these commands are not related to git, I would pick another tool to do this.

From looking at your screenshot, it appears you are using Powershell. You can use powershell functions to achieve that:

function project1 { cd /c/projects/test1 }
function project2 { cd c:/projects/test2 }

Put this in your powershell profile.ps1 file to make these functions available every time you launch powershell. More info here .

For Windows (tested on 7,10) you may try https://en.wikipedia.org/wiki/DOSKEY feature:

  1. Put macros.doskey file to C:\Users\%userprofile%\bin with the following content
    mvnx=mvn clean install -s path\to\my\customsettings.xmlfile -T 2C... ... all your other windows aliases...
  2. Open cmd and type the following information
    reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"%userprofile%\bin\macros.doskey\"" /f reg query "HKCU\Software\Microsoft\Command Processor" /v Autorun
  3. Restart Windows cmd
  4. After that, once I'm type mvnx in Windows CMD it triggering the alias above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM