Restore a deleted folder in a Git repo

后端 未结 8 575
执笔经年
执笔经年 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 07:53

    If you are just looking to recover a deleted folder and you have other commits after the deletion, then you can also just goto your project on github.com.

    From github.com, go you to your last commit that has your folder. You should see the commit message and to the right there's a button labeled "Browse Files". Clicking this will take you to all the files from that stage of the commit.

    From there you can clone the code or just download the code as a zip.

    0 讨论(0)
  • 2020-12-02 07:57

    As of git 2.24.0, there's an experimental new git command: git restore

    git restore --staged some/deleted/folder
    
    0 讨论(0)
  • 2020-12-02 08:00

    If you don't specify a specific file you should be able to pull the full contents of a specific commit. Like: git checkout 264794319e9695ba843cd6 (assuming that hash has all your files at the right state).

    The reason pull isn't restoring files is that git sees your deletions as the more recent change, applying that on top of whatever you're pulling.

    (I'd recommend experimenting in a new branch.)

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

    Everything you can do with a file, you can do with a folder too.

    Also note Find and restore a deleted file in a Git repository


    Files are deleted from working tree but not committed yet:

    If you have not yet indexed (git add) your changes you can revert content of a directory:

    git checkout -- path/to/folder
    

    If the deletion is already indexed, you should reset that first:

    git reset -- path/to/folder
    git checkout -- path/to/folder
    


    Restore the full working tree (not a single folder), but lose all uncommitted changes

    git reset --hard HEAD
    


    When files are deleted in some commit in the past:

    Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.

    git rev-list -n 1 HEAD -- <file_path>
    

    Then checkout the version at the commit before, using the caret (^) symbol:

    git checkout <deleting_commit>^ -- <file_path>
    


    Restore the full working tree from a distant commit

    git reset --hard <revision> 
    
    0 讨论(0)
  • 2020-12-02 08:01

    If you have not yet commited your changes you can revert content or a directory:

    git checkout -- removed_directory
    

    If you want to revert all changes do:

    git reset --hard HEAD
    
    0 讨论(0)
  • 2020-12-02 08:04

    You can restore files or folder with git restore.

    git restore --source master~1 "PATH_IN_YOUR_REPO"
    

    Here, master~1 reverts your folder to "1" revision back from your master branch.

    Source: https://git-scm.com/docs/git-restore

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