It can be easier than amending or interactively rebasing. If I realize I committed a file I shouldn't have, I do a soft reset and then redo the commit properly. More generally, git reset -soft HEAD^ is great for undoing non-pushed commits. Just remember not to share the branch with a WiP commit or someone else may build upon it and object when you rewrite history. This will leave the working tree in the WiP state, but roll back the current HEAD one commit, as though you never made that commit. When I get back to a branch, find that the last commit is "WiP" and want to restore my previous state, I do git reset -soft HEAD^ (Idea: make the shell prompt indicate stash.) Instead, when I have work in progress, I just commit it as usual, with something like "WiP" as the message: git commit -a -m "WiP" However, I dislike having to remember I have something stashed. So the stash apply got a conflict.With Git, if you have some work in progress and find you need to switch to another incompatible branch, you can git stash the changes and later restore them with git stash pop. For example, maybe you started with a file this The stash can't be reverse-applied as a patch because of the conflict markers. You'd still have to do something like reverse-patching for files that were modified in both places without conflict. To see if the file contains local modifications. To see if the stash modified a file, and something like git diff stash - path/to/file Of you could handle it file by file, using git stash show -name-only Then you would resolve the conflicts with checkout -ours, and reverse-apply the rest of the patch. There are two ways to go about fixing this:īuilding on what you were trying to do before, you could store the patch from the stash and remove the entries for files that were conflicted. To push a new stash onto your stack, run git stash or git stash push: git stash Saved working directory and index state \ 'WIP on master: 049d078 Create index file' HEAD is now at 049d078 Create index file (To restore them type 'git stash apply') You can now see that your working directory is clean: git status On branch master nothing to. Ok, so cases 1 and 2 take no action, and 4 is easy to find and resolve. It again looks pretty much like cases 2 and 3, and to fix it you need to separate your changes from those added by the stash. automatic merge resolution combined the changes successfully). You can restore your version by saying git checkout -ours - file/with/conflicting/changesĥ) You had applied local changes to the file, and the stash applied changes to the same file, but the changes are not in conflict (i.e. In this case, the conflict-marked version of the file is in the work tree. This looks pretty much like case 2, so knowing the difference is the first challenge (see below) once you identify a file in this state, you could revert this file to the previously-committed version (but might not want to just yet again, see below) git checkout HEAD - file/with/only/stashed/changesĤ) You had applied local changes to the file, and the stash applied conflicting changes to the same file. The index contains the file as modified by the stash. Your locally-changed version is in the index, and you can leave this file alone.ģ) You had not applied local changes to the file, and the stash did apply changes to it. Nothing to see here.Ģ) You applied local changes to the file, and the stash did not apply changes to the file. So now there are (at least) five possible states for each file:ġ) Neither your local changes nor the stash applied changes to the file. It can be a bit of a pain when there are conflicts, though, because now it updates the index for conflict resolution. It requires that the work tree match the index, and it is only expected to write to the work tree, so it would seem easy to undo. Generally git stash apply is a fairly safe command. I'll go into more detail about that at the end if you want to dig into that, but more importantly: what to do instead? The problem with reverse-applying the stash is due to the merge conflicts.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |