简体   繁体   中英

How to get all deleted local files from remote using GIT

I have a local directory with multiple files. These files are already stored in remote git (BitBucket).

I accidentally deleted these files locally, how can I get it back from the remote repository?

Did you commit the deletions?

If not, you can use git checkout -- file to recover the files.

If you did, you can git reset to a previous commit where the files still existed.

If you need a more fine-tuned approach, you can git clone your remote repository to a new directory and then use file operations to copy files from the copy to your original repository.

You can reset your local master branch to the remote repo master like so:

git fetch
git reset --hard origin/master

You don't need to get them from remote if you already had those files locally.

You can use:

git checkout -- <deleted file name>

You can also use file and directory wildcards:

git checkout -- dir1/*

 git checkout [<tree-ish>] [--] <pathspec>... 

Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the <pathspec> are updated both in the index and in the working tree.

If you just deleted the file and do not run git add , then

git checkout -- <file>

If you have run git add , then

git reset HEAD <file>
git checkout -- <file>

If you have commit it to local repository, then

git reset --hard <the_commit_before_bad_commit>

Anyway, you'd better backup all the files when you try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM