littleworth
littleworth

Reputation: 5169

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?

Upvotes: 1

Views: 416

Answers (4)

galian
galian

Reputation: 900

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.

Upvotes: 0

pepak
pepak

Reputation: 742

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.

Upvotes: 2

sergej
sergej

Reputation: 18009

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.

Upvotes: 2

MaddawgX9
MaddawgX9

Reputation: 151

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

git fetch
git reset --hard origin/master

Upvotes: 1

Related Questions