How to Add, Commit, and Push in One Git Command
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.
- Defines an alias
acpthat can be run like so:git acp "commit message" - Creates a function
f(), which does the following:- Runs
git add . - Runs
git commit -m "whatever commit message is passed" - Runs
git push origin HEAD, which will push to the current branch
- Runs
- 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.
Unset an alias
To unset an alias, we can use the --unset flag.
git config --global --unset alias.alias_name