Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git lists same file modified and not staged for commit?

Tags:

git

For some reason Git is telling me I have a file which is both "to be committed" and also "not staged for commit"? This doesn't make sense:

% git status  # On branch master # Changes to be committed: #   (use "git reset HEAD <file>..." to unstage) # #   modified:   Dir1/Dir2/filename.cpp # # Changes not staged for commit: #   (use "git add <file>..." to update what will be committed) #   (use "git checkout -- <file>..." to discard changes in working directory) # #   modified:   Dir1/Dir2/filename.cpp 

Dir1, Dir2 and filename.cpp are definitely all the same file. I had to add filename.cpp back again to get it to appear as a change to be committed (after it was originally in the repository). About the only thing that may have caused the problem was that I stashed, pull --rebase, then popped the stash? .gitignore does not list Dir1, Dir2 or filename.cpp anywhere and the patterns don't suggest they would catch this file?

like image 402
user1406626 Avatar asked May 20 '12 18:05

user1406626


People also ask

Why does git say changes not staged for commit?

The “changes not staged for commit” message shows when you run the “git status” command and have a file that has been changed but has not yet been added to the staging area. This is not an error message, rather a notification that you have changed files that are not in the staging area or a commit.

How do I see changes not staged for commit?

If you just want to see the diff without committing, use git diff to see unstaged changes, git diff --cached to see changes staged for commit, or git diff HEAD to see both staged and unstaged changes in your working tree.

What is meant by changes not staged for commit?

"Changes not staged for commit" means Some changes are not staged. So, to stage all changes perfectly, run this command: git add -A. Then, run this command: git commit -m "Update"


2 Answers

It means that you made a change to filename.cpp, added that change (with git add), then made another change that has not yet been added.

The "changes to be committed" part means that Git has updated its index with a change. When you run git commit, the changes to the index will be used to create the new commit object.

The "changes not staged" part shows the difference between the index and your working copy.

You can reproduce what you're seeing like so:

  • Edit filename.cpp
  • Run git status. You'll see "changes not staged".
  • Run git add filename.cpp
  • Run git status. You'll see "changes to be committed".
  • Edit filename.cpp again
  • Run git status. You'll see both "changes not staged" and "changes to be committed".

Does that make sense? It's always a little tricky explaining how Git works.

like image 124
Cameron Skinner Avatar answered Sep 27 '22 21:09

Cameron Skinner


In CVS, SVN, BZR, and no doubt a dozen other tools, once a file is added that means it'll be included in the next commit.

Not so GIT.

In git you don't "add a file", you "add a change" (sometimes it's called "stage a change").

If you change a file twice you have to "add" it twice.

like image 24
ams Avatar answered Sep 27 '22 23:09

ams