43

Is there a way in Git to stage and commit files in one command? For example in my local repository I created files index.html, styles.css located in css folder and script.js located in js folder. Now I want to run one command to stage and commit all this files. I tried code below but it didn't work

git commit -a -m "my commit message"
1
  • 2
    This is generally a really bad idea: It invites errors of including too much in the commit. If you run git status, possibly followed by git diff <file> and eventually git add and git commit, it is easy to make sure that the commit is sane. If you just commit everything in your directory, you are eventually going to commit executables, generated PDFs, object files, scripts you didn't want to publish, private keys/passwords (yes, this actually happens!), files with private notes, etc. pp., the list goes on, and on, and on. Better safe than sorry. Jan 20, 2020 at 14:54

3 Answers 3

42

What you want to do is:

git commit -am "Message for my commit"

This will automatically add all tracked files and you can type your message in one command.

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

-m <msg> --message=<msg>
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

https://git-scm.com/docs/git-commit

If you want to stage and commit untracked files, you can:

git add -A && git commit -am 'message'
2
  • git add -A ; git commit -am "message" in PowerShell Feb 4, 2020 at 12:35
  • note that the code should have two ampersands, not one: git add -A && git commit -am 'message'. can't edit because it's less then 6 characters Nov 11, 2020 at 9:38
23

git commit -a ... will automatically add and commit files that have already been commited previously and are modified or deleted now. As you found out it does not affect new files.

You could use an alias to combine the git add ... and git commit ... into one command line. But if you do, take the time to script it to not need to use git add . or git add -A as that will inevitably lead to commiting files you really don't want to.

11

You can do this by using an alias.

Define an alias like this:

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

Then you can use it like a normal git command: git your-alias -m 'commit message'

1
  • 1
    This should be set as answer, works great. I use [alias] ac = !git add -A && git commit and it works wonders for my workflow :)
    – Fumler
    Feb 11, 2013 at 23:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.