Pass an argument to a Git alias command
Asked Answered
K

4

78

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

I have some alias in Git config, like so:

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 rb alias so that git rb <x> works for any <x>?

I tried this alias:

rb = rebase -i HEAD~

but then for instance git rb 8 does not work.

Kb answered 10/8, 2011 at 3:20 Comment(4)
Why are you doing this so frequently that you think you need these aliases?Alicia
The question is really comes from the shortcut of "rebase", although it seems a meaningless question to these not so frequent commands. But this question can be a general question. So...Kb
possible duplicate of git alias with positional parameters (git foo aaa bbb ccc => foo aaa && bar bbb && baz ccc)Smoothen
Agreed with @meagar. If the whole point of doing this is to just rebase all commits since your branch diverged from its parent branch, without having to directly rebase from that branch, which can lead to merge-conflicts that you might not necessarily want to deal with at the moment, see my answer.Provided
O
105

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

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

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:

[alias]
        rb = "!f() { git rebase -i HEAD~$1; }; f"
Ommatidium answered 10/8, 2011 at 3:54 Comment(8)
Thank you! I've add an alias to show files in a commit ($1 == commit hash). shf = "!shf() { git diff-tree --no-commit-id --name-only -r $1; }; shf"Metronome
I'm using 1.7.12.4 on Mac and I never use the sh -c part..."!git rebase -i HEAD~$1;" works fine for me.Garate
I needed put a # to the end: [config] rbi = "!git rebase -i HEAD~$1 #"Uranic
"!f() { git rebase -i HEAD~$1; }; f" syntax works like a charm!Beitch
Are the quotation marks in the first alias really "..."..."..." and not "...'...'..."?Flexed
@Nick yes, considering the inner double quotes are escaped.Ommatidium
You can alias this on ~/.gitconfig with commit-diff = "!sh -c \"git diff $1~ $1\" -". Then use git commit-diff e48ed72acafb25b998321c4faa882f30115bf21a =)Quirinus
@vnbrs Indeed, nice one!Ommatidium
P
5

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 $1 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

Why this, rather than git rebase -i parentBranch

The reason you would do this rather than a straight git rebase -i parentBranch is 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://mcmap.net/q/12525/-how-do-i-use-39-git-rebase-i-39-to-rebase-all-changes-in-a-branch

Provided answered 24/6, 2015 at 20:35 Comment(1)
Interesting variation. +1. More precise than my answer for the use case you mention.Ommatidium
J
2

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

function grb {
  git rebase -i HEAD~$1
}

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

https://github.com/rayning0/zsh_profile/blob/master/.zprofile#L157

Juline answered 7/7, 2018 at 15:8 Comment(2)
Quoting tilde there shouldnt be necessaryAlexandro
this work for me on git bash and seems to be by far the most legible answer hereCertiorari
E
1

@Droogans pointed out in a comment on 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 $1 as the placeholder value representing the argument in the alias. So, to set up an alias so that git rb 8 becomes git rebase -i HEAD~8:

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

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

    f = "!git fetch origin $1:$1"
Evoy answered 16/8, 2019 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.