1

I would like to use git as automated folder history. My flow is more or less like below:

  1. Make change in some files
  2. Git commit
  3. Run my command (for example vagrant up or vagrant provision)

The ideas which are coming to my mind is: 1. Create an alias for my command (in bash or powershell depending on OS) 2. In alias always run git add . and git commit -m "txt" and after above run "real" command

But above idea looks quite ugly (for example "txt" is always static and doesn't describe anything). Does anybody see something better?

1 Answer 1

1

It is possible to define bash functions to create more advanced aliases.

In your case it could look like this:

[alias]
    ...
    my_alias = "!f() { git add "./$GIT_PREFIX"; git commit -m '$1'; vagrant up; }; f"

The ! tells git to execute the alias on the bash (non-git alias)

git my_alias "my commit message"

This will add all files in the current directory and it's subdirectory, commit them with the specified message and start vagrant afterwards.

On the other hand such a non-git alias will always be executed in the root directory of the repository, which means that using git add . would result in the whole repository being added to the index.

At the moment I don't see an easy way to work around this restriction, so you will probably have to add the files in question manually in before.

EDIT Etan mentioned the existance of the GIT_PREFIX variable for exactly this purpose. I edited the answer accordingly.

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

3 Comments

This is what the variable GIT_PREFIX is for in context of aliased shell commands... that being said there's effectively no reason to make this a git alias as opposed to a shell alias which doesn't need to use GIT_PREFIX and can just operate on the current directory.
@EtanReisner good to know about GIT_PREFIX. Creating a git alias instead of a shell alias makes it platform independent and makes it for example easily available in Powershell or CMD. I think that's an advantage.
Saying that makes it "platform independent" is a bit of a stretch, but yes, it does mean it is available from non-shell command environments as long as the default shell git tries to use to execute that is available.

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.