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

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

  • Git Add and Git Commit in One Command
  • Add Files to a Git Repository
  • Add All Files for Commit Except One File
  • Undo the Git add Command
  • Add All Files in a Folder to Commit in Git
  • Related Article - Git Push

  • Commit and Push a Single File to the Remote
  • Difference Between Git Push Origin and Git Push Origin Master
  • Git Push Origin Head
  • Git Push Hangs
  • Make an Initial Push to a Remote Repository With Git
  • Related Article - Git Commit

  • Get Current Commit in Git
  • Git List Commits
  • Git Add and Git Commit in One Command
  • Commit Current Changes to a Different Branch in Git
  • Exit the Commit Message Editor