|
|
Subscribe / Log in / New account

Hmm …

Hmm …

Posted Nov 4, 2018 19:03 UTC (Sun) by smurf (subscriber, #17840)
In reply to: Hmm … by epa
Parent article: Apache Subversion 1.11.0 released

> It's more about 'git checkout .' which will silently overwrite every local change in the working copy.

That depends on whether you want to get a single file (or three), or go to a different commit. The latter refuses to overwrite things.

> what is the safe git command to bring back deleted files

There is none. If you overwrite a file that hasn't been checked in, it's gone, just like when you use "rm". In fact, if you try to "git rm" a modified file, it'll warn you.

Adding a warning option does make sense; I'll file a feature request.


to post comments

Hmm …

Posted Nov 4, 2018 20:11 UTC (Sun) by madscientist (subscriber, #16861) [Link]

Note the OP includes a "." at the end of the git checkout command:

> It's more about 'git checkout .' which will silently overwrite every local change in the working copy.

Adding a "." causes a checkout of all (modified) files from the current directory down. It cannot go to a different commit.

For me, if I want to un-modify a file I use "git status" to see a list of modified files, and "git checkout filex filey ..." (perhaps copy/paste) to check out specific files. If I want to throw away everything I use "git reset --hard HEAD" or similar. I virtually never use "git checkout <directory>" and wouldn't suggest it to others, either.

> what is the safe git command to bring back deleted files

By this I assume you mean a committed file that you have deleted and then want to recover and not an untracked file that you've deleted.

The best way to know how to solve almost ANY issue in a workspace is to run "git status" and it will tell you how to undo it. For example, if you "rm somefile" and then decide you want it back, "git status" will tell you:

> Changes not staged for commit:
> (use "git add/rm <file>..." to update what will be committed)
> (use "git checkout -- <file>..." to discard changes in working directory)

> deleted: Demo/readme.txt

If you use "git rm somefile" then decide you want it back, "git status" will tell you:

> Changes to be committed:
> (use "git reset HEAD <file>..." to unstage)

> deleted: Demo/readme.txt

And finally when in doubt, I use "git stash" to stash modified files before doing any kind of bizarre operations on a repository where there are modifications I want to preserve, just in case.

Hmm …

Posted Nov 5, 2018 6:59 UTC (Mon) by epa (subscriber, #39769) [Link] (1 responses)

No, what I mean is when you 'rm' a file or two in the working copy and then you want to restore those files from the current commit (or from git's index, if they happen to be in there).

'git checkout .' will appear to work for this task but it is highly dangerous, since it unconditionally trashes any other local changes. As another poster noted, there doesn't seem to be a command which does it, short of running 'git status', looking for 'deleted' lines, and running 'git checkout' over those.

Now, you may say that this isn't a sensible way to work, and the right way to revert changes in a file if you messed it up is to run 'git checkout FILE' directly. Not to just delete the file and then hunt for the right incantation to bring it back. But rightly or wrongly it is a natural human reaction to say "I really messed this up, let's just delete it and start again".

I did raise the feature request on the mailing list: https://public-inbox.org/git/loom.20150603T104534-909@pos...

Hmm …

Posted Nov 13, 2018 22:51 UTC (Tue) by arnout (subscriber, #94240) [Link]

> No, what I mean is when you 'rm' a file or two in the working copy and then you want to restore those files
> from the current commit (or from git's index, if they happen to be in there).

You might want to give 'git checkout -p' a try.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds