82

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

1
  • 9
    Great questionI found this highly confusing as well. Apparently, you don't "Add" to the project, you add to the commit! Commented Mar 9, 2016 at 12:01

3 Answers 3

58

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." 
Sign up to request clarification or add additional context in comments.

2 Comments

It goes further even than that. Using git add -p, you can stage some changes within a file, but not others.
whoa. that's really powerful. Good to know that's possible, it will probably come un super handy the few times I need it!
25

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

3 Comments

Also, note that 'git stage' can be used instead of 'git add', if it helps to keep things straight in your head.
This has to be one of the better answers.
that last one yields the error fatal: Paths with -a does not make sense.
14

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

1 Comment

Ah, thanks for the PS! That is definitely something I have been struggling with!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.