INDEXCOMMIT=`echo "" | git commit-tree $INDEXTREE -p HEAD` # create a commit with only the stuff in staging Unfortunately, git doesn't offer a git stash -only-index or similar, so I whipped up a script to do this. That is, to stage some changes, and then stash only those staged changes. I often find it more intuitive to do the inverse of approach. Update () - I've rewritten the script a bit, to better handle the case of conflicts, which should now be presented as unmerged conflicts rather than. git checkout - afile # reset the file to the HEAD content, before any local modificationsĪt the end of that rather cumbersome process, you will have only one or several files stashed.git stash pop # re-apply all your files modifications.git stash # this time, only the files you want are stashed.(Actually, eleotlecram proposes an interesting alternative) If -patch doesn't work, a manual process might:įor one or several files, an intermediate solution would be to: However that will save the full index (which may not be what you want since it might include other files already indexed), and a partial worktree (which could look like the one you want to stash). The selected changes are then rolled back from your worktree. The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively. With -patch, you can interactively select hunks from in the diff between HEAD and the working tree to be stashed. Yet, git stash save -patch could allows you to achieve the partial stashing you are after: (Original answer June 2010: manual stash) In order to get to the original state - which is a clear staging area and with only some select un-staged modifications, one could softly reset the index to get (without committing anything like you - bukzor - did). You should " git reset -soft" after such a stash to get your clear staging back: Go see and upvote his answer, which should be the official one (instead of mine).Ībout that option, chhh points out an alternative workflow in the comments: This (the stash -patch original solution) is nice, but often I've modified a lot of files so using patch is annoyingīukzor's answer (upvoted, November 2011) suggests a more practical solution, based on The original answer (below, June 2010) was about manually selecting what you want to stash. The test case for this feature shows a few more options off: test_expect_success 'stash with multiple pathspec arguments' ' Simplified example: git stash push path/to/file See " Stash changes to specific files" for more. When pathspec is given to ' git stash push', the new stash records the modified states only for the files that match the pathspec # here you can for example check your commit with git hooks, run tests, etc.Since Git 2.13 (Q2 2017), you can stash individual files, with git stash push: git stash push ![]() So to avoid conflict when restoring the stash, you could first reset your current tree (thus removing the staged modifications), and then pop your stash (which will put you back in the initial state, since the index was saved) git stash push -keep-index -include-untracked -m "cleaning_before_git_hook" NB: if 'b' was farther in the code, it would be okay. When adding line 'b' after the stage, git stash pop thinks the modifications are different, and tries bluntly to add 'a' + 'b' on top of 'a' (as 'a' is still on your tree), raising a conflit. If not adding line 'b', git stash pop should add 'a' to 'a' (still there because of the -keep-index), but is smart enough to realize it is the same modification, and does not do anything and does not trigger a conflit. 'git stash' indeed saves the index - even with -keep-index that leaves your staged changes (but saves them at the same time). What I want to happen is for git stash pop -index to recognise that the index is already in the correct state, and to change the working tree files to match the stashed files. ![]() I don't understand why a merge conflict happens in the last step. Why do merge conflicts unexpectedly happen? For example: $ git init testĬONFLICT (content): Merge conflict in file The second sentence in the quoted documentation is confusing to me. However, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally). If the -index option is used, then tries to reinstate not only the working tree’s changes, but also the index’s ones. Git stash pop has an option -index, with this documentation: You can see this by running git log -graph after creating a stash. As far as I'm aware, git stash saves a diff between the working tree and HEAD, it doesn't save the index, the state of the working tree, as well as the index.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |