Is there any way to use these three commands in one?
git add .
git commit -a -m \"commit\" (do not need commit message either)
git push
Som
For MAC VSC users the best setup is:
1) press 'shift+cmd+P' and type:
Shell Command: install 'code' command in PATH
Press ENTER (this will install code command to get to the bash_profile easily)
2 ) you can now run: code ~/.bash_profile
to open the empty bash_profile
3) enter a new function in there:
function lazygit() {
git add .
git commit -m "$*"
git push
}
4) now restart VSC
5) make a change, save it and type lazygit message
to run the three commands concurrently
There are some issues with the scripts above:
shift "removes" the parameter $1, otherwise, "push" will read it and "misunderstand it".
My tip :
git config --global alias.acpp '!git add -A && branchatu="$(git symbolic-ref HEAD 2>/dev/null)" && branchatu=${branchatu##refs/heads/} && git commit -m "$1" && shift && git pull -u origin $branchatu && git push -u origin $branchatu'
Simpliest solution would be to:
git commit -a -m "commit" && git push
git add
is already contained in -a parameter of commit, but if you want you can connect them all:
git add . && git commit -a -m "commit" && git push
While I agree with Wayne Werner on his doubts, this is technically an option:
git config alias.acp '! git commit -a -m "commit" && git push'
Which defines an alias that runs commit
and push
. Use it as git acp
. Please be aware that such "shell" aliases are always run from the root of your git repository.
Another option might be to write a post-commit hook that does the push.
Oh, by the way, you indeed can pass arguments to shell aliases. If you want to pass a custom commit message, instead use:
git config alias.acp '! acp() { git commit -a -m "$1" && git push ; } ; acp'
(Of course, now, you will need to give a commit message: git acp "My message goes here!"
)
Building off the lazygit answer, the following solution adds a user check to verify the changes before pushing. It will revert the commands if cancelled. And all that will happen if and only if there are changes in the local repo.
### SAFER LAZY GIT
function lazygit() {
git add .
if git commit -a -m "$1"; then
read -r -p "Are you sure you want to push these changes? [y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
git push
;;
*)
git reset HEAD~1 --soft
echo "Reverted changes."
;;
esac
fi
}
I ended up adding an alias to my .gitconfig
file:
[alias]
cmp = "!f() { git add -A && git commit -m \"$@\" && git push; }; f"
Usage: git cmp "Long commit message goes here"
Adds all files, then uses the comment for the commit message and pushes it up to origin.
I think it's a better solution because you have control over what the commit message is.
The alias can be also defined from command line, this adds it to your .gitconfig
:
git config --global alias.cmp '!f() { git add -A && git commit -m "$@" && git push; }; f'