1

I am a new user of Git, and I wonder if the the following scenario is possible:

I want to 'clean' a directory that i have been using, as to keep a clear oversight of my project, by deleting some of the files it contains. Let's say that the file I want to delete is called old.txt.

However I cannot be sure that I will never want to come back to this file ever again - so I would like to still have the option to revert to it by using the git history.

Is there a way to make sure that this is still possible?

jim jarnac
  • 4,804
  • 11
  • 51
  • 88
  • Possible duplicate of [How can I delete a file from git repo?](https://stackoverflow.com/questions/2047465/how-can-i-delete-a-file-from-git-repo) – Sunny Patel Aug 29 '18 at 15:01
  • Your scenario is not clear to me. Are your to-be-deleted files already commited to yout Git repository? If so, no problem, then you'll be able to resurrect them in the future. – leonbloy Aug 29 '18 at 15:03
  • @Sunny Patel No it is the opposite i want to do. -> keep the file in the git repo but remove it from the dir file system – jim jarnac Aug 29 '18 at 15:05
  • Not a duplicate! The question (as I understand it) is whether it is possible to come back to a deleted file and how to make sure it is found again – Maximilian C. Aug 29 '18 at 15:05
  • `git rm` will delete the file locally and in git. If you want the file again, just look in the commit history to find it, and you can cherry pick to restore that version again. – Sunny Patel Aug 29 '18 at 15:06
  • @leonbloy Thx, good to now this is possible - Do you know any link to that functionality in the git documentation? It would be important for me to understand the ins and outs, in order to keep the file system of my project clean (dozen of old files starts to collect and that would be a great way to archive) – jim jarnac Aug 29 '18 at 15:06
  • 4
    Possible duplicate of [Find and restore a deleted file in a Git repository](https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository) – Adam Millerchip Aug 29 '18 at 15:08
  • From my understanding of your question, this is a duplicate because you will be able to restore it in the future once it is deleted. – Sunny Patel Aug 29 '18 at 15:08
  • Note that the point of a version control system is to be able to get back *every* version ever committed—or at least, every version you haven't cleaned away at some point in the future. (Git does not provide a "clean out / forget-forever old versions" feature, for various good technical reasons, but that's something people do want in some commercial VCSes. One can build this for Git, sort of, using `git replace` and filter-branch.) – torek Aug 29 '18 at 15:28

2 Answers2

2

When traversing the commit tree (e.g. by using Git log), you will still be able to see the changes you did to your file and see which commit deleted it. However, this is not helpful when you want to come back and maybe restore the file at a later point in time.

I'd suggest to use a Git tag command to mark ("tag") the version of your software before you perform the deletions:

git tag -a v0.3-snapshot-before-refactoring -m "Some meaningfull message"

You could then retrieve this version using the Git checkout command:

git checkout v0.3-snapshot-before-refactoring
Nisarg
  • 1,631
  • 6
  • 19
  • 31
Maximilian C.
  • 967
  • 5
  • 22
0

Say you have a distant repository, for example Github or Bitbucket.

Git enable your to navigate between all your repository states with the git checkout command.

The only to lose your old.txt is to perform a git push --force or similar on your repository. Apart from this command you are safe.

If you don't use --force flag, the command will only add some history, without altering current history.

Of course I put aside the possibility of Github / Bitbucket server to crash or corrupt your data, or the possibility that the repository could be deleted.

Matthias Beaupère
  • 1,731
  • 2
  • 17
  • 44