How to git add, git commit, and git push in One Command

John Wachira Feb 15, 2024
How to git add, git commit, and git push in One Command

This article discusses two methods you can employ to add, commit and push files to the remote repository in one command. When making small changes in single files, you still need to follow the three-stage process of publishing your changes to the remote repository.

Luckily, you can create a command that adds, commits and pushes your changes to the remote repository.

git add, git commit, and git push in One Command

There are two ways of doing this.

  1. Create a bash function.
  2. Create an alias.

Create a Bash Function

We can create a Bash function that adds, commits, and pushes our local changes to the remote repository. This function should be stored in the .bashrc file.

The .bashrc file is simply a shell script that defines the configuration for a terminal’s session.

This file is usually located in your home directory. A home directory is the directory you are standing at when you launch Git Bash.

Launch Bash and use the command below to create one if you do not have the .bashrc file.

$ touch ~/.bashrc

Like the .gitconfig file, the .bashrc is a hidden file. Run the command below to add the function to your .bashrc file.

$ notepad ~/.bashrc

This command will open the file in Notepad, and you can add the function shown below.

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

The "$1" at git commit will allow you to give a custom commit message when running the acp command.

You can give your function any name. Save the file and run the command below to activate the function.

$ source ~/.bashrc

Note that newer Git versions start with --login. In such cases, Git only reads a bash_profile file.

For this reason, Git will not recognize your .bashrc file. To remedy this, run the command below.

$ if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

Git will now read your .bashrc file. We can use the add, commit, and push changes to the remote in one command using acp, as shown below.

$ acp "Update README.md"

This will add, commit, and push our changes to the remote.

add commit and push in one command

Note: Use the name you assigned to your function. For example, if you named your function Lazygit, you will run:

$ Lazygit "Update README.md"

Create an Alias

As illustrated below, we can create an alias that adds, commits, and pushes changes to the remote repository.

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

You can give your alias any name. Note the "$@" at git commit will allow us to feed a custom commit message when using the alias on the command line.

Since we named our alias lazygit, we will run:

$ git lazygit "Update LICENSE.md"

add commit and push in one command using an alias

You can add, commit, and push in one command by creating a Bash function to your .bashrc file or create an alias. We have seen how you can create both with the option of adding a custom commit message.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Add

Related Article - Git Push

Related Article - Git Commit