Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does running command as git alias gives different results?

Tags:

git

I have the following 1-liner that I use to see who might be a good candidate for help with a peice of code:

git log --pretty=short . | grep ^Auth | sort | uniq -c | sort -nr

which lists authors in order of commits, it's crude but it works OK.

When I add it to my git config however, like this:

[alias]
    guru=!git log --pretty=short . | grep ^Auth | sort | uniq -c | sort -nr

running

git guru

Gives different results to running it from the command line.

stuart@beavis(rp):~/git/apps$ git log --pretty=short . | grep ^Auth | sort | uniq -c | sort -nr
710 Author: dave <dave@b2368a2b-315f-46b9-a0b0-05934f827f41>
415 Author: pete <pete@b2368a2b-315f-46b9-a0b0-05934f827f41>
402 Author: craig <craig@b2368a2b-315f-46b9-a0b0-05934f827f41>

Compared to:

stuart@beavis(rp):~/git/apps$ git guru
859 Author: craig <craig@b2368a2b-315f-46b9-a0b0-05934f827f41>
813 Author: paul <paul@b2368a2b-315f-46b9-a0b0-05934f827f41>
798 Author: dave <dave@b2368a2b-315f-46b9-a0b0-05934f827f41>

As Stefan Näwe notes below, aliases run in the root of your repository, is there any way to run the command on the directory I'm in, or specify?

like image 378
Stuart Grimshaw Avatar asked Jan 12 '12 09:01

Stuart Grimshaw


People also ask

Should you create new Git alias?

As you go on using Git, you’ll probably use other commands frequently as well; don’t hesitate to create new aliases. This technique can also be very useful in creating commands that you think should exist. For example, to correct the usability problem you encountered with unstaging a file, you can add your own unstage alias to Git:

What does the git config command do?

The git config command is actually a helper utility for writing to the global and local Git config files. Invoking this command will update the underlying global config file just as it had been edited in our previous example. Git aliases are a powerful workflow tool that create shortcuts to frequently used Git commands.

Can git diff detect when a file is renamed?

This will allow git diff to detect when a file was renamed. If you rename a file and don’t use -M, git diff will show one file as deleted and another one as newly added, as opposed to showing it as a rename. Note that this option does not work 100% of the time, because git can’t know with certainty that a file was renamed.

Why use GCO instead of Co in git config?

The reason is simple, it is much easier and faster to type gco than git co, which makes git usage more enjoyable. Since (almost) all of our aliases will be prefixed with g (such as ga, gco, gc, …) they will be just as easy to discover if you ever forget them as their git config alias counterpart.


2 Answers

is there any way to run the command on the directory I'm in, or specify?

Yes, there is. When you run a git command, your environment gets a GIT_PREFIX variable with the value of the relative path (from toplevel to the path before executing git) so what you need is prefix your git aliases (that run shell commands and need the right pwd) with cd ${GIT_PREFIX:-.} &&.

Simple test, add this to your ~/.gitconfig

[alias] p = !pwd && cd ${GIT_PREFIX:-.} && pwd

like image 78
Andrei Neculau Avatar answered Oct 05 '22 03:10

Andrei Neculau


git's aliases using a shell command (i.e. prefixed by !) run in the toplevel of the working directory (where the .git lives). Simple aliases (without !) run at the current directory.

like image 39
Stefan Näwe Avatar answered Oct 05 '22 04:10

Stefan Näwe