2

Is it possible to use a variable from .gitconfig in another definition in .gitconfig?

In bash I can do:

delimiter="^"
log_res="log --graph --pretty=format:'$delimiter%cr$delimiter%cs$delimiter%h'"

what results in

log --graph --pretty=format:'^%cr^%cs^%h'

Is the same possible in .gitconfig to achieve the result alias???

[alias]
      result    = log --graph --pretty=format:'^%cr^%cs^%h'
      delimiter = "^"
      log-res   = ??? log --graph --pretty=format:'DELIMITER%crDELIMITER%csDELIMITER%h' ???

How do I have to write 'log-res' that a change of the delimiter is taken over? Could this be done nicely?

I know I can do something like this what is ugly if you define a lot of log commands:

[alias]
      result    = log --graph --pretty=format:'^%cr^%cs^%h'
      delimiter = "^"    
      log-res   = !bash -c '"git $(echo \"log --decorate=short --graph --pretty=format:'$(git config alias.delimiter)%cr$(git config alias.delimiter)%cs$(git config alias.delimiter)%h'\")"'

where git result is the same like git log-res, but this horrible to read, isn't it?

1 Answer 1

1

No, it's not possible to reference other settings in a .gitconfig. In general, this would be tricky, because of the inheritance rules: if the value changed in the repo local config, would an alias in the global config use the version in the global config or the repo local config?

As you've noticed, you can indeed use sh to do this, but it can be a little simpler:

log-res = "!f() { d=\"$(git config alias.delimiter)\"; git log --graph --pretty=\"format:$d%cr$d%cs$d%h\" \"$@\"; };f"

You may want to store the value somewhere other than under the alias section, so that your shell completion doesn't try to complete git delimiter, which wouldn't work.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the right or a better section for delimiter to store in?
Anything you like. You can create any key that you want, including custom ones. You could do something like log.delimiter, or a key named after your initials or handle, or pretty much anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.