Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an argument to a Git alias command

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.

like image 293
HaveF Avatar asked Aug 10 '11 03:08

HaveF


People also ask

How do I make an alias command in git?

It is important to note that there is no direct git alias command. Aliases are created through the use of the git config command and the Git configuration files. As with other configuration values, aliases can be created in a local or global scope.

Where do I put git alias?

Your git aliases are often stored per your user's configuration at ~/. gitconfig . You can also manually set aliases using, for example, the command git config alias. s 'status -s' .

What is alias in git?

2.7 Git Basics - Git Aliases This means that, for example, instead of typing git commit , you just need to type git ci . As you go on using Git, you'll probably use other commands frequently as well; don't hesitate to create new aliases.

How do I edit a git alias?

Start a new branch. gitconfig file and add each alias under [alias], like so: Additionally, you can have repo-specific aliases. Just edit . git/config in the repo where you want to add the alias, and follow the same syntax.


2 Answers

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" 
like image 134
VonC Avatar answered Oct 14 '22 04:10

VonC


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://stackoverflow.com/a/31036645/444610

like image 28
Seth Flowers Avatar answered Oct 14 '22 04:10

Seth Flowers