Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

svn

git-svn

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

like image 329
Hamy Avatar asked Jul 01 '10 19:07

Hamy


People also ask

Why do I have to git add everytime?

You have to git add even with plain git to stage files for commit. It's just that if you explicitly list what you want to commit with git commit , it does staging for you. But if you don't specify list of files to commit, then git commit only commits files you staged with git add .

Can you git commit without adding?

Without adding any files, the command git commit won't work. Git only looks to the staging area to find out what to commit. Staging, or adding, files, is possible through the command line, and also possible with most Git interfaces like GitHub Desktop by selecting the lines or files that you'd like to stage.

Does git commit add all files?

Committing in git can be a multiple step process or one step depending on the situation. This situation is where you have multiple file updated and wants to commit: You have to add all the modified files before you commit anything. with this you have to add the message for this commit.

How often should I commit git?

The general rule (for both scenarios) would be: Commit as often as possible. If you think "it's not ready yet" (because it'll break the build or simply isn't done yet) then create a branch and commit to that branch but make sure you do commit.


2 Answers

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." 
like image 171
Justin L. Avatar answered Sep 30 '22 14:09

Justin L.


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

like image 43
Derek Greer Avatar answered Sep 30 '22 12:09

Derek Greer