Stash changes while keeping the changes in the working directory in Git

时光总嘲笑我的痴心妄想 提交于 2020-03-17 03:41:29

问题


Is there a git stash command that stashes your changes, but keeps them in the working directory too? So basically a git stash; git stash apply in one step?


回答1:


For what it's worth, another way to do this is to stage the changes you want to keep, and then stash everything using --keep-index:

$ git add modified-file.txt
$ git stash save --keep-index

The commands above will stash everything including modified-file.txt, but it will also leave that file staged and in your working directory.

From the official Linux Kernel Git documentation for git stash:

If the --keep-index option is used, all changes already added to the index are left intact.




回答2:


git stash and then git stash apply (git stash && git stash apply) will stash files and apply stash immediately after it. So after all you will have your changes in stash and in working dir.

You can create an alias if you want it in one piece. Just put something like this to ~/.gitconfig:

[alias]
    sta = "!git stash && git stash apply"

The drawback of this approach is that all files are stashed and recreated. This means that timestamps on the files in question will be changed. (Causing Emacs to complain when I try to save the file if opened it before I did the git sta, and may cause unnecessary rebuilds if you're using make or friends.)




回答3:


A small enhancement in the answer which in practical may likely to use.

$ git add modified-file.txt  
(OR $ git add .    ---- for all modified file)
$ git stash save --keep-index "Your Comment"



回答4:


There's a trick may help you, not stash' thing but FWIW:

git add -A
git commit -m "this is what's called stashing"       (create new stash commit)
git tag stash                               (mark the commit with 'stash' tag)
git reset HEAD~        (Now go back to where you've left with your working dir intact)

And so now you have a commit tagged stash at your disposal, it's not possible to do a git stash pop anyway but you can do things like creating patch or resetting files etc. from there, your working dir files are also left intact BTW.




回答5:


You can use git stash create to create a stash commit, and then save it to the stash using git stash store:

git stash store $(git stash create) -m "Stash commit message"

This can be saved to a git alias to make it more convenient:

git config --global alias.stash-keep-all '!git stash store $(git stash create)'

git stash-keep-all -m "Stash commit message"

Note that this does not do everything that git stash push does. For instance, it does not append the branch name to the commit, e.g. "stash@{0}: On myBranch: Stash commit message".



来源:https://stackoverflow.com/questions/17843384/stash-changes-while-keeping-the-changes-in-the-working-directory-in-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!