Restore a deleted folder in a Git repo

后端 未结 8 576
执笔经年
执笔经年 2020-12-02 07:22

I have deleted all the contents inside a folder and the folder is empty. I still had a copy in my remote repo. But when I did a git pull it didn\'t put back the

相关标签:
8条回答
  • 2020-12-02 08:05

    The only thing that worked for me was to checkout the repo in another folder. Assume the current repo is in /home/me/current.

    I then did

    git clone /home/me/current /home/me/temp
    

    This make a separate clone of the repo in /home/me/temp

    I can now go to /home/me/temp and do whatever I want. For example

    git reset --hard commit-hash-before-delete
    

    Now I can copy the deleted file folder back

    cp -r /home/me/temp/some/deleted/folder /home/me/current/some/deleted/folder
    

    And delete the temp folder

    rm -rf /home/me/temp
    

    The examples of

    git checkout -- some/deleted/folder
    git checkout -- some/deleted/folder/*
    

    DO NOT WORK

    $ git checkout -- some/deleted/folder/*
    zsh: no matches found: some/deleted/folder/*
    $ git checkout -- some/deleted/folder
    error: pathspec 'some/deleted/folder' did not match any file(s) known to git.
    

    Other examples like

    git reset --hard HEAD
    

    are destructive beyond just the deleted files. Any other changes will also be lost.

    Similarly

    git reset --hard some-commit
    

    will lose any commits after some-commit

    0 讨论(0)
  • 2020-12-02 08:11

    for uncommited deletions, Its as simple as this :

    git reset HEAD rel/path/to/deleted/directory/*

    0 讨论(0)
提交回复
热议问题