Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git add, commit and push commands in one?

Tags:

git

Building off of @Gavin's answer:

Making lazygit a function instead of an alias allows you to pass it an argument. I have added the following to my .bashrc (or .bash_profile if Mac):

function lazygit() {
    git add .
    git commit -a -m "$1"
    git push
}

This allows you to provide a commit message, such as

lazygit "My commit msg"

You could of course beef this up even more by accepting even more arguments, such as which remote place to push to, or which branch.


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'

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!")


I use this in my .bash_profile

gitpush() {
    git add .
    git commit -m "$*"
    git push
}
alias gp=gitpush

It executes like

gp A really long commit message

Don't forget to run source ~/.bash_profile after saving the alias.


I think you might misunderstand the workflow that git was designed for. (To clarify/correct what I meant in the comment, you don't need the git add ., since commit -a usually serves the same purpose - adding any changes that have not yet been staged, if the files have already been added)

Typically you'll do something like this:

# make some changes
$ git commit -a -m "Changed something"
# make some more changes
$ git commit -a -m "Changed something else"

wash, rinse, repeat, until you've finished feature X, or you're at a stopping point, or you just want other people to see what you've done. Then you do

$ git push

Git is not SVN - but it appears that you're trying to use it as such. You might find some of the resources at the end of the article here to be of some use.


You can use bash script , set alias to launch any command or group of commands

git commit -am "your message" && git push 

Set as an alias in bash:

$ alias lazygit="git add .; git commit -a -m '...'; git push;";

Call it:

$ lazygit

(To make this alias permanent, you'd have to include it in your .bashrc or .bash_profile)