Git add and commit in one command
Asked Answered
F

25

591

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?

Flareup answered 28/11, 2010 at 20:40 Comment(4)
possible duplicate of Git Commit all the files using single cmdGavage
time saving and commit every change is better, so i am using git commit -am "yes the code in committed"Chassepot
git add . is a quicker option for adding allVelez
git add -A and even worse, git add . are both harmful and shouldn't be used in the vast majority of scenarios. You want git add -p or git add -i to actually consider what you're staging.Decal
L
555

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

Lallation answered 28/11, 2010 at 21:23 Comment(24)
@joedevon The alias is either in $PROJECT/.git/config or in $HOME/.gitconfig in case of the --global flag.Lallation
Use git config --global --unset alias.<your_alias> to unset the alias.Factional
@MrFusion It makes the command an external shell command instead of a shortcut to in internal git command, i.e. making the whole line being executed as a separate process.Lallation
@MartinC. is there any reason not to alias it as "addcom" instead of "add-commit", just to avoid having to reach up into the number row to type the hyphen, and eliminate one pair of double letters? I get myself in trouble typing doubles; sometimes my finger doesn't lift far enough and I only get one "m" where I wanted two (in the case of "commit")Disadvantageous
@DoktorJ of course you can alias it any way you like.Lallation
Brilliant! I'd done it the hard way, made a bashscript ~/git-commit.sh that takes in the message as a parameter: #!/bin/bash git add --all && git commit -m $1 Endamoeba
I prefer git config --global alias.add-commit '!git add -A && git commit -m' so I don't have to type -m every time.Reluct
Forgot to add the git prefix again before the commit!Digamy
Why do people use "-A" after add? Wouldn't a "." be better and shorter to write?Cheloid
@Cheloid Just using "." won't stage removed files for deletion from the index (it will just stage new and changed files). Using -A does.Lallation
@MartinC. I'm confused. So let's say I add, commit, and the push all of my files and then decide to delete 1 file. So you're saying that if I did "." then the removed file in my GitHub repo would NOT disappear, but "-A" would?Cheloid
@Cheloid I might be wrong, it's been some time. But there is/was a subtle difference between "." and "-A". And if I read the man-page correctly, it could be that the difference between "." and "-A" vanished since the git-version this answer was written against. Also the behavior is differently if you are not in the root of your repository.Lallation
I'm too lazy to type 'add-commit' and '-m' every time. Consequently, I type: git config --global alias.ac '!git add -A && git commit -m' once and every time I just type: git ac 'comment' :)Laidlaw
@BlairAnderson You can, but it has a little bit of a different semantics, especially regarding newly created files.Lallation
git config --global alias.add-commit "!git add -A && git commit" (i.e. double quotes instead of single quotes) is what works on Windows.Genovese
Double quotes fails on UNIX; you should add that to your answer. UNIX tried to expand the ! via history expansion, similar to variable expansion with $.Suribachi
@LukeDavis Thanks for the hint, the initial version had ticks, but changed for windows... will point that out explicitly.Lallation
For me I prefer using git ac, therefore I go for: git config --global alias.ac '!git add -A && git commit'Endamoeba
time saving and commit every change is better, so i am using git commit -am "yes the code in committed"Chassepot
git commit -am "..." does not add untracked / new filesSkewbald
alias: git config --global alias.up '!git add -A && git commit -m $(read -p "Enter the commit: " m; echo $m) && git push'Xl
What does the ! do in this case?Sinfonietta
It looks like the preceding ! makes the alias run as though it was a regular shell command being run at the root of the git repo. I just wrote about that here.Sinfonietta
@GabrielStaples Yes, the ! is a trigger for running it as an external program. See also git-scm.com/book/en/v2/Git-Basics-Git-AliasesLallation
N
417
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.

Neona answered 28/11, 2010 at 20:45 Comment(3)
or just git commit -am "message" - just make sure there aren't new files that git hasn't picked up yet. otherwise you'll need to use git add . && in front of that.... to do it in one line.Maurey
Sorry but the original question was exactly about this - avoiding git add . in case files were addedFoetor
From the git help: -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. Mendiola
O
93

The most simply you can do it is :

git commit -am "Your commit message"

I dont understand why are we making this tricky.

Oden answered 8/8, 2020 at 4:54 Comment(5)
Solution already given in Jed Schneider's answer: https://mcmap.net/q/13692/-git-add-and-commit-in-one-commandDrews
Ohh, the length confused me but was worth reading. Thanks @EricAyaOden
This doesn't add new files. The a is for all, not for add.Dolli
Hi @tymtam this command adds all tracked files to staging area and commits with given message. Untracked files are added to staging area, with command git add -A.Oden
But this command will fail incase you have created another file . It will only work once you have pushed that file to repository.Cartilaginous
P
69

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.

Plascencia answered 5/12, 2011 at 1:54 Comment(5)
Should be noted that this is specific to a Linux shell (bash and possibly others).Welby
git commit -am "comment" is perfectWarman
If you used: git add . Then you don't need to use option -a, just need: git commit -m "comment"Anachronous
. will only add files in the current directory. You should do --all insteadEmpty
git commit -am "comment" does not seem to work in linuxGlider
O
34

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"

For Windows:
git add . && git commit -am "comment"

Ogburn answered 2/11, 2017 at 15:22 Comment(3)
How to do git commit -am "comment", but for 1 file only? Let's say file.txt.Ensure
git commit -am "comment" is a shortcut for adding and committing all changed files. If you want to add and commit only 1 file than you'll need to execute: git add file.txt ; git commit -m "comment"Ogburn
In second case, there is no need to add -a with m. It should be git add . ; Git commit -m "message"Collective
A
23

Just combine your commands:

git add -A && git commit -m "comment" 
Abscise answered 10/9, 2012 at 15:46 Comment(0)
R
17

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"
Roderich answered 14/10, 2016 at 15:25 Comment(1)
This command will not commit the new files, only the updated ones.Rascal
H
17

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

Hern answered 21/10, 2019 at 8:32 Comment(1)
User note: this is not technically restricted to git ... this answer applies to the behavior of a general-purpose tool that allows typing commands using predictive "fuzzy search" style data entry, which applies to all commands entered on the commandline, not just git commands.Durr
S
15

pretty sure you can use:

git commit -am "commit all the things"
Syllabism answered 20/6, 2017 at 16:57 Comment(4)
This command is the shortest and probably the easiest way to achieve the required result. Test on git version 2.6.3.windows.1.Groff
This wont commit newly added files though, only the "updated" ones!Haskel
CORRECT it will not start tracking files that are un-trackedSyllabism
@BlairAnderson - Is there a command to add un-tracked files also with add and commit ?Arsenious
V
14

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 .
Vessel answered 2/10, 2014 at 14:7 Comment(1)
FYI that script has a git push in it. Don't copy and paste that.Figge
A
12

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"
}
Anatol answered 10/8, 2017 at 11:27 Comment(0)
S
9

Just use:

git commit -m "message" .     

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

Somehow answered 9/10, 2018 at 16:40 Comment(2)
will this work for newly created files or just existing ones which are changed ?Arsenious
This works only for already tracked files.Vilhelmina
L
8

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'
Laidlaw answered 3/10, 2016 at 12:16 Comment(1)
this should be the top answer, super easy and includes UNTRACKED files unlike most of the other suggestions.Resa
P
4

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
Pigg answered 13/1, 2012 at 22:4 Comment(2)
not portable to other OSs, and git can do this itself, so what's the point?Expellant
@Expellant it is obvious that git can do this by yourself, the point is that this shell serves to streamline the process for people using git on linux, being much simpler to run a sh git.sh your commit message, and not be made portable does not mean it will work properly.Pigg
D
4

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, ~/.bash_profile or ~/.zshrc 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

Dig answered 15/2, 2021 at 11:30 Comment(0)
S
2

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.
Snowinsummer answered 31/1, 2019 at 8:47 Comment(1)
-a / -all doesn't add new filesDolli
B
2

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"
Bondholder answered 23/5, 2019 at 16:29 Comment(0)
F
2
  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'

Freakish answered 13/1, 2020 at 2:56 Comment(0)
G
1

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"

Gamboa answered 10/9, 2015 at 14:4 Comment(0)
R
1

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

Rappee answered 27/4, 2016 at 14:8 Comment(0)
R
1

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.

Royster answered 16/12, 2016 at 18:58 Comment(3)
I should sue you for plagiarism. :)Laidlaw
Please explain the different additional insight your answer provides when compared to the noticably older answer by Navid.Parrotfish
@Parrotfish I guess I didn't see Navid's answer before posting since he's "too lazy" :P to post it as a full answer. Indeed, it's the same, and actually I have this alias since a long time ago, borrowed it from a coworker. So I guess we can call this: plagiarism chain. Sorry if it bothered you.Royster
P
1

you can use

git commit -am "[comment]" # best solution 

or

git add . && git commit -m "[comment]"
Portugal answered 18/3, 2017 at 9:29 Comment(2)
Second solution not needed at all.Mendiola
Second solution is needed. First one doesn't always work.Peeved
D
1

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'
Devine answered 4/1, 2019 at 13:9 Comment(1)
Love the choices. You could even add git config --global alias.acmp '!git add -A && git commit -m "update" && git push'Valenzuela
B
1

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.
Bergwall answered 17/4, 2020 at 2:6 Comment(0)
A
0

This answers the question in the title.. not the question in the description, but I think some people who end up here might find this useful.

The following bash script adds and commits files in one command. IT DOES NOT ADD ALL FILES, it just adds files you specify on the command line. This could easily be modified to add all files if no files are specified on the command line. However, that seems a little dangerous to me so I haven't done it.

#!/bin/bash

if [[ $# -lt 2 ]]
then
    echo "Usage:  $(basename $0) FILENAME+ \"COMMIT_MESSAGE\""
    echo 
    echo 'Shorthand for "git add FILENAME_0 FILENAME_1 .. FILENAME_n && git commit -m "COMMIT MESSAGE".'
    echo 'You must specify at least one filename, and supply one single commit message.'
    echo 
else    
    git add ${*: 1: $#-1} && git commit -m "${*: -1}"
fi

Save it in a file called gac, say, and use it like this

gac file_a file_b file_c "adding three files because.. reasons"

Ripping on the work of @LuisEnMarroquin in this thread.

Avelar answered 20/11, 2022 at 1:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.