0

I am adding a new file in remote repository on master branch when i do git fetch instead of fetching new changes it deletes the file which i added for example i added abc.txt in remote repository (master)

i am using git.fetch in code before pull (master)

but git fetch is deleting the file

abc.txt

here is my code

git.fetch().setRemote("origin").setRefSpecs(new RefSpec("refs/heads/master:refs/heads/master")).call();

please guide me what i am doing wrong i also looked at this answer JGit FetchCommand

swaheed
  • 3,671
  • 10
  • 42
  • 103
  • _"I am adding a new file in remote repository"_ do we agree that you're adding new file in the same repo where you do the fetch? Also, the added content is not yet committed? – Mincong Huang Jun 03 '18 at 15:51
  • yes i am adding the file to the same repo where i am doing the fetch ...in remote repo ............while staying on github website i am adding a new file and commitng it – swaheed Jun 03 '18 at 15:53
  • I cannot reproduce in my machine. I think you might mix the notion of pull and fetch. Fetching content from upstream does not change your workspace. So, if a file is added on GitHub directly, fetching content from it won't change anything in your local repo (which makes you think JGit deletes it). You need to "pull" instead. – Mincong Huang Jun 03 '18 at 15:59
  • i know git fetch will not make any changes that is exactly what i want ..also i need to use git fetch first and then pull in my use case – swaheed Jun 03 '18 at 16:13
  • git fetch is working fine in console but not in code please help – swaheed Jun 03 '18 at 16:14

0 Answers0