How to undo the changes in github after deleting a file?

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)

1 answer

  • answered 2021-10-12 17:51 Kamafeather

    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.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum