HOWTO · Git
How to git add, git commit, and git push in One Command
You can add, commit and push files in one command in Git.
On this page
You can run git add, git commit, and git push from one shortcut. The shortcut still runs these as separate Git operations: it stages selected changes, creates a local commit, then sends that commit to a remote. You can set up either a Bash function or a Git alias and pass a commit message to it.
Before using either shortcut, check which changes are staged and confirm the current branch and remote. Both examples below use origin and push HEAD; the remote must exist, the branch must be accepted, and your credentials must allow the push. A failed push does not undo a commit that was already created locally.
Add, Commit, and Push with One Command
Choose what the shortcut stages before automating the sequence. git add . stages changes under the current directory, including its subdirectories. git add -A stages additions, modifications, and deletions across the working tree. Either command can include work you did not intend to commit, so inspect the staged diff with git diff --cached before running the commit when the scope is uncertain.
The examples use git push origin HEAD to send the current commit to origin. This relies on a configured remote and a branch that the remote accepts. If the push fails, the commit remains local; check the remote URL, branch policy, authentication, and network connection before trying again.
Create a Bash Function
A Bash function is convenient when you want a short command available in your shell. Add the function to ~/.bashrc, then load that file in the current Bash session with source ~/.bashrc. For future sessions, Bash startup behavior depends on how the shell was launched and configured. Git does not read .bashrc; if the function is missing, check whether your Bash session loads that file, or source it directly.
This self-contained example creates a temporary local repository and remote, defines acp, and calls it with a custom message. In your own setup, copy the function definition into ~/.bashrc; the setup commands for the temporary repository are included so the example can run on its own.
set -e
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
git init --bare -q "$tmp_dir/remote.git"
git init -q -b main "$tmp_dir/work"
cd "$tmp_dir/work"
git config user.name "Example User"
git config user.email "example@example.com"
git remote add origin "$tmp_dir/remote.git"
printf 'Initial content\n' > README.md
git add README.md
git commit -qm "Initial commit"
push_output=$(git push -q -u origin HEAD 2>&1) || {
printf '%s\n' "$push_output" >&2
exit 1
}
printf 'Updated content\n' > README.md
acp() {
git add -A && git commit -qm "$1" && {
push_output=$(git push -q origin HEAD 2>&1) || {
printf '%s\n' "$push_output" >&2
return 1
}
}
}
acp "Update README.md"
Here "$1" passes the first argument as the commit message. The function uses git add -A, so it stages changes across the working tree before committing. Review the staged diff first if the repository contains other work you want to keep out of this commit. To use the function after saving it in ~/.bashrc, run source ~/.bashrc once in the current session; then call it with a quoted message, for example acp "Update README.md".
Create a Git Alias
A Git alias works from a repository without relying on a shell startup file. Configure it with git config --global alias.lazygit '!f() { git add -A && git commit -m "$@" && git push origin HEAD; }; f', then invoke it as git lazygit "Update LICENSE.md". Use --global to make the alias available across your repositories, or omit it to save the alias only in the current repository.
This runnable example uses the same temporary local setup as the function example, then defines a repository-scoped alias. The alias chains its operations with &&, so it proceeds to the next operation only when the preceding one succeeds.
set -e
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
git init --bare -q "$tmp_dir/remote.git"
git init -q -b main "$tmp_dir/work"
cd "$tmp_dir/work"
git config user.name "Example User"
git config user.email "example@example.com"
git remote add origin "$tmp_dir/remote.git"
printf 'Initial content\n' > LICENSE.md
git add LICENSE.md
git commit -qm "Initial commit"
push_output=$(git push -q -u origin HEAD 2>&1) || {
printf '%s\n' "$push_output" >&2
exit 1
}
git config alias.lazygit '!f() { git add -A && git commit -qm "$@" && git push -q origin HEAD; }; f'
printf 'Updated content\n' > LICENSE.md
if ! alias_output=$(git lazygit "Update LICENSE.md" 2>&1); then
printf '%s\n' "$alias_output" >&2
exit 1
fi
In the alias, "$@" forwards the arguments after git lazygit; quoting a multiword message keeps it as one commit message. The alias also stages all changes with git add -A, so check git diff --cached before committing when you need to verify its scope. The example stores the alias in this temporary repository; the --global command above stores it in your user Git configuration instead.
Choose and Use the Right Approach
Use a Bash function when you want a shell command that can include shell logic or be adjusted in your Bash configuration. Use a Git alias when you want the shortcut attached to Git and available through git <alias>. In both cases, git add, git commit, and git push remain distinct steps, and a successful commit does not guarantee a successful push.
Before calling either shortcut, confirm that you are in the intended repository and branch, that origin points to the intended remote, and that only the desired changes will be staged. The function and alias here both use git add -A, which stages additions, modifications, and deletions across the worktree. To limit staging to a directory, change the staging command deliberately and review its result; do not assume the shortcut selects only the file named in your commit message.
If the function is unavailable, verify that the active Bash session has loaded ~/.bashrc. If the alias is unavailable, inspect the repository or global Git configuration where you saved it. If a push is rejected, the local commit is still present; resolve the remote or branch issue and push the intended commit using your repository’s normal workflow.