How to Add, Commit, and Push in One Git Command

Published Sep 9, 2021

How can we add, commit, and push code changes in a single Git command?

Using a Git Alias

The first option is to use a Git alias.

We can wrap any number of Git commands into a single command using a Git alias.

In our ~/.gitconfig, we can view all available aliases under [alias].

To add an alias, we can either add it directly into that file, or run a git config command like below.

git config --global alias.acp '!f() { git add . && git commit -m "$@" && git push origin HEAD; }; f'

This command does several things.

  1. Defines an alias acp that can be run like so: git acp "commit message"
  2. Creates a function f(), which does the following:
    1. Runs git add .
    2. Runs git commit -m "whatever commit message is passed"
    3. Runs git push origin HEAD, which will push to the current branch
  3. Executes function f

As explained above, we can pass a commit message into our alias.

git acp "commit message"

Using a bash function

We can also add a function to our ~/.bashrc.

function acp() {
    git add .
    git commit -m "$1"
    git push origin HEAD

Once we run a source ~/.bashrc, we should have access to this function.

acp "commit message"

Not a Git alias, but still a nice solution to our problem.

More CLI Articles