0

Today I raised a PR (from my IDE): inside that, 3 files have been touch, in the commit I pushed.

Now what I did: I deleted one file's in GitHub and created a new commit (file changed is 2 now) .

Can you please help me to revert/undo the deletion of the file?

(I am very new to Git versioning system & GitHub.com)

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
Code cracker
  • 316
  • 1
  • 5
  • 20
  • You need to provide more details. Which branch did you delete the file from ? Did you delete the file from another branch and then merge via Pull Request (in this case you can [revert the Pull Request](https://docs.github.com/en/enterprise-server@3.0/github/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/reverting-a-pull-request)) or you just deleted from master/main branch and directly committed on the master/main ? – Asif Kamran Malick Oct 12 '21 at 16:45
  • I edited your message trying to make it clearer and to correct the use the technical terms. I tried my best, but please verify that the meaning has been preserved. – Kamafeather Oct 12 '21 at 17:33

1 Answers1

1

This is what I understood you did:
(even though you might have done it only via the GitHub.com GUI)

git add file1 file2 file3
git commit -m 'add files'
rm file1
git add file1
git commit -m 'removed unnecessary file'
git push origin master

now you want to restore the file1, right?

If yes, you can prepare via:

  • git stash -u to temporary remove all non-committed changes.
  • git checkout master to ensure you are in the same branch you want to restore the file into (can also use git log and git show HEAD to see you last commits)

Then…

…to actually restore the file1 you can:

  • git revert HEAD and confirm; this will create a new commit that restores all the changes brought from the last commit (HEAD). This is effectively like an undo operation, that will be visible in your Git history (again, see git log)

or

  • instead of restoring the whole previous commit, you can checkout (restore) only the specific file1, from a commit back in the history (HEAD is the current/last commit, while HEAD^ is one commit back in time, HEAD^^^ is 3 commit back in time; you can see its changes with git show HEAD^)
    • git checkout HEAD^ -- file1 to restore the file (see the created change via git diff)
    • git add file1 & git commit -m 'restore the file that was wrongly removed' in order to re-add the file and re-commit it.
    • git push origin master --force-with-lease to overwrite the previous commit
      • BEWARE: THIS IS STRONGLY DISCOURAGED! Especially if you don't have A LOT of confidence with Git. Forcing a git push has the potential to ERASE FOREVER ALL THE WORK YOU DID ON A PROJECT. So please, avoid this option, and check it again in the future. You can also give a check to this old question of mine.

Additionally

I have the impression that you do not have clear what is the difference between Git (a protocol and a versioning system managed via console) and GitHub.com (the Microsoft online portal to store, manage, display Git repositories).

The interface provided from GitHub.com is extremely limited, so you cannot perform the operation of revert only via the GitHub.com user interface.

Unfortunately you will have to learn how to manage Git via CLI (Command Line Interface). It is a pain in the beginning, but it is well worth the effort (if you are a developer).
Using one Git GUI Client (see this list) might help a lot while you try to understand Git, its commands and its logic.

PS - Don't forget to mark this as best answer, with the ✅, if this clarifies your doubts.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
  • Thanks for ur answer and explanation, I raised a PR from the vs code, I am explaining the steps what I did, I create one branch inside that I did only two file changes but I don't know why previous branch changes also reflected into the git then it becomes 3files changed in git PR, in the GitHub I deleted a file-3 but now I need to revert back Steps what I did in viscode:-1) git checkout 2)createNew branch3)commit message 4)select a tick mark In vs code 5)git push Steps I did in GitHub:-1) click on PR 2)select file changes 3)click on file 3 and delete 4) from vscode I did git pull – Code cracker Oct 12 '21 at 18:17
  • When you create a new branch, this is based on the current branch (so when you create a `PR-branch` from `branch-1`, you are at the same point in the Git history (you are only using a different pointer now, called `PR-branch`). When you create a commit on `PR-branch` this becomes the same as *[ history of `branch-1`, plus 1 commit pointed by `PR-branch`]*. **E.g.** of your history: INIT-commit <-- FIRST-commit (master) <-- SECOND-commit (branch-1) <-- THIRD-commit (PR-branch) <-- FOURTH-commit (commit to revert the changes from SECOND-commit). Tell me if I am wrong, it's not easy to guess. – Kamafeather Oct 12 '21 at 18:35
  • If that's correct, then I think your mistake was to **base the `PR-branch` onto the history of `branch-1`**, instead of basing `PR-branch` onto the history of `master` branch (so `PR-branch` derails from the history of `branch-1`). – Kamafeather Oct 12 '21 at 18:37