[git] Git add and commit in one command

Is there any way I can do

git add -A
git commit -m "commit message"

in one command?

I seem to be doing those two commands a lot, and if Git had an option like git commit -Am "commit message", it would make life that much more convenient.

git commit has the -a modifier, but it doesn't quite do the same as doing git add -A before committing. git add -A adds newly created files, but git commit -am does not. What does?

This question is related to git

The answer is


I use this git alias:

git config --global alias.cam '!git commit -a -m '

So, instead of call

git add -A && git commit -m "this is a great commit"

I just do:

git cam "this is a great commit"


  1. type git add . && git commit -am and enter
  2. type historyand enter
  3. remember the number for the above command, say the number is 543
  4. From now on, for every commit type !543 "your comment here" i.e. exclamatory mark+number and a space and your comment.

I do a shell

#!/bin/sh

clear

git add -A 
git commit -a -m "'$*'"

save for example git.sh and later call:

sh git.sh your commit message

I use the following alias for add all and commit:

git config --global alias.ac '!git add -A && git commit -a'

Then, by typing:

git ac

I get a vim window to get more editing tools for my commit message.


You ca use -a

git commit -h

...
Commit contents options
    -a, -all    commit all changed files
...

git commit -a # It will add all files and also will open your default text editor.

  1. Create an alias in bash: alias gac="git add -A && git commit -m"

    (I chose to call the shortcut 'gac' but you don't have to)

  2. Use it: gac 'your commit message here'


For the silver backs in the crowd that are used to things like Subversion... to do a "commit" in the old sense of the word (i.e. -- or in git speak -- to add, commit, and push) in a single step I generally add something like the following in the root of my project (as a bat file in windows e.g. git-commit.bat). Then when I want to add, commit, and push I just type something like git-commit "Added some new stuff" and it all goes to the remote repo.

Also, this way anyone on the project can use the same with out having to change anything locally.

I usually also run git config credential.helper store once so I don't need to give uid/pwd when this is run.

::
:: add, commit, and push to git
::

@echo off
echo.
echo.
echo 
echo Doing commit...
git add -A && git commit -m %1
echo.
echo.
echo Doing push...
git push
echo.
echo.
echo Done.
echo.

To keep it in one line use:

git add . && git commit -am "comment"

This line will add and commit all changed and added files to repository.


In case someone would like to "add and commit" for a single file, which was my case, I created the script bellow to do just that:

#!/bin/bash

function usage {
    echo "Usage: $(basename $0) <filename> <commit_message>"    
}

function die {
    declare MSG="$@"
    echo -e "$0: Error: $MSG">&2
    exit 1
}

(( "$#" == 2 )) || die "Wrong arguments.\n\n$(usage)"

FILE=$1
COMMIT_MESSAGE=$2

[ -f $FILE ] || die "File $FILE does not exist"

echo -n adding $FILE to git...
git add $FILE || die "git add $FILE has failed."
echo done

echo "commiting $file to git..."
git commit -m "$COMMIT_MESSAGE" || die "git commit has failed."

exit 0

I named it "gitfile.sh" and added it to my $PATH. Then I can run git add and commit for a single file in one command:

gitfile.sh /path/to/file "MY COMMIT MESSAGE"

Just combine your commands:

git add -A && git commit -m "comment" 

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


To keep it in one line use:

gacm "your comment"

Only adapting the Ales's answer and the courtsimas's comment for linux bash:

To keep it in one line use:

git commit -am "comment"

This line will add and commit all changed to repository.

Just make sure there aren't new files that git hasn't picked up yet. otherwise you'll need to use:

git add . ; git commit -am "message"


I hope this helps someone and please feel free to edit or improve. I'm not sure what the fastest way is but this certainly simplifies my code commit process by using "ohmyzsh" for Git.

https://ohmyz.sh/

  • git add . is shortened to ga .
  • git commit -m "message" is shortened to gc -m "message"
  • git push is shortened to gp
  • git fetch is shortened to gf
  • git pull origin master is shortened to ggl master
  • git push origin master is shortened to ggp master
  • git checkout -b is shortened to gcb
  • git merge is shortened to gm
  • git remote is shortened to gr
  • git status is shortened to gst

enter image description here


Some are saying that git commit -am will do the trick. This won't work because it can only commit changes on tracked files, but it doesn't add new files. Source.

After some research I figured that there is no such command to do that, but you can write a script on your .bashrc or .bash_profile depending on your OS.

I will share the one I use:

function gac {
  if [[ $# -eq 0 ]]
    then git add . && git commit
  else
    git add . && git commit -m "$*"
  fi
}

With this all your changes will be added and committed, you can just type gac and you will be prompted to write the commit message

Or you can type your commit message directly gac Hello world, all your changes will be added and your commit message will be Hello world, note that "" are not used


The most simply you can do it is :

git commit -am "Your commit message"

I dont understand why are we making this tricky.


Just use:

git commit -m "message" .     

Notice the "." at the end... which can also be a path to a file/directory


Check first what aliases you have...

git config --get-regexp alias

If it's not there you can create your own (reference: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)

// git add

git config --global alias.a '!git add -A'

// git commit

git config --global alias.c '!git commit'

// git commit -m

git config --global alias.cm '!git commit -m'

// git add commit

git config --global alias.ac '!git add -A && git commit'

// git add commit -m

git config --global alias.acm '!git add -A && git commit -m'

For instance, if you use the last one...

git acm 'My commit'

pretty sure you can use:

git commit -am "commit all the things"

I use the following (both are work in progress, so I'll try to remember to update this):

# Add All and Commit
  aac = !echo "Enter commit message:" && read MSG && echo "" && echo "Status before chagnes:" && echo "======================" && git status && echo "" && echo "Adding all..." && echo "=============" && git add . && echo "" && echo "Committing..." && echo "=============" && git commit -m \"$MSG\" && echo "" && echo "New status:" && echo "===========" && git status

# Add All and Commit with bumpted Version number
  aacv = !echo "Status before chagnes:" && echo "======================" && git status && echo "" && echo "Adding all..." && echo "=============" && git add . && echo "" && echo "Committing..." && echo "=============" && git commit -m \"Bumped to version $(head -n 1 VERSION)\" && echo "" && echo "New status:" && echo "===========" && git status

With the echo "Enter commit message:" && read MSG part inspired by Sojan V Jose

I'd love to get an if else statement in there so I can get aacv to ask me if I want to deploy when it's done and do that for me if I type 'y', but I guess I should put that in my .zshrc file


I have this function in my .bash_profile or .profile or .zprofile or whatever gets sourced in login shells:

function gac () {
  # Usage: gac [files] [message]
  # gac (git add commit) stages files specified by the first argument
  # and commits the changes with a message specified by the second argument.
  # Using quotes one can add multiple files at once: gac "file1 file2" "Message".
  git add $1 && git commit -m "$2"
}

If you type:

git config --global alias.a '!git add -A && git commit -m'

once, you will just need to type

git a

every time:

git a 'your comment'

In the later version of git you can add and commit like this

git commit -a -m "commit message"

Additionally you an alias:

[alias]
    ac = commit -a -m

Then you can use it like this:

git ac "commit message"

git commit -am "message"

is an easy way to tell git to delete files you have deleted, but I generally don't recommend such catch-all workflows. Git commits should in best practice be fairly atomic and only affect a few files.

git add .
git commit -m "message"

is an easy way to add all files new or modified. Also, the catch-all qualification applies. The above commands will not delete files deleted without the git rm command.

git add app
git commit -m "message"

is an easy way to add all files to the index from a single dir, in this case the app dir.


This works for me always please run following commands:

1.git add .
2.git commit -m "no bugs please"
3.git push origin *

where * is based off the branch you are pushing to, and also commit messages can always be changed to suit the context.


On my windows machine I have set up this .bashrc alias to make the entire process more simple.

  • create / locate your .bashrc - refer SO thread
  • add the following line to file

    alias gacp='echo "enter commit message : " && read MSG && git add . && git commit -m "$MSG" && git push'
    

    it does git add commit and push . tweak it in any manner, say you don't want the push command remove that part

  • reload .bashrc / close and reopen your shell

  • now you can do the entire process with gacp command .

you can use git commit -am "[comment]" // best solution or git add . && git commit -m "[comment]"