Git add and commit in one command

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

You can use git aliases, e.g.

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

and use it with

git add-commit -m 'My commit message'

EDIT: Reverted back to ticks ('), as otherwise it will fail for shell expansion on Linux. On Windows, one should use double-quotes (") instead (pointed out in the comments, did not verify).

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

The most simply you can do it is :

git commit -am "Your commit message"

I dont understand why are we making this tricky.

Solution 5 - Git

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"

Solution 6 - Git

Just combine your commands:

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

Solution 7 - Git

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"

Solution 8 - Git

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 .

Solution 9 - Git

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

Solution 10 - Git

pretty sure you can use:

git commit -am "commit all the things"

Solution 11 - Git

Just use:

git commit -m "message" .     

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

Solution 12 - Git

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

Solution 13 - Git

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'

Solution 14 - Git

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

Solution 15 - Git

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.

Solution 16 - Git

  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)

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

Solution 17 - Git

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

Solution 18 - Git

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.

Solution 19 - Git

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'

Solution 20 - Git

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"

Solution 21 - Git

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.

Solution 22 - Git

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

Solution 23 - Git

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"

Solution 24 - Git

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

Solution 25 - Git

  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.

Solution 26 - Git

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

Solution 27 - Git

To keep it in one line use:

gacm "your comment"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChetanView Question on Stackoverflow
Solution 1 - GitMartin C.View Answer on Stackoverflow
Solution 2 - GitJed SchneiderView Answer on Stackoverflow
Solution 3 - GitAlesView Answer on Stackoverflow
Solution 4 - GitAnurag KumarView Answer on Stackoverflow
Solution 5 - GitdaniloView Answer on Stackoverflow
Solution 6 - GitYarinView Answer on Stackoverflow
Solution 7 - GitsirwinnyView Answer on Stackoverflow
Solution 8 - GitSojan JoseView Answer on Stackoverflow
Solution 9 - GitdhahnView Answer on Stackoverflow
Solution 10 - GitBlair AndersonView Answer on Stackoverflow
Solution 11 - GitJosh OlsonView Answer on Stackoverflow
Solution 12 - GitLenar HoytView Answer on Stackoverflow
Solution 13 - GitPositive NavidView Answer on Stackoverflow
Solution 14 - Gitademar111190View Answer on Stackoverflow
Solution 15 - GitDruta RuslanView Answer on Stackoverflow
Solution 16 - GitH. AlmidanView Answer on Stackoverflow
Solution 17 - GitavjaarsveldView Answer on Stackoverflow
Solution 18 - GitGusView Answer on Stackoverflow
Solution 19 - GitCruz JuradoView Answer on Stackoverflow
Solution 20 - GitBruno Negrão ZicaView Answer on Stackoverflow
Solution 21 - GitJohnView Answer on Stackoverflow
Solution 22 - GitLuisEnMarroquinView Answer on Stackoverflow
Solution 23 - GitAlejandro SilvaView Answer on Stackoverflow
Solution 24 - GitGaurav RathiView Answer on Stackoverflow
Solution 25 - GitKosmos NagiosView Answer on Stackoverflow
Solution 26 - GitAnang HanafiView Answer on Stackoverflow
Solution 27 - GitSathish RavulaView Answer on Stackoverflow