{"thread":{"id":"55571","subject":"Request: `git restore $commit $file` shouldn’t override uncommited changes","startedAt":"2021-04-27T17:38:12Z","lastAt":"2021-04-28T09:10:39Z","messageCount":5,"participants":["Robin Moussu","Johannes Altmanninger","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"423117","messageId":"pYZzGPZTHnJjYBKrUAVGcso74I_xJgfzNpSwDN94fhYcDoOamp62-IFvxVrU056uw0txy3MTHYSwny_II0XY4trSY5_E25q7EXwhNHjy3VY=@pm.me","threadId":"55571","inReplyTo":null,"subject":"Request: `git restore $commit $file` shouldn’t override uncommited changes","fromName":"Robin Moussu","fromEmail":"moussu.robin@pm.me","sentAt":"2021-04-27T17:37:59Z","receivedAt":"2021-04-27T17:38:12Z","isPatch":false,"sender":{"key":"moussu.robin@pm.me","avatar":"https://gravatar.com/avatar/e6f1710356a7fb672e1ee31e71b2ceafe4d788678de323436463ff3ae47a98fb?d=mp&s=160"},"body":"Hello,\nThat’s the first time I’m interacting with the git community, I’m not very familiar with the process. I hope I’m at the right place for a feature request.\n\nCurrently, I don’t think that it’s possible to get an error when copying the content of a file from another revision into your working tree if said file has uncommitted changes.\n\nI recently discovered that `git restore` was introduced to make file manipulation less confusing than with `git checkout`. I know it was introduced a few years ago, I’m late to the party! I would have expected that the semantic of `git restore` or `git restore $file` would discard all uncommitted changes (you are restoring the file after all), while `git restore $commit $file` would copy the content of said file from another revision only if your don’t have uncommitted changes or to get an error. If it was really what I wanted to do, I would have expected to either do `git restore $file && git restore $commit $file`, directly `git restore --force $commit $file` or something similar.\n\nIs my expectation wrong? Would it be worth considering adding an option in `.gitconfig` to have such behavior?\n\nSincerely,\nRobin.\n\n"},{"id":"423123","messageId":"20210427192906.7obdkopxwajqnv53@gmail.com","threadId":"55571","inReplyTo":"pYZzGPZTHnJjYBKrUAVGcso74I_xJgfzNpSwDN94fhYcDoOamp62-IFvxVrU056uw0txy3MTHYSwny_II0XY4trSY5_E25q7EXwhNHjy3VY=@pm.me","subject":"Re: Request: `git restore $commit $file` shouldn’t override uncommited changes","fromName":"Johannes Altmanninger","fromEmail":"aclopte@gmail.com","sentAt":"2021-04-27T19:29:06Z","receivedAt":"2021-04-27T19:29:11Z","isPatch":false,"sender":{"key":"aclopte@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6853872?v=4"},"body":"> I would have expected that `git restore $commit $file` would copy the\n> content of said file from another revision only if your don’t have\n> uncommitted changes or to get an error.\n\nThe positional arguments to \"git restore\" are files.  So that command will\nerror unless a file called $commit exists.\n(You shell's tab completion should guide you here.)\nYou can use the --source option to specify the commit.\n\n> If it was really what I wanted to do, I would have expected to either do\n> `git restore $file && git restore $commit $file`, directly `git restore\n> --force $commit $file` or something similar.\n\nIs your question that you expect a command like\n\n\tgit restore --source=some-commit some-file\n\nto error if you have uncommitted changes (to \"some-file\")?\nAnd instead you would run\n\n\tgit restore some-file\n\tgit restore --source=some-commit some-file\n"},{"id":"423181","messageId":"xmqqbl9yc1o6.fsf@gitster.g","threadId":"55571","inReplyTo":"pYZzGPZTHnJjYBKrUAVGcso74I_xJgfzNpSwDN94fhYcDoOamp62-IFvxVrU056uw0txy3MTHYSwny_II0XY4trSY5_E25q7EXwhNHjy3VY=@pm.me","subject":"Re: Request: `git restore $commit $file` shouldn’t override uncommited changes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-04-28T07:23:53Z","receivedAt":"2021-04-28T07:24:00Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robin Moussu <moussu.robin@pm.me> writes:\n\n> That’s the first time I’m interacting with the git community,\n> I’m not very familiar with the process. I hope I’m at the right\n> place for a feature request.\n>\n> Currently, I don’t think that it’s possible to get an error when\n> copying the content of a file from another revision into your\n> working tree if said file has uncommitted changes.\n\nYes, \"git restore <from-where> <pathspec>\" is like \"I made a mess in\nthe paths <pathspec> in the working tree and I want to start from a\nknown state, so please take the contents for these paths from\n<from-where> and overwrite the garbage I have in the working tree\".\n\nIt would be a grave regression to stop overwriting by default, as it\nmisses the entire point of the command.\n\nThe same applies to \"git checkout <from-where> -- <pathspec>\".\n\n"},{"id":"423187","messageId":"I_ZK84DfFkUoO9FcqjidSjmsvktNt-k4tPiAGNIP5ztKjk0RQCfFmyRrRHaB414UaWsJO7kWPBcgHlRqAecH7r9mAj0TLm5k6T5_YzmiZ4c=@pm.me","threadId":"55571","inReplyTo":"20210427192906.7obdkopxwajqnv53@gmail.com","subject":"Re: Request: `git restore $commit $file` shouldn’t override uncommited changes","fromName":"Robin Moussu","fromEmail":"moussu.robin@pm.me","sentAt":"2021-04-28T08:35:46Z","receivedAt":"2021-04-28T08:35:58Z","isPatch":false,"sender":{"key":"moussu.robin@pm.me","avatar":"https://gravatar.com/avatar/e6f1710356a7fb672e1ee31e71b2ceafe4d788678de323436463ff3ae47a98fb?d=mp&s=160"},"body":"I effectively did a typo, I meant `git checkout $commit $file` or `git restore -s $commit $file`. I forgot the --source in the `git restore` command.\n\n> Is your question that you expect a command like\n>\n> git restore --source=some-commit some-file\n>\n> to error if you have uncommitted changes (to \"some-file\")?\n> And instead you would run\n>\n> git restore some-file\n> git restore --source=some-commit some-file\n\nExactly. If `--source $commit` isn’t specified, erasing uncommitted changes is what I expect. I scream-up, and want to start from a fresh state.\n\nOn the contrary,  if `--source $commit` is specified, I would like to get an error if $file has uncommitted changes. The reason I want to error when `--source $commit` is specified is because I most probably didn’t screw-up, but just forgot that I modified the file before copying its content from another revision.\n\nRobin.\n\n---\n\nShould I wrap my text in 80 column? I’m not familiar with plain-text netiquette.\n\n"},{"id":"423189","messageId":"xmqqy2d2ai5w.fsf@gitster.g","threadId":"55571","inReplyTo":"I_ZK84DfFkUoO9FcqjidSjmsvktNt-k4tPiAGNIP5ztKjk0RQCfFmyRrRHaB414UaWsJO7kWPBcgHlRqAecH7r9mAj0TLm5k6T5_YzmiZ4c=@pm.me","subject":"Re: Request: `git restore $commit $file` shouldn’t override uncommited changes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-04-28T09:10:35Z","receivedAt":"2021-04-28T09:10:39Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robin Moussu <moussu.robin@pm.me> writes:\n\n> On the contrary, if `--source $commit` is specified, I would like\n> to get an error if $file has uncommitted changes.\n\nNot necessarily.  I've done this number of times:\n\n - start from the current state (HEAD), make changes\n\n - end up making a mess that I'd rather not use.\n\n - realize that the endgame I seek is fairly close to the work I did\n   for another branch.\n\n - \"git checkout $that_branch -- $those_paths\".\n\nSo, it is not cut-and-dried that it _always_ (or even _often_) is a\nmistake to try overwriting a working tree file with modifiations with\na version of a file from commit that is not HEAD.  It may be _always_\nan error for your work habit.  It would almost always be what I want\nin my experience for me.\n\n\n"}]}