简体   繁体   English

如何在Windows cmd上执行git log?

[英]How to execute git log on windows cmd?

I have the following command: 我有以下命令:

git log --pretty=tformat:'<li>%h %ci %d %s</li>' > changelog.html

That saves the git log to the changelog.html file. 这将git日志保存到changelog.html文件中。 When i execute it through git bash, it works fine, but when i put this code in a .bat file and run it, i get the following error: 当我通过git bash执行它时,它工作正常,但是当我将此代码放在.bat文件中并运行它时,我收到以下错误:

The system could not find the specified file

I think it's caused by the quotes on the format parameter, but i don't know how to solve the problem... Is there a way to escape the quotes maybe? 我认为这是由格式参数的引号引起的,但我不知道如何解决问题...有没有办法逃避引号呢?

Inside a Windows batch file, you have to: 在Windows批处理文件中,您必须:

  • double % chars. %字符。 for them to be taken as literals 他们被视为文字
  • use double quotes to protect embedded whitespace or other special chars. 使用引号来保护嵌入的空格或其他特殊字符。 such as > : 例如>
git log --pretty=tformat:"<li>%%h %%ci %%d %%s</li>" > changelog.html
  • Generally, you can also use ^ to escape individual characters in an unquoted string (eg, ^% to represent literal % , or ^| to represent literal | ), but that wouldn't work here , because of the following caveat : 通常,您还可以使用^来转义未加引号的字符串中的单个字符(例如, ^%表示文字% ,或^|表示文字| ),但这不起作用 ,因为以下警告
    You cannot use ^ to escape a space character in order to form a single argument with embedded whitespace; 不能使用^来转义空格字符以形成具有嵌入空格的单个参数; for instance, foo^ bar is still split into 2 arguments, foo and bar . 例如, foo^ bar仍然分为2个参数, foobar

Some background for readers from the Unix world : 来自Unix世界的读者的一些背景

  • There is no equivalent to a POSIX-like shell's single -quoted strings (strings to be taken literally , with no expansions taking place) in the Windows batch language. 在Windows批处理语言中, 没有相当于类似POSIX的shell的引号字符串字面意义上的字符串,没有发生扩展)。
    • Single quotes generally have no special meaning in the Windows batch language (except in certain special contexts, such as a for loop) - they canNOT be used to delimit a string with embedded whitespace. 引号通常在Windows批处理语言中没有特殊含义 (在某些特殊上下文中除外,例如for循环) - 它们不能用于分隔具有嵌入空格的字符串。
  • Thus, analogous to how you have to escape $ in double-quoted bash strings as \\$ to prevent expansion, you have to escape % as %% in double-quoted strings to prevent expansions in batch files, given that an identifier enclosed in % chars. 因此,类似于你怎么也逃脱$双引号的bash字符串作为\\$防止膨胀,你有逃跑%作为%%在双引号字符串,以防止在批处理文件的扩展 ,因为标识符括在%字符。 (eg, %PATH% ) signifies a variable reference to expand. (例如, %PATH% )表示要扩展的变量引用
    • The curious thing is that an interactive cmd.exe prompt doesn't require escaping of % , whereas batch files do. 好奇的是一个互动 cmd.exe提示不需要逃逸% ,而批处理文件执行。 More specifically, % chars. 更具体地说, % chars。 in an interactive command line are treated as literals, unless they are part of a variable reference to an existing variable (eg, %windir% ), in which case expansion does take place. 在交互式命令行中被视为文字, 除非它们是对现有变量的变量引用的一部分(例如, %windir% ),在这种情况下会发生扩展。 This oddity is the reason that a for loop must be written as for %i in ... ( one % ) on the command line, and as for %%i in ... ( two % ) in a batch file. 这一奇怪现象是,原因for循环必须写成for %i in ...一个 % )在命令行上,并作为for %%i in ... %在批处理文件)。
  • A caveat re passing double-quoted strings to batch files : When passing an argument enclosed in double quotes, the double quotes are included in the argument value ; 警告将双引号字符串传递给批处理文件 :当传递用双引号括起来的参数时, 双引号包含在参数值中 ; the ~ parameter modifier can be used to strip them ; ~参数修饰符可用于剥离它们 ; eg, %~1 returns the 1st argument without double quotes (if any). 例如, %~1返回没有双引号的第一个参数(如果有的话)。
  • Finally, the counterpart of \\ -escaping individual characters in an unquoted string in a POSIX shell is ^ -escaping , but, as stated, ^ cannot be used to escape a space in order to form a single argument with embedded whitespace. 最后, 的配对\\一POSIX壳没有引号的字符串-escaping 各个字符^ -escaping,但是,如上所述, ^不能使用,以形成带有嵌入空白一个参数逸出的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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