52

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?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
vico
  • 17,051
  • 45
  • 159
  • 315

11 Answers11

89

You can reset local branch to what's at remote

git reset --hard origin/main
whiletrue
  • 10,500
  • 6
  • 27
  • 47
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 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... – Native Coder Jun 06 '17 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."* – Jeff Puckett Jun 06 '17 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! – Native Coder Jun 06 '17 at 23:02
  • Be careful while doing HARD reset. It will reset everything including your configurations. – Ram Feb 26 '19 at 16:05
  • prefered method: git checkout [dir_name] – Teson Mar 18 '20 at 20:06
  • it is git reset --hard origin/main – Badr Bellaj Jun 24 '22 at 10:11
37

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.

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
15

This shows all the deleted files.

git ls-files --deleted 

This will restore the deleted file.

git checkout <deleted file name with complete path>
ivcubr
  • 1,988
  • 9
  • 20
  • 28
Manjunatha S M
  • 180
  • 1
  • 5
4

For all the unstaged files use

git checkout -- .

Or the more safer way

git clean -fd

Shravan40
  • 8,922
  • 6
  • 28
  • 48
  • 2
    Assuming he didn't commit anything – everton Jun 25 '16 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) – TamusJRoyce Oct 17 '19 at 20:54
3

thats all you need

git reset --hard
git fetch origin 
git pull origin master
Maksym Semenykhin
  • 1,924
  • 15
  • 26
1

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

barden
  • 349
  • 6
  • 18
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.

Pervez Alam
  • 1,246
  • 10
  • 20
0

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.

aquawicket
  • 524
  • 1
  • 9
  • 27
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
Gowtham SB
  • 332
  • 1
  • 3
  • 16
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

LoanJ4490
  • 19
  • 2