Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of: git reset --hard --patch?

Tags:

git

Is it possible to do equivalent of git reset --hard --patch? (as this gives me: fatal: --patch is incompatible with --{hard,mixed,soft}).

In other words, how to do git reset --patch, but have the "unstaged" changes immediately discarded? (especially for newly added files - I don't want them littering my working directory, as I have it already quite littered with other stuff... so it's hard to find the new litter to remove it by hand...)

edit: or, maybe something like "multiple staging areas", where chunks can be passed from one to another easily like with --patch?

edit2: This seems not clear from the above question, so I'll copy a disclaimer from a comment I made below: Please note I don't want to have any files which show as "new file" in "Changes to be committed" in output of git status moved to "Untracked files". And for each of them I want to be able to explicitly decide if I want to keep them or discard them permanently (i.e. delete from disk).

In still other words: I'm trying to split a commit in two, but I have some "new files" in it. Some of them I want to move into the "other half" of the commit. But I don't want to have to add them one by one from the list of "Untracked files".

edit3: again to clarify: I have numerous "Untracked files" staying around that I don't want to add to .gitignore or .git/info/exclude for one or other reason.

like image 631
akavel Avatar asked Apr 18 '16 15:04

akavel


People also ask

What is hard reset in git?

Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

Should you use git reset hard?

First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it.

What is git reset -- hard head 5?

To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).

What is the difference between a soft reset git reset -- soft and a hard reset git reset hard?

reset --soft : History changed, HEAD changed, Working directory is not changed. reset --mixed : History changed, HEAD changed, Working directory changed with unstaged data. reset --hard : History changed, HEAD changed, Working directory is changed with lost data. It is always safe to go with Git --soft.


1 Answers

git reset --hard is meant to bring you to a clean state in essentially any situation: it works if you have staged or unstaged changes, if you have conflicts in your index, ... In these situations, a --patch option would not really make sense.

Actually, git reset --hard is usually not a good idea for the end-user: not only it discards changes, but does so in an unrecoverable way. As opposed to this, git stash keeps the changes. It can both be used for "I want to put these changes aside temporarily and I'll get them back soon" or "I know I want to discard these changes permanently, but I'll keep a backup just in case".

As you already noted, git stash has a --patch option, so it's clearly a good solution. An alternative is to use git reset --patch (without --hard) to get your index in the state you want, and then git stash --keep-index to have the work tree match the index.

If the change is large enough that you need to worry about the disk space usage, or if you just don't want to add clutter to your stash list, you can then run git stash drop to remove the stash entry.

like image 153
Matthieu Moy Avatar answered Sep 23 '22 15:09

Matthieu Moy