Git Add and Git Commit in One Command

Git Add and Git Commit in One Command

This article discusses combining the git add and git commit commands into one on the command line. Combining the two commands into one command can save you time.

When combining the two commands, you must remember what you want to be committed to. Let’s look at some common scenarios.

Git Add and Git Commit in One Command

If you are well versed in Git version control, you know that a working space has different categories of files. These are:

  1. Modified files
  2. Deleted Files
  3. Untracked files

Here is an example.

git working space

Assuming we only want to commit the modified and deleted files, how do we add and commit in one command?

We can run the command below to add the modified and commit the deleted files.

$ git commit -am "Shortcut"

Ideally, the command above will stage the files for commit and commit them automatically.

It should leave the untracked file as it is. Let’s confirm our case.

git add and commit

Let’s look at another example.

working space 2

What if we want to add and commit all the files in our working space that include the untracked files?

We know that to add all the files on the index, we use the git add command with the -A flag. We can combine the command with the git commit command by creating a git alias.

The alias will allow us to run the two commands into one while giving a custom commit message. Here is how you can create an alias in Git.

$ git config --global alias.combo-AC '!git add -A && git commit'

You can give your alias any name you desire. In our case, we have called it combo-AC.

Here is an illustration of its usage.

$ git combo-AC -m "Shortcut2"

This command should add and commit everything to our working space. Let’s confirm our case.

git add and commit alias

Alternatively, we can create a function that adds and commits all the files in our working space. We will need to add the function to our .bashrc file to do this.

A .bashrc file is a shell script that dictates the functionality of a terminal session.

This file is hidden and is located in your home directory. This is usually the first directory when you launch Git Bash.

To add the function to the .bashrc file, we will run the following:

$ notepad ~/.bashrc

This will open the file in Notepad, and we can add a function, as illustrated below.

function ac() {
	git add -A
	git commit -m "$1"
}

Save the file and exit Notepad. On Git Bash, 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

Make sure you run the command above in the root folder of your project. Here is an illustration of the function’s usage.

$ ac "New"

This will add and commit all files in our index.

function ac

In a nutshell, when combining the git add and git commit commands into one, consider what you want to commit.

We have discussed combining the two when you only want to commit deleted and modified files. We have also discussed two methods to add and commit all files in one command.

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

  • Add Files to a Git Repository
  • git add, git commit, and git push in One Command
  • 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 Commit

  • Get Current Commit in Git
  • Git List Commits
  • git add, git commit, and git push in One Command
  • Commit Current Changes to a Different Branch in Git
  • Exit the Commit Message Editor