12

Possible Duplicate:
git add -A, git commit in one command?

If I understand correctly, when new files are added to a directory when using git, I have to call more than one command to make sure that the files are committed:

git add <directory name>
git commit -m 'commit comment'

This doesn't sound right because I know I (and many other people) will forget to call git add <directory name> and then end up with a project that is missing all the new files that were created. It seems that I should be able to commit the entire directory, including any and all new files, in one command without having to specify in a previous command to add all new files to the commit. How do I do this?

Community
  • 1
  • 1
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258

2 Answers2

7

git commit -a will commit all files known to git:

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

If you want to make sure you're committing everything you want to be, you can use git-status prior to a git-commit, to see the files that are staged for committing.

git uses a two-stage process to get changes from the working directory into the repository. A catch-all approach like git commit -a will work, but it will lead to messy, non-atomic commits. I would advocate for smaller, focused commits, rather than using a catch-all approach such as you are searching for.

This doesn't sound right because I know I (and many other people) will forget to call git add...

git-status is your friend :)

Note: git-add does not add files to the repository. git commit commits things from the staging area the repository, not from the working area. This might seem odd at first, but gives much greater flexibility. See another answer for more information.

Community
  • 1
  • 1
simont
  • 68,704
  • 18
  • 117
  • 136
2

get G2 at https://github.com/orefalo/g2

G2 will help you learn the git command line by providing easy to use macros.

the command is g freeze -m "commit msg".

It works with additions, deletions and changes. you are freezing a state.. which should have been the way to be from day one.

Olivier Refalo
  • 50,287
  • 22
  • 91
  • 122
  • 1
    Or you could skip G2 and just alias it: `git config alias.freeze "!git add -A && git commit"` – ellotheth May 25 '12 at 19:57
  • indeed, the alias is more like this freeze="!f() { ( [ -z $@ ] && git add -A || git add -A "$@" ) && git st; }; f". G2 has much more to offer... – Olivier Refalo May 25 '12 at 21:17
  • G2 has much more to offer.. see how freeze is implemented https://github.com/orefalo/g2/blob/master/cmds/g2-freeze.sh – Olivier Refalo May 25 '12 at 21:53
  • 1
    I did, that's where I got the alias (it works the same way: `git freeze -m "COMMIT ALL THE THINGS"`). G2 looks very cool and convenient, but it's overkill for the question. The OP wants one command to add and commit everything in the working copy, which vanilla Git can handle just fine. – ellotheth May 25 '12 at 22:07
  • I can only agree with the fact that it is overkill. – Olivier Refalo May 26 '12 at 12:19