Should I have to add files to git every time I want to commit?

I'm new to git, coming from SVN world. So far, it seems a lot more useful, but I am still working out the kinks.

Currently, my workflow is like this:

make changes > git add . > git commit > enter log message

What I don't understand is why I seem to have to add all of my files before I commit. They are under version control already? Why does git commit tell me there are no changes added to the commit, but also point out that I have modified files? It says "Changed but not updated:". What does this mean??

Sorry if this is easy, I feel like I am missing some overarching point

19854 次浏览

This allows you to separate commits by edits. If you only want to commit these files now, under one commit, and then these next files now, under a second commit, you could do:

git add files_under_one_topic
git commit -m "this is about one thing"


git add files_left_over_to_commit_about_a_completely_different_topic
git commit -m "this is about another thing."

You're not adding files in the sense that you're putting them under git control, but you're adding them to a change list. Some other SCMs such as perforce do this as well. It's handy for building up distinct sets of changes that you aren't ready to commit yet, but you want to commit in separate blocks.

You can commit in a more subversionish way by simply doing git commit -a -- which will commit everything that git knows about that has been changed, just like what svn commit does.

(PS. Since you're coming from the svn world, I'll mention another gotcha that threw me for a loop for a bit -- when you git diff, it will only show you the diff between your current state and what is in the changelist, NOT the difference between your current state and the last commit. If you run git diff immediately after adding all changed files (e.g. git add -u), you'll see an empty diff, even though there are differences to be committed!)

Git works by using a "staging" area where you prepare what you are going to bundle together as a commit. So, you decided what set of changes you want to commit (e.g. all or a subset), you add them to the staging area, and then you commit what's in the staging area.

When you invoke git status, it shows you what has been added to the staging area (i.e. "Changes to be committed"), what has been modified among the files git is tracking (i.e. Changed but not updated"), and any new files that you've never added before (i.e. Untracked files).

If you want to just commit stuff that you've changed, but not include newly created files you can use git commit -a -m "Comment for modified files already under source control."