0

Here are my current aliases in .gitconfig, and they work great. However, I'd like to have a single alias that can do all three.

Example of what I'd like at terminal:

git x my_commit_message

Psuedo-code of .gitconfig:

[alias]
         x = add -A <then do> commit -m <use variable from command line> push

I have push set to default = current, so push alone works.

[push]
    default = current

Any help is appreciated, thanks!

1

2 Answers 2

2

If you want to combine add, commit and push, you'll need a bash function. Git add and commit can be combined with git -am "msg", but the push can only be done as an additional command. So, just define a bash function like this:

gacp() {
  git add -A &&
  git commit -m "${1?'Missing commit message'}" &&
  git push
}

This works by doing the git add -A first, and if it succeeds, then the command git commit -m is executed, with the required message, and if that succeeds, then the git push is executed.

It's important to make the latter commands depend on successful execution of the previous commands in order to avoid downstream messes. In other words, you don't really want to commit changes unless the add succeeded, and you don't really want to push your most recent commits unless the commit succeeded.

You use it like this:

gacp "Latest changes"
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use shell function to be able to execute multiple commands inside a git alias.

  1. First, start with ! so Git will run it as a shell command.
  2. Write your function, like: f() { git add -A && git commit -m "$1" && git push }.
  3. Execute your function just after its declaration.

You should write something like:

[alias]
        x = "!f() { git add -A && git commit -m \"$1\" && git push } f"

Note that:

  • $1 will be replaced by your variable from command line,
  • && will execute the next command only if the previous one has succeed.

1 Comment

You're missing some semicolons: x = "!f() { git add -A && git commit -m \"$1\" && git push; }; f"

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.