【问题标题】:git add commit push one liner for powershell windowsgit add commit push one liner for powershell windows
【发布时间】:2019-10-30 12:38:06
【问题描述】:

我正在寻找一个命令,我可以在其中 add commitpush 在 Windows 上使用新的 Powershell
lazygit my commit msg 这样的单个命令 // 如果可能的话,我更喜欢不带引号

我查看了一些关于 SO 的问题,例如 git add, commit and push commands in one?,它提供了解决方案,但针对 bash

function lazygit() {
    git add .
    git commit -a -m "$*"
    git push
} 
//use it like lazygit my commit msg

另一个答案建议使用 git 别名:git config --global alias.lazy '!f() { git add -A && git commit -m "$@" && git push; }; f'

但我必须添加引号并且不能在提交消息中使用空格(给出错误error: pathspec 'commit message' did not match any file(s) known to git


当然,有一种解决方案可以使用; 在一行中编写多个命令,但我希望使用简单的单字命令

【问题讨论】:

  • 使用 "$*" 将整个 args 列表引用为一个单词,别名中的 "$@" 单独引用每个剩余的参数。 …'!f() { git add -A && git commit -m "$*" && git push; }; f'.
  • 在函数中用$args替换"$*"不起作用吗?
  • 您对编写一个 PowerShell 脚本将其封装到一个命令中有何感想?
  • @JoshuaDrake 是的,如果你能提供工作的 sn-p,我不习惯写 shell 脚本
  • @jthill 不起作用,提供多个单词时出错:pathspec '第二个参数' 与 git 已知的任何文件都不匹配

标签: windows git powershell


【解决方案1】:

这个功能对你有用吗?

function lazygit {
  param(
    [Parameter(ValueFromRemainingArguments = $true)]
    [String[]] $message
  )
  git add .
  git commit -a -m "$message"
  git push
}

运行这个:

lazygit my commit message

将运行这三个命令:

git add .
git commit -a -m "my commit message"
git push

【讨论】:

  • 我不知道我可以将函数粘贴到 powershell 中以使其运行,它可以工作但对于同一个会话窗口。我认为将其作为一个全局函数需要做一些额外的工作。
  • 将该函数添加到您的 PowerShell 配置文件脚本中,它将在所有 PowerShell 会话中可用(以 -NoProfile 开头的 PowerShell 会话除外)。有关配置文件脚本的信息,请参阅 help about_Profiles
  • 是的,这行得通。对于未来的读者:我以管理员身份打开 pshell,通过执行 notepad $PROFILE.CurrentUserAllHosts 创建和打开配置文件并在那里编写函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 2014-09-11
相关资源
最近更新 更多