53

I had up to date repository. Then I had deleted several files and this was mistake. I would like to get fresh repository back. I do

$ git pull origin master

And expect to get everything from server, but getting message that everything is up to date.

How to get my sources from server using git pull then?

11 Answers 11

95

You can reset local branch to what's at remote

git reset --hard origin/main
7
  • 1
    Because the user doesn't say anywhere that he commited the changes to the project. He could just checkout the files that he deleted... Commented Jun 6, 2017 at 21:38
  • 3
    @NativeCoder it doesn't matter if he committed them or not, a hard reset to the remote tracking branch will restore the working directory to that state. Specifically, he states "I would like to get fresh repository back." Commented Jun 6, 2017 at 22:02
  • Upon second look, the op DOES state "with git pull". In which case this would be the only "correct" answer. Sorry! Have that rep back! Commented Jun 6, 2017 at 23:02
  • Be careful while doing HARD reset. It will reset everything including your configurations. Commented Feb 26, 2019 at 16:05
  • prefered method: git checkout [dir_name]
    – Teson
    Commented Mar 18, 2020 at 20:06
43

The files are in your local git history so you won't need to pull code to get them back. Run

git checkout <file>

on the missing files. If you don't have any uncommited changes then

git checkout .

in the git root directory will work as well.

1
  • 5
    This is my favorite answer, despite mine having been accepted with ~3x more upvotes. Cheers 🍻 Commented Nov 6, 2020 at 2:20
16

This shows all the deleted files.

git ls-files --deleted 

This will restore the deleted file.

git checkout <deleted file name with complete path>
4

For all the unstaged files use

git checkout -- .

Or the more safer way

git clean -fd

2
  • 2
    Assuming he didn't commit anything
    – everag
    Commented Jun 25, 2016 at 23:04
  • This does delete any modified files. It does not just do a fresh pull of deleted files (even if OP wants a fresh restore...good to mention since it is easy for others to miss that point) Commented Oct 17, 2019 at 20:54
3

thats all you need

git reset --hard
git fetch origin 
git pull origin master
1
  • 2
    No need to fetch on origin if you will pull next
    – everag
    Commented Jun 25, 2016 at 23:04
1

What is the result of git status command ? You can also try git reset --hard origin/master

1

This will restore all of the deleted files since they still reside in your local repository.

git checkout -- .

Whenever I want to start over with a file I messed up, or even just scrap the in repository builds, I just delete everything except the .git folder and run this to get back to a fresh start.

0

If you just revert your change, you will get all the files deleted. It is not a workaround (that you mentioned) where you have to reset, IMO it is better than getting fresh repo.

0

To get the deleted files from git pull, we can use this command to retrieve this deleted files back

git rev-list -n 1 HEAD
0

try the command

$ git reflog 

above command will give you tips of branches and other references that were updated in the local repository like below (Recent updates are at top)

da82426 (HEAD -> main) HEAD@{0}: reset: moving to HEAD
da82426 (HEAD -> main) HEAD@{1}: reset: moving to HEAD~
774b952 (origin/master, origin/main) HEAD@{2}: reset: moving to HEAD
774b952 (origin/master, origin/main) HEAD@{3}: checkout: moving from master to main
8851ece (master) HEAD@{4}: checkout: moving from master to master
8851ece (master) HEAD@{5}: commit: First commit
8bd9080 HEAD@{6}: commit (initial): Initialize project using Create React App

Then revert to your desired stage by using command like below

$ git reset --hard 8851ece

It will probably get files back.

-1

What about git stash command ? If you didn't commit your changes (e.g the files deletions) then you can just run git stash

1
  • git stash won't help here. Just take the accepted answer as accepted
    – Nico Haase
    Commented Jan 25, 2018 at 17:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.