Can a file be both staged and unstaged in Git?

后端 未结 4 1922
挽巷
挽巷 2021-01-31 10:56

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 i

4条回答
  •  名媛妹妹
    2021-01-31 11:52

    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.

提交回复
热议问题