0

I've been struggling for a while trying to create the following alias (displays the very first commit of the tree's history) in my .gitconfig :

[alias]
    first =  log $(git log --pretty=format:%H|tail -1)

However, I get the following exception:

fatal: ambiguous argument '$(git': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I have been trying several alternative versions of the command, without being able the get it right. I would like to create it manually, inside the .gitconfig file and not by providing the command in the terminal (i.e. git config --global).

Does anybody have any idea of what might go wrong in this one? I'm on my baby-steps in unix/terminal :-)

EDIT I want to be able to have the same results with the alias as when running the command from the terminal. That is, author, SHA, date, commit message and not only SHA.

teobais
  • 2,820
  • 1
  • 24
  • 36

2 Answers2

3

| tail -1 is not a git command so it cannot be used like this. Try:

first = "! git log --pretty=%H | tail -1"

As to the "first" commit, a branch may have multiple first commits considering merging unrelated branches. They are called root commits. --max-parents=0 can list only the root commits. In most cases a branch has only one root commit. Try in the git way:

first = log --pretty=%H --max-parents=0
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Thanks. Unfortunately, though, I'm getting the same result for both of the solutions you provided. Both of them only return the SHA of the specified (first) commit. However, I wanna have a descriptive output (i.e. date, author, commit message), like here: gyazo.com/f30c00074bd2faa13787eaea65492c77 @ElpieKay – teobais Apr 27 '17 at 20:46
  • 1
    @toubou you could add more placeholders like %H to get what you need. For example %an for author name, %cd for commit date, %s for commit subject, %b for commit body, %B equivalent to %s%b. All the placeholders can be found in `git log --help`. Combine them freely to format the output. You can add extra strings in it. `--pretty="hash:%h author:%an subject:%s"` for example. – ElpieKay Apr 28 '17 at 01:24
  • 1
    Thanks for directing me into the correct route! Below is what worked for me, (just for future reference :-) ) – teobais Apr 28 '17 at 09:55
0

Actually, for some reason I had to use single quotes. Credits to @ElpieKay for directing me into the correct route. Below is the solution for what I wanted to achieve!

first =  log --pretty='%C(yellow)commit %H%nDate:\t%ad%nAuthor: %an <%ae>%n%n\t%s' --max-parents=0
teobais
  • 2,820
  • 1
  • 24
  • 36