|
|
Subscribe / Log in / New account

Hmm …

Hmm …

Posted Nov 4, 2018 17:52 UTC (Sun) by epa (subscriber, #39769)
In reply to: Hmm … by pizza
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. I would have expected it to prompt on overwrite, or at least have an option to do so (like cp -i or mv -i).

Perhaps the fault is with whoever recommended 'git checkout .' as the way to recover, from the current commit, files deleted in the working copy. (With svn I would do 'svn update' for that, which is reasonably safe.) But then, what is the safe git command to bring back deleted files while not trashing local changes in others?


to post comments

Hmm …

Posted Nov 4, 2018 19:03 UTC (Sun) by smurf (subscriber, #17840) [Link] (3 responses)

> 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.

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.

Hmm …

Posted Nov 6, 2018 15:55 UTC (Tue) by jezuch (subscriber, #52988) [Link] (2 responses)

One small nit: "git checkout <path>" will trash all changes that are not in the index (AKA staging area). I use it all the time to trash only select changes: "git add -p" and then "git checkout ." usually followed by "git reset". It's very powerful. But you need to have total situational awareness of your working copy for this to work without surprises.

Hmm …

Posted Nov 6, 2018 20:09 UTC (Tue) by epa (subscriber, #39769) [Link] (1 responses)

Yes, that's the core of my complaint: it sounds a lot more innocuous than it is. If it were called 'git overwrite-local-changes' there wouldn't be any issue. I think that being called 'checkout' it should prompt a bit more before trashing multiple files.

Hmm …

Posted Nov 11, 2018 18:37 UTC (Sun) by jezuch (subscriber, #52988) [Link]

I agree that names could be better. This is one more illustration of evolution over intelligent design ;) But after a while of using git it's all just... identifiers.

Hmm …

Posted Nov 6, 2018 20:14 UTC (Tue) by neilbrown (subscriber, #359) [Link] (4 responses)

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

Anyone want to write a patch which causes "git checkout" (and anything else potentially destructive) to do an automatic "git stash" first ???

Hmm …

Posted Nov 11, 2018 17:58 UTC (Sun) by Wol (subscriber, #4433) [Link] (1 responses)

And how is that going to help us noobs (who don't have a great understanding of git)?

I tend to use git with a bit of a cookbook aide-memoir, I have an understanding of what's going on but it's not particularly good. An automatic "git stash" will be a pretty good way of helping me lose stuff, because I haven't yet got to grips with what stash actually does. Yes I know it puts stuff "somewhere safe", but as it is at home, "somewhere safe" usually equates to "I've forgotten where I put it" ie I've lost it.

Cheers,
Wol

Hmm …

Posted Nov 11, 2018 21:59 UTC (Sun) by neilbrown (subscriber, #359) [Link]

> And how is that going to help us noobs (who don't have a great understanding of git)?

The best way to help noobs like you is through education.
An important part of education is encouraging exploration and experimentation.

If something goes wrong and you try to fix it up and fail, and then post in some community "Hi, X happened so I tried Y and now Z", then someone says "Oh no, never try Y - that is a disaster, I hope you have backups" - then you might not want to experiment again.
If, instead, they could say "Y probably wasn't the best approach, but git has your back - just run this simple command and look through the resulting log until you see the code you lost - then do Q to recover it" - you might be more likely to experiment more next time.

So I agree that auto-git-stash is not a complete solution, but it does seem that it would still be valuable.

Hmm …

Posted Nov 14, 2018 8:29 UTC (Wed) by jezuch (subscriber, #52988) [Link] (1 responses)

There *is* autostash but I haven't used it so I don't know what it does or when :)

Hmm …

Posted Nov 14, 2018 15:28 UTC (Wed) by mathstuf (subscriber, #69389) [Link]

It's just used during `git rebase` (AFAIK). It stores your local diffs until the rebase is over and then applies them (keeping them in the stash if there are conflicts). It also comes into play when using `git pull --rebase`.


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