Search code examples
gitgithubgit-forkgit-rm

Git - How do I delete files in upstream repo but not from my fork


I have forked a repo (called the original repo upstream) because there had to be done some cleanup but I wanted to keep the folder johannes I work with.

So I went along and deleted that specific folder with

git rm -cached -r johannes/
git commit -m "deleted my folder so it doesn't bother you anymore."
git push upstream

I thought with declaring the upstream repo in my push the folder should still be available in my own fork. However it is gone there too...

  1. What is the best way to delete files on the original repo without having them deleted on the fork? Is that even possible?

  2. How can I recover the deleted folder in my fork repo? (I do still have it locally, but all the history would be gone if I just pushed that again)


Solution

  • In order to not mix up the pushes, I would keep two separate clones:

    • one of the original repo
    • one of the fork

    That way, what you do in one (and its associate push) won't affect the other repo (both the local and remote one)

    Regarding your fork, if you have only made one commit recording that deletion, you can (if you don't have any current cork in progress):

    git reset --hard @~
    git push --force
    

    (assuming you are on the right branch, and that branch is set to be pushed to origin)

    If you have pushed some commits since then, but don't mind rewriting the history of your fork, do this in a different local clone of your fork

      git clone https://github.com/<you>/<yourFork> yourFork2
      cd yourFork2
      git reset --hard <last_commit_where_folder_was_present>
      git --work-tree=../yourFork add .
      git commit -m "Add work since last_commit_where_folder_was_present>
      git push --force