4

Couldn't find my problem so I figured I would ask.

Myself and another person is working on this repo. My local clone is up to date.

My partner deletes a file 'example.txt', commits and pushes it up.

I come in the next day, do a git status (up-to-date), then a git pull. It deletes the 'example.txt' from my local repo.

The problem is that I needed that file and my partner accidentally deleted it.

How do I prevent this from happening in the future? Is there a way to have git pull warn me first before it deletes something?

So I tried first doing a git fetch, and then git status, but it doesn't show that I'm about to delete a file if I merge.

Discoveringmypath
  • 1,049
  • 9
  • 23
  • [Why git rm --cached not remove local ever tracked file but others](https://stackoverflow.com/q/55663325/6521116) – LF00 Apr 25 '19 at 01:13

2 Answers2

1

You could show all differences between your local branch, say, master branch and a remote master branch after doing git fetch like that:

$ git diff ...origin/master
diff --git a/example.txt b/example.txt
deleted file mode 100644
index e69de29..0000000

At this point you still have example.txt on your master but it will be removed when you do git merge. Out of habit I also frequently do that before finally merging fetched changes into my local branch.

However, judging by the way you describe the problem it seems that you don't really understand how git works. If another user has committed file deletion and pushed commit to the remote branch this operation cannot be undone and you cannot ignore it - you will have to accept that change into your local master branch sooner or later before pushing your changes to the remote. If another user has removed that file inadvertently you can restore it and push it to the remote, ideally in a separate commit or revert the commit that another user has pushed.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
0

As a git pull is basically a git fetch followed by a git merge, it is interesting to see this comparison about these two (fetch x merge), then take a look in this strategy regarding how to preview merges and, therefore, something that can help you pulling safely using git.

vfalcao
  • 332
  • 1
  • 3
  • 12