0

Basically I want to compare a local file in the current branch against the remote file, but also I would like to know if there is a way to mix Git aliases with Linux commands.

The script I created looks like this:

git show origin/$(git rev-parse --abbrev-ref HEAD):$1 > /tmp/xxx <br/>
git diff /tmp/xxx $1

Is there a way to do that as a Git alias instead of having to create scripts?

Kjuly
  • 34,476
  • 22
  • 104
  • 118
CCC
  • 2,642
  • 7
  • 40
  • 62

3 Answers3

2

An alias will be passed to the shell for execution if the value begins with the ! character. So it is possible to define an alias to do what you want.

[alias]
    diff-remote = "!f() { git show origin/$(git rev-parse --abbrev-ref HEAD):$GIT_PREFIX$1 > /tmp/xxx; git diff /tmp/xxx $GIT_PREFIX$1; }; f"

Aliases are always run from the top directory of the repository, with the path from there to the original directory stored in $GIT_PREFIX. So it is necessary to prepend that to file names in order for an alias to work from other directories within the repository.

But, to do that it is necessary to put the actual code into a function. Otherwise there is no way for that variable to be inserted immediately before the first argument.

So this alias definition defines a shell function (named f) with the desired commands and then immediately runs that. There's no need to cleanup the temporary function because that shell will be exiting right after the function does.

qqx
  • 18,947
  • 4
  • 64
  • 68
1

Do it in the shell, rather than git itself. Either write a script and save it in your path, or write it as a shell function:

function diff-remote()
{
    git show "origin/$(git rev-parse --abbrev-ref HEAD):$1" > /tmp/diff-remote.$$
    git diff /tmp/diff-remote.$$ "$1"
    rm /tmp/diff-remote.$$
}

you can type that on the command line, save it to a file and source it, or add it directly to your .bashrc.

Just type diff-remote myfile to use.

Useless
  • 64,155
  • 6
  • 88
  • 132
0

Since git aliases can't take arguments (e.g. $1), there's probably no way to do this directly with aliases. There are a couple other alternatives, though:

  1. Create a script some_script and set up a git alias that calls that script, or
  2. Name the script git-something, make sure it's in your PATH, and then you can use it as git something, or
  3. Just use the script outside of git.

In this case, however, I think you should be able to git diff origin/master:file master:file. That skips the rev-parse bit, so it won't automatically sort out which remote branch to use, and thus requires a little more mental effort to know what is upstream of your current local branch, but in most cases, that shouldn't be to strenuous...

twalberg
  • 59,951
  • 11
  • 89
  • 84