将参数传递给 Git 别名命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7005513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:31:14  来源:igfitidea点击:

Pass an argument to a Git alias command

gitgit-configgit-alias

提问by HaveF

Can I pass arguments to the alias of a Git command?

我可以将参数传递给 Git 命令的别名吗?

I have some alias in Git config, like so:

我在 Git 配置中有一些别名,如下所示:

rb1 = rebase -i HEAD~1
rb2 = rebase -i HEAD~2
rb3 = rebase -i HEAD~3
rb4 = rebase -i HEAD~4
....

Is it possible to make an rbalias so that git rb <x>works for any <x>?

是否可以创建一个rb别名,以便git rb <x>对任何人都有效<x>

I tried this alias:

我试过这个别名:

rb = rebase -i HEAD~

but then for instance git rb 8does not work.

但是例如git rb 8不起作用。

回答by VonC

If you consider the Git Faq section "Git Aliases with argument", you could do it, but by calling git through a shell:

如果您考虑Git 常见问题部分 "Git Aliases with argument",您可以这样做,但是通过 shell 调用 git:

[alias]
        rb = "!sh -c \"git rebase -i HEAD~\" -"

I haven't tested it yet, but if you can pass an argument, that would be the way to do it.

我还没有测试过它,但是如果你可以传递一个参数,那就是这样做的方法。

A similar solution would be to use a shell function:

类似的解决方案是使用shell 函数

[alias]
        rb = "!f() { git rebase -i HEAD~; }; f"

回答by Seth Flowers

Rebasing all commits since branching

自分支以来重新调整所有提交

If you just want to rebase all the commits that are new in your branch, since the time you branched from the parent branch, it would be easier to just have the following alias in your config:

如果您只想重新设置分支中所有新提交的基础,那么自从您从父分支分支以来,在您的配置中只使用以下别名会更容易:

rbi = !sh -c \"git rebase -i `git merge-base  HEAD`\" -

Then, if you wanted to rebase all the commits you've added to your current branch, you could simply run:

然后,如果您想重新设置您添加到当前分支的所有提交,您可以简单地运行:

git rbi parentBranch

This approach uses an argument, but instead of having to know how many commits to go back, you just supply the branch name, and it figures out most recent commit shared between the current branch and the parent branch via git merge-base

这种方法使用一个参数,但不必知道要返回多少次提交,您只需提供分支名称,它就会通过以下方式找出当前分支和父分支之间共享的最新提交 git merge-base

Why this, rather than git rebase -i parentBranch

为什么这样,而不是 git rebase -i parentBranch

The reason you would do this rather than a straight git rebase -i parentBranchis that you might not want to deal with the merge-conflicts until a later point, or even deal with a merge-conflict in one commit, and then the same conflict on the same line in another commit. See https://stackoverflow.com/a/31036645/444610

您这样做而不是直接这样做的原因git rebase -i parentBranch是您可能不想在稍后处理合并冲突,甚至不想在一次提交中处理合并冲突,然后在同一行中处理相同的冲突另一个提交。见https://stackoverflow.com/a/31036645/444610

回答by Andrew Patton

@Droogans pointed out in a commenton the accepted answer that, at least on macOS (I would imagine the same will hold true for any unix-like OS, and maybe even windows), you can just use $1as the placeholder value representing the argument in the alias. So, to set up an alias so that git rb 8becomes git rebase -i HEAD~8:

@Droogans 在对已接受答案的评论中指出,至少在 macOS 上(我认为对于任何类似 Unix 的操作系统,甚至可能是 Windows),您可以仅用$1作表示参数的占位符值在别名中。因此,要设置别名,使其git rb 8变为git rebase -i HEAD~8

    rb = "!git rebase -i HEAD~;"

You can also use it multiple times in an alias, so if, for example, you wanted an alias that would translate git f my-branchto git fetch origin my-branch:my-branch, you can do:

您还可以在别名中多次使用它,例如,如果您想要一个可以转换git f my-branch为的别名git fetch origin my-branch:my-branch,您可以执行以下操作:

    f = "!git fetch origin :"

回答by Raymond Gan

I wrote this function "grb" to do Git interactive rebase on a Mac, so I can say grb 5to show my last 5 commits:

我编写了这个函数“grb”来在 Mac 上执行 Git 交互式 rebase,所以我可以说grb 5显示我最近的 5 次提交:

function grb {
  git rebase -i HEAD~
}

The top answer on this page does not work for me. To see my .bash_profileand all other Git aliases I use on my Mac:

此页面上的最佳答案对我不起作用。要查看我.bash_profile在 Mac 上使用的我和所有其他 Git 别名:

https://github.com/rayning0/bash_profile/blob/master/.bash_profile#L146

https://github.com/rayning0/bash_profile/blob/master/.bash_profile#L146