[git] git add, commit and push commands in one?

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

Sometimes I'm changing only one letter, CSS padding or something. Still, I have to write all three commands to push the changes. There are many projects where I'm only one pusher, so this command would be awesome!

This question is related to git

The answer is


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'

In Linux/Mac, this much practical option should also work

git commit -am "IssueNumberIAmWorkingOn --hit Enter key
> A detail here --Enter
> Another detail here --Enter
> Third line here" && git push --last Enter and it will be there

If you are working on a new branch created locally, change the git push piece with git push -u origin branch_name

If you want to edit your commit message in system editor then

git commit -a && git push 

will open the editor and once you save the message it will also push it.


Write a small script named gitpush.sh with below lines and add it your ~ directory.

echo $1
git add .
git commit -m "$1"
git push

Now add an alias in ~/.bashrc like below :

alias gitpush='~/gitpush'

Now from any git repository just write gitpush "message" .


This is perfect for command grouping.

Grouping Commands

{ list; } Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

legit(){ git add --all; git commit -m "$1"; git push origin master; }
legit 'your commit message here'

Add in ~/.bash_profile for adding, committing and pushing with one command put:

function g() { git commit -a -m "$*"; git push; }

Usage:

g your commit message
g your commit message 'message'

No quotes are needed although you can't use semicolons or parenthesis in your commit messages (single quotes are allowed). If you want to any of these just simply put double quotes in you message, e.g.:

g "your commit message; (message)"

To create a comment in your message do:

g "your commit message:
> your note"

There's also a function for adding and committing in a similar way:

function c() { git add --all; git commit -m "$*"; }

Works exactly the same way that g function and has the same constraints. Just put c instead. E.g.

c your commit message

You can also add an alias for pushing to the remote:

alias p='git push'

Usage:

p

That amounts into 2 letters, c and p you use while working with your git repository. Or you can use g instead to do it all with only one letter.

Full list of aliases and functions: https://gist.github.com/matt360/0c5765d6f0579a5aa74641bc47ae50ac


You can try gitu.

For the first time (node js has to be installed):

npm install -g git-upload

After that:

gitu COMMIT_MSG

To issue those three commands at once.

The good thing is that you don't have to worry when you reinstall your system or when you want to do this on different computers and No file modification is needed. This also work on different platforms (not just Linux and Mac, but also Windows under command prompt like cmd and powershell) just that you have to install npm and nodejs (git of course).


I did this .sh script for command

#!/bin/sh
cd LOCALDIRECTORYNAME/  
git config --global user.email "YOURMAILADDRESS"
git config --global user.name "YOURUSERNAME"
git init
git status
git add -A && git commit -m "MASSAGEFORCOMMITS"
git push origin master

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.


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'


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

git commit -am "your message" && 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!")


I found this yolo alias to be amazing to even submit a random comment to the commit while I am being lazy. It works really well out of the box, so I just do git yolo and all my changes are pushed automatically.


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.


This Result - Try this: Simple script one command for git add, git commit and git push

Open your CMD on Windows and paste this answer

git commit -m "your message" . && git push origin master

This example my picture screenshot : https://i.stack.imgur.com/2IZDe.jpg


define function in .bashrc

function gitall() {
    file=${1:-.}
    comment=${2:-update}
    echo $file
    echo $comment
    git add $file && git commit -m '$comment' && git push origin master
}

in your terminal

gitall

default gitall will add all in current git repo

gitall some-file-to-add 'update file'

will add certain file to change, and use custom commit message


There are plenty of good solutions already, but here's a solution that I find more elegant for the way I want to work:

I put a script in my path called "git-put" that contains:

#!/bin/bash
git commit "$@" && git push -u

That allows me to run:

git put -am"my commit message"

..to add all files, commit them, and push them.

(I also added the "-u" because I like to do this anyway, even though it's not related to this issue. It ensures that the upstream branch is always set up for pulling.)

I like this approach because it also allows to to use "git put" without adding all the files (skip the "-a"), or with any other options I might want to pass to commit. Also, "put" is a short portmanteau of "push" and "commit"


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

For the macOS users:

  1. Open your Terminal or iTerm2 or another terminal that you use.

  2. Move to your User profile folder with command ~/. It's a default folder for .bash_profile file:

User folder example

  1. Type nano .bash_profile This command will open the .bash_profile document (or create it if it doesn’t already exist) in the easiest to use text editor for terminal – nano.

  2. Now you can make a simple change to the file. Paste these lines of code to change your Terminal prompt:

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

Nano text editing

  1. Now save your changes by typing ctrl + o and hit return to save. Then exit nano by typing ctrl + x.

  2. Now we need to activate your changes. Type source .bash_profile (or . ~/.bash_profile) and watch your prompt change.

  3. In iTerm2 Preferences/Profiles/General/Command set to Login Shell and Send text at start to source ~/.bash_profile. So you don't need to make it manually after each macOS restart. iTerm 2 preferences

Credentials: https://natelandau.com/my-mac-osx-bash_profile


If you use VSCode, you can download this extension which will let you do it in one simple keyboard shortcut.


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


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)


Since the question doesn't specify which shell, here's the eshell version based on the earlier answers. This goes in the eshell alias file, which might be in ~/.emacs.d/eshell/alias I've added the first part z https://github.com/rupa/z/ which let's you quickly cd to a directory, so that this can be run no matter what your current directory is.

alias census z cens; git add .; git commit -m "fast"; git push

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
}

If you're using fish shell (building off of btse's answer):

Save this file within '~/.config/fish/functions' as 'quickgit.fish'. Create the directory if it does not exist. '--git-dir=$PWD/.git' Ensures that we run the git commands against the git project where we called the function

function quickgit # This is the function name and command we call
    git --git-dir=$PWD/.git add . # Stage all unstaged files
    git --git-dir=$PWD/.git commit -a -m $argv # Commit files with the given argument as the commit message
    git --git-dir=$PWD/.git push # Push to remote
end

Restart terminal, navigate to project, make changes, and now you can call 'quickgit "example message"'. Changes will now be added, committed, and push :).

Also can be found as a Gist here: https://gist.github.com/Abushawish/3440a6087c212bd67ce1e93f8d283a69


I use a batch file:

@ECHO OFF
SET /p comment=Comment:
git add *
git commit -a -m "%comment%"
git push

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.


If the file is already being tracked then you do not need to run git add, you can simply write git commit -am 'your message'

If you do not want to write a commit message you might consider doing something like

git commit --allow-empty-message -am ''


I like to run the following:

git commit -am "message";git push

When you want svn-like behavior of git commit, use this in your git aliases in your .gitconfig

commit = "!f() { git commit \"$@\" && git push; };f"


If you're using a Mac:

  1. Start up Terminal Type "cd ~/" to go to your home folder

  2. Type "touch .bash_profile" to create your new file.

  3. Edit .bash_profile with your favorite editor (or you can just type "open -e .bash_profile" to open it in TextEdit).

  4. Copy & Paste the below into the file:

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

After this, restart your terminal and simply add, commit and push in one easy command, example:

lazygit "This is my commit message"

As mentioned in this answer, you can create a git alias and assign a set of commands for it. In this case, it would be:

git config --global alias.add-com-push '!git add . && git commit -a -m "commit" && git push'

and use it with

git add-com-push

Please see my answer,I added everything into a single line

alias gitcomm="echo 'Please enter commit message';read MSG ;git add --all;git commit -am=$MSG;git push"