What is the difference in git of rm file and reset hard?

1

I'm starting in the world of programming but I have a doubt that there is a difference between rm "file" and git reset hard

    
asked by Edgardo Luis Jimenez 26.11.2018 в 19:03
source

1 answer

-1

In principle they have nothing to do with each other.

rm (without git in front) is a shell command. Delete a file without more. If the file was not under version control, git will not miss it (in fact it will stop telling you that the file is not under version control, as it has ceased to exist). If instead it was under version control, git will warn you that it has disappeared, giving you the option to recover it or also remove it from the repository.

git rm tells git that this file (which should be under version control) is not present and also deletes it from the disk. It would be the correct way to delete a file from the work folder at the same time you remove it from the repository. However it is common to make the mistake of deleting it with rm to dry, to later realize that git rm should have been used. In this case nothing happens, you can do the git rm later (even if the file no longer exists) so that git miss missing it.

After a git rm it is necessary to make a commit so that this change is registered in the history of the repository. Of course that does not erase the file of previous commits in which it already appeared, but at least when any other person makes a git clone to get the latest version, you will not get that file in your folder of work (although if you do checkout to previous versions you could get it, so it's not a way to erase it "at all").

Finally, git reset is a way to make a branch "backtrack" to a past commit. If from that point you return to do commit s new, they would "overwrite" (so to speak) those that had the original branch from that point. It is not convenient to do it if your repository is shared with others, because you are overwriting the history of a branch and that will cause problems to other people who can lose work if they had been developing in the same branch.

git reset --hard in addition to backing the branch, update the working folder eliminating all the modifications that would have been made in all the files from the indicated commit, and eliminating the files created since then. That is, the work folder will remain as it was in that commit. It is a dangerous command that can make you lose work. Use it only if you are sure of what you do.

    
answered by 26.11.2018 в 20:04