Home > Mobile >  discard changes not staged for commit
discard changes not staged for commit

Time:11-13

When I git status under my project I see:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   MyApp/mylib (new commits)

The mylib is a git submodule under which there are other files.

I would like to discard changes in MyApp/mylib. So, I tried run command:

git restore MyApp/mylib

But after that when I git status again, I see the same result. How can I discard changes of MyApp/mylib so that it doesn't show as changes not staged?

CodePudding user response:

The modified (new commits) status really just means that the submodule is currently "on" a different commit than the superproject calls for.

To force the submodule back to the commit that the superproject calls for, you need to have Git itself run:

git -C MyApp/mylib switch --detach $hash

where $hash is the hash ID that the superproject (i.e., your current commit in your repository) is calling for. Where do you get that hash ID? Well, the easy way is to let Git fish it out for you, by running:

git submodule update --checkout MyApp/mylib

The git submodule command has a lot of modes; the update --checkout mode says using the current superproject commit, find the $hash value, and then run the above git -C command to check out that commit within the submodule.

(The fact that the submodule is on the "wrong" commit is suggestive though: it means that maybe you don't want to force the submodule back to the superproject's recorded commit. Maybe instead you'd like the superproject to record the submodule's current commit. But you did not ask how to do that, so let's not confuse things more.)

CodePudding user response:

Either of these 2 should work:

  • To discard unstaged changes to a file permanently:

    git checkout -- MyApp/mylib

  • To discard changes and save them for later use

    git stash

  • Related