Git deleted my files after a failed branch switch

Viewed 618

Normally I would work on a local branch called test and then merge that branch into master once the feature is completed. However, for one reason or another I lost my latest changes.

Here is what I did:

T:\Test [test × +4 ~3 -5 !]> git status
On branch test
Your branch is based on 'origin/test', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   jest.config.js
        modified:   ormconfig.ts
        modified:   src/index.ts
        deleted:    src/it-portal/entity/Kiwi.ts
        deleted:    src/it-portal/entity/Movie.ts
        deleted:    src/it-portal/entity/User.ts
        deleted:    src/resolvers/HelloWorldResolver.ts
        deleted:    src/resolvers/MovieResolver.test.ts
        deleted:    src/resolvers/MovieResolver.ts
        deleted:    src/tests/Interfaces/global.d.ts
        deleted:    src/tests/config/globalSetup.ts
        deleted:    src/tests/config/globalTeardown.ts
        deleted:    src/tests/config/setupFiles.ts

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/it-portal/entity/Account.ts
        src/resolvers/AccountResolver.test.ts
        src/resolvers/AccountResolver.ts
        src/test-utils/

no changes added to commit (use "git add" and/or "git commit -a")

T:\Test [test × +4 ~3 -10 !]> git add .

T:\Test [test × +4 ~7 -6 ~]> git commit -am 'added Account entity and resolvers'
[test caaa67a] added Account entity and resolvers
 17 files changed, 164 insertions(+), 183 deletions(-)
 create mode 100644 src/it-portal/entity/Account.ts
 delete mode 100644 src/it-portal/entity/Kiwi.ts
 delete mode 100644 src/it-portal/entity/Movie.ts
 delete mode 100644 src/it-portal/entity/User.ts
 create mode 100644 src/resolvers/AccountResolver.test.ts
 create mode 100644 src/resolvers/AccountResolver.ts
 delete mode 100644 src/resolvers/HelloWorldResolver.ts
 delete mode 100644 src/resolvers/MovieResolver.test.ts
 delete mode 100644 src/resolvers/MovieResolver.ts
 rename src/{tests => test-utils}/Interfaces/global.d.ts (100%)
 rename src/{tests => test-utils}/config/globalSetup.ts (100%)
 rename src/{tests => test-utils}/config/globalTeardown.ts (100%)
 rename src/{tests => test-utils}/config/setupFiles.ts (100%)
 create mode 100644 src/test-utils/helpers/axios.ts

T:\Test [test ×]> git checkout master
fatal: cannot create directory at 'src/resolvers': Permission denied

T:\Test [test × +3 ~3 -8 !]> git merge test
Already up to date.

T:\Tes [test × +3 ~3 -8 !]> git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        jest.config.js
        ormconfig.ts
        src/index.ts
Please commit your changes or stash them before you switch branches.
error: The following untracked working tree files would be overwritten by checkout:
        src/it-portal/entity/Kiwi.ts
        src/it-portal/entity/Movie.ts
        src/it-portal/entity/User.ts
Please move or remove them before you switch branches.
Aborting

T:\Test [test × +3 ~3 -8 !]> git log
commit caaa67ae6643b5efa2c7f3bd65a3aa4317037367 (HEAD -> test)
Author: Me <bob@gmail.com>
Date:   Thu Jun 18 14:25:10 2020 +0200

    added Account entity and resolvers

commit 724bb33f38f3c380ac07666f0f6c3e5ca8648d8f (origin/master, master)
Author: Me <bob@gmail.com>
Date:   Wed Jun 17 15:27:14 2020 +0200

T:\Test [test × +3 ~3 -8 !]> git status
On branch test
Your branch is based on 'origin/test', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   jest.config.js
        modified:   ormconfig.ts
        modified:   src/index.ts
        deleted:    src/it-portal/entity/Account.ts
        deleted:    src/resolvers/AccountResolver.test.ts
        deleted:    src/resolvers/AccountResolver.ts
        deleted:    src/test-utils/Interfaces/global.d.ts
        deleted:    src/test-utils/config/globalSetup.ts
        deleted:    src/test-utils/config/globalTeardown.ts
        deleted:    src/test-utils/config/setupFiles.ts
        deleted:    src/test-utils/helpers/axios.ts

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/it-portal/entity/Kiwi.ts
        src/it-portal/entity/Movie.ts
        src/it-portal/entity/User.ts

no changes added to commit (use "git add" and/or "git commit -a")

T:\Test [test × +3 ~3 -8 !]> git branch
  master
* test

I noticed something went wrong while switching to the master branch. But now, although I did the correct commit in test, and still being on the branch test it's not showing me the added files like Account.ts and still showing me the removed files like Kiwi.ts. Any idea on how to fix this?

Also check the below, this is showing what I don't want to happen.

T:\Test [test × +3 ~3 -8 !]> git checkout test
Switched to branch 'test'
M       jest.config.js
M       ormconfig.ts
M       src/index.ts
D       src/it-portal/entity/Account.ts
D       src/resolvers/AccountResolver.test.ts
D       src/resolvers/AccountResolver.ts
D       src/test-utils/Interfaces/global.d.ts
D       src/test-utils/config/globalSetup.ts
D       src/test-utils/config/globalTeardown.ts
D       src/test-utils/config/setupFiles.ts
D       src/test-utils/helpers/axios.ts
Your branch is based on 'origin/test', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
1 Answers

What happens:

  1. git checkout master failed due to Permission denied. You may have created the new files with a more privileged users than your or the permissions on those files have been altered/corrupted for some reasons.
    Git was not able to perform the required changes to switch the branch and restore the working tree of the master branch.

  2. git merge test did not have any effect. You never switched the branch and you already were on test branch.

  3. The second git checkout master warned you of uncommitted changes and stopped the switch branch command.

  4. git log showed you the commit you did. This is a guarantee your work is not lost and a confirm you were on the test branch.

  5. git status showed you a messed up working tree.
    That working tree is the result of the first git checkout master (at point 1).
    In an usual scenario, when a git checkout is not possible due to a dirty working tree, git stops the operation, restores the working tree and shows the user the reasons it cannot change the branch, as it happened in point 3 (the second git checkout).

Most probably, the first git checkout was not able to restore the working tree as it not had the correct permissions to create folders, resulting in a messed up working tree.

You have a lot of methods to restore the working tree to the last commit in the current branch.
I suggest you to git reset --hard HEAD to fully restore the working tree of your test branch and to check any permission issues on files and resolve it before to attempt any checkout or merge command.

Related