Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a file be both staged and unstaged in Git?

While working on another file, I edited README.md and then ran git add README.md. When doing a git commit, I see that README.md is both in the "Changes to be committed" and "Changes not staged for commit".

Does this make sense? Where in .git could I look to see the authoritative state of this file?

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#       modified:   utils/arrterm
#
# 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:   README.md
like image 665
Mark Harrison Avatar asked Jul 19 '14 07:07

Mark Harrison


People also ask

What is the difference between staged and unstaged files in git?

Unstaged changes are changes that are not tracked by the Git. For example, if you copy a file or modify the file. Git maintains a staging area(also known as index) to track changes that go in your next commit.

Will unstaged files be committed?

TL;DR: When one file has staged and unstaged changes, a commit will commit both versions, with the most recent changes to the file.

Is it possible to stage only some part of file git?

Staging PatchesIt's also possible for Git to stage certain parts of files and not the rest.


1 Answers

This means part of the changes you made are staged for commit and parts are not.

You can check what is staged if you run

git diff --staged -- README.md

and check for what is unstaged by running

git diff -- README.md

Most version control systems generally only store the changes between two states. The way git works, while you make multiple changes to a file, you will have to add/mark each of them to be part of one set of changes a.k.a a commit. When you use git add this is done automatically.

However it is not the only way you add can all your individual changes(hunks) to your "index". You can for example make multiple changes to the same file and commit them in different commits or add only specific changes to a commit. E.g. to explicitly add some changes to your "index" but not others, you can do so by using git add -p to add only some "hunks" (groups) of the changes instead of the whole list of changes itself.

What has happened here is that the changes you made to README.md before staging ( git add) will show in staged and any changes you made after staging README.md will show as unstaged as you have got above.

like image 106
Manquer Avatar answered Oct 29 '22 17:10

Manquer