How to revert changes to the working directory only

I have a broken merge, and I want to return the changes to the working directory to find out where it is broken. I want to keep the index so that I can add corrections to it and ultimately check them. Essentially, I want the working directory to be the same as HEAD, but without changing the index. This will essentially apply the inverse patch based on the index to the working directory. It looks like it will be the opposite of git reset .

How do you do this in git?

+4
source share
5 answers

You can commit the index, reset the working directory, and then reset commit.

 $ git commit -m "blah" $ git reset --hard $ git reset --soft HEAD^ 
+3
source

Have you tried git checkout <commit> ?

Snippet git help checkout :

 git checkout [<branch>], git checkout -b|-B <new_branch> [<start point>], git checkout [--detach] [<commit>] This form switches branches by updating the index, working tree, and HEAD to reflect the specified branch or commit. If -b is given, a new branch is created as if git-branch(1) were called and then checked out; in this case you can use the --track or --no-track options, which will be passed to git branch. As a convenience, --track without -b implies branch creation; see the description of --track below. If -B is given, <new_branch> is created if it doesn't exist; otherwise, it is reset. This is the transactional equivalent of $ git branch -f <branch> [<start point>] $ git checkout <branch> that is to say, the branch is not reset/created unless "git checkout" is successful. 
+2
source

Reverse patch can be generated for existing commits, and not for changes in the index.

  • Commit changes to the index [may be in the diff section].
  • Run git revert -n <commit>

It will create a reverse patch in your working directory for your commit.

+2
source

You are probably looking for git stash . With git stash you can make the working directory look like HEAD, and you can do anything else when you are in this state. Once you're done, you can git stash pop and revert all your changes. You must make sure that you do not change files that were hidden, although otherwise you must resolve merge conflicts.

+1
source

I looked at a simple team doing the same thing, but it is not. Although you pointed out the correct way to do this in your question:

This will significantly apply the index-based reverse patch to the working directory.

So the answer is pretty simple:

 git diff --no-color --staged | git apply --reverse 

So you don’t have to do anything, just apply a backlink to your index.

Please note that if some changes in the working directory are not placed / in the index, they will not be undone in this way.

If you want to return everything to the working directory (even changes, not indexed and new files), except for the index itself, do the following:

 git stash save --include-untracked --keep-index 

You can remove it after that if you are not going to reuse it:

 git stash drop 

If you want to return everything except files without a trace (new files are not in the index)
Do it before:

 git checkout . 

To summarize, a few bash functions:

 function git-unapply-index { git diff --no-color --staged | which git apply --reverse } function git-revert-workdir-keep-untracked { git checkout . git-unapply-index } function git-revert-workdir-all { git stash save --include-untracked --keep-index git stash drop git-unapply-index } 
+1
source

Source: https://habr.com/ru/post/1443092/


All Articles