git - 我可以对 Git 别名使用一组命令(外部和内部)吗?

标签 git

Windows 版 Git 有 a problem写入位于网络上的配置文件。

以下可以用作该错误的解决方法:

mv "//hyprostr/dfs/groups/developers/settings/gitconfig.txt" "./gitconfig.txt"
git config -f "./gitconfig.txt" http.proxy http://@proxy2:8080
mv "./gitconfig.txt" "//hyprostr/dfs/groups/developers/settings/gitconfig.txt"

这不是一个很好的解决方案,但它对我有用。

但我想通过 Git 别名调用这组命令...注意 - 第一个和最后一个命令是外部命令,而第二个命令是内部命令。

我阅读了关于 alias.*git help config 部分。读完后我认为不可能通过别名来编写这三个命令。我对吗?我希望我是错的,但这是可能的。如果我错了,那我该怎么办?

UPD(决定)

感谢@VonC的回答。我写了这样的脚本:

# edit_config.sh
# © Andrey Bushman, 2015
# This is a workaround of the problem described here:
# https://github.com/git-for-windows/git/issues/241
# Arguments:
# $1 - target config file
# $2 - parameter full name
# $3 - parameter value
random_file_name=$RANDOM
mv $1 $random_file_name
git config -f $random_file_name $2 $3
mv $random_file_name $1

我也在网络中找到了脚本文件。现在可以直接通过 Git Bash 启动它:

sh "//hyprostr/dfs/groups/developers/settings/edit_config.sh" "//hyprostr/dfs/groups/developers/settings/gitconfig.txt" "http.proxy" "http://@proxy2:8080"

或通过别名

git config --global alias.editconfig '!sh "//hyprostr/dfs/groups/developers/settings/edit_config.sh"'

我可以启动这个:

git editconfig "//hyprostr/dfs/groups/developers/settings/gitconfig.txt" "http.proxy" "http://@proxy2:8080"

对我来说效果很好。

最佳答案

如果无法直接实现,您仍然可以:

  • 编写一个执行这些命令的 shell 脚本(sh:甚至在 Windows 上)
  • 将该脚本添加为 git 别名。

参见“How to embed bash script directly inside a git alias

 git config --global alias.diffall '!sh myscript.sh'

实际上,如果您的脚本名为 git-xxx,您甚至不需要别名:git xxx 就可以工作(并调用 git- xxx 脚本文件)。

关于git - 我可以对 Git 别名使用一组命令(外部和内部)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31447193/

相关文章:

git - 在将功能分支 merge 到主干之前对其进行修复的最佳方法是什么?

git - 如何 'git commit'单个文件/目录

git clone 和 checkout 在一个命令中

linux - 是否有必要在每个二分步骤后清理 Linux 内核源代码树?

git - 是否可以提交违背 .gitattributes 指定的行结束行为的文件?

git - 无法将更改从 AWS CodeBuild 推送到 AWS CodeCommit

git - 我怎样才能撤消从以前的提交 : A Gitastrophy 删除

git - 切换分支不会刷新 Visual Studio 中的解决方案资源管理器

git - 我如何对包含 ">"的字符串进行 git grep?

git - git merge 是添加所有提交还是只添加最后一次提交?