312

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!

7
  • 15
    Have you tried writing a shell script? Commented Oct 25, 2013 at 16:34
  • 1
    stackoverflow.com/questions/7852148/… Commented Oct 25, 2013 at 16:38
  • There are a lot of changes in my code, when you are changing just a padding in css, or one letter and etc. Commit messages and all these commands stuff adds a lot of work. Not tried a shell script. How it can be done? Thanks! Commented Oct 25, 2013 at 16:38
  • 3
    "(do not need commit message either)" images.wikia.com/dragonball/images/a/a7/Facepalm_227785.jpg Commented Oct 25, 2013 at 20:12
  • 2
    if you are doing a commit for every single instance of a change, you are doing it wrong. do the command's when you're feature is finished or bug is solved (this could be a single change) Commented Aug 16, 2016 at 11:50

37 Answers 37

351

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.

Sign up to request clarification or add additional context in comments.

21 Comments

Restart not needed, just do "source .bashrc"
could use $* instead of $1 to save typing the quotes: lazygit My commit msg
@KaiCarver Nice thought, but personally I do not like it. It prevents you from using multiple args, and also goes against the normal convention of a single string arg with spaces being wrapped in quotes. This is second nature to me at this point (and I would assume a lot of developers). Leaving out the quotes feels dirty.
lazygit, best name ever!
@btse me being a diplomatic person + it's friday. Here is the function using @KaiCarver suggestion: function dirtygit() { git add . git commit -a -m "$*" git push }
|
135

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'

8 Comments

That's exactly what I've been looking for! Works perfectly, thanks! I couldn't find a full syntax reference or a detailed list of commands. What does !f() { ... }; fdo exactly? More explanation of that line would make the answer even better.
@DmitriyDemir It defines the function f, then calls it.
Works really well. I added git pull to the start so that merges are reduced.
@Oliver Why is it in quotes? Why is there an exclamation point? Does the f at the end call the function? What is "$@"?
@PhilipRego Wrong Oliver :p
|
92

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

5 Comments

@wlz I just tried it and it works fine. Note that the whole string is inside single-quotes. git automatically added the escapes (checked with git config -e).
Sorry , you're right :) . I directly edit the .gitconfig file . The " has already escaped with \" .
Why isn't this the accepted answer? It does what was asked. +1.
On Windows, I needed to escape the double quotes around $1 to get it to work from PowerShell (using ... \"$1\" ...)
Please update with an answer that works on windows as well
50

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.

2 Comments

I really like this. ...and have therefore adopted it into my own ~/.bash_profile ;) thanks.. +1
Careful on the alias gp. It would conflict with Oh-My-Zsh's git plugin, where gp is used to git push
45

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.

12 Comments

+1 for the explanation about the workflow. However git add . and git commit -a are not the same thing
Thanks for the explanation, but GIT will not collapse if there will be a command like this for those who knows what they are doing...
@Lucas: Experienced programmers will tell you that people who ask for an automatic commit-push command don't know what they're doing. There's a reason for the 3-stage code checkin (if you're using feature branches, 4-stage).
@slebetman: I didn't explain it correctly. With facebook apps, every single change must be deployed to live server even for development. So, we have setup commit hooks to do that. You do a change, commit it and see the change in your app. With git, I need to add, stage then push it to see a single change. Doesn't it look a overkill?
Yes, but you don't need to commit to test your code, either. You should write some code, test it, and then commit and push. The legendary use of local git commits to provide some sort of multi-file "undo" functionality is used far more in legend than in reality, in my experience.
|
43

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

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

3 Comments

If you wish to make this an answer, please add more commentary around how it works. Answers which are simply a code dump are generally not well received. See How to Answer.
I think this should be the correct answer. As it answer the question as succinctly as possible. Just type it into terminal and it works! If you doesn't want to add an alias an alternative would be to simply create a textExpander expand and trigger it via a key-combo.
Works beautifully. I think this should be the accepted answer.
25

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

2 Comments

love it. worth considering the addition of git pull & as the first step as well
git add is not included in the -a, particularly for the untracked files: -a, --all Tell the command to automatically stage files that have been modified and deleted, but new files you have not told git about are not affected.
21

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)

2 Comments

You would need it add that alias to .bashrc or .bash_profile to make it permanent
I think this page can help to explain: serverfault.com/questions/3743/…
11

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.

Comments

9

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).

Comments

8

If you're using a Mac:

  1. Start up Terminal and input cd ~/ to go to your home folder

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

  3. Edit .bash_profile with your favourite 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"

Comments

7

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 ''

Comments

5

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.sstatic.net/2IZDe.jpg

Comments

4

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

2 Comments

This doesn't add anything to any of the previous answers.
I'm sorry, you are right. I think I didn't notice that Tilman Vogel's answer was the same as mine.. Should I delete it??
4

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

Comments

3

I use a batch file:

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

1 Comment

probs would be easier if you accepted in args from the command line. i simply go "lezygit MESSAGE" etc. its pretty cool!
3

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" .

Comments

3

I like to run the following:

git commit -am "message";git push

1 Comment

I think the ; should be replaced with && in your command, since the later will execute the git push command only if there's no error on the previous command(s).
3

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"

Comments

2

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

Comments

2

Hello you have just to add un alias with [nano or vi] ~/.bashrc

echo Enter You commit message && read msg && git add . && git commit -m "$msg" && git push origin main 

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
works nicely. btw: usually git push at the end will be sufficient. And I would put Enter ... message in quotation marks, just for style reasons.
1

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'

Comments

1

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"

Comments

1

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
}

Comments

1

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

Comments

1

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"

Comments

1

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.