git-stash更改而无需还原 [英] git-stash changes without reverting

查看:90
本文介绍了git-stash更改而无需还原的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Git进行项目.我偶尔会发现自己想要保存所做的更改(不提交更改),作为备份,然后继续工作.我通常要做的是 git stash ,然后立即 git stash apply 使代码回到与存储之前相同的状态.我的问题是git stash还原了我的工作目录,因此即使我立即应用stash也必须重建文件和项目,因为它们似乎已更改.这很令人讨厌,因为我们的某些项目需要很长时间才能构建.

I work on a project using Git. Every once in a while I find myself wanting to save my changes, without committing them, just as a backup, and then continue working. What I usually do is git stash and then immediately git stash apply to bring the code back to the same state as before stashing. The problem I have with this is that git stash reverts my work directory so even though I apply the stash immediately files and projects have to be rebuilt because they appear to have changed. This is quite annoying because some of our projects take ages to build.

所以我的问题是,有没有办法隐藏我的更改而不恢复?如果stash不能执行此操作,那么git中是否还有其他命令可以执行此操作?谢谢.

So my question is, is there a way to stash my changes without reverting? If stash doesn't do that, is there any other command in git that can do it? Thank you.

推荐答案

当我发布此问题时,我是git的新手,并不完全了解它的功能.现在我意识到隐藏不是我所需要的,而且git的本地分支可以更好地完成这项工作.

When I posted this question I was new to git and didn't understand its power in full. Now I realize that stashing is not what I needed and that git's local branches do the job much better.

假设您在main_branch上,您想使其免受实验性更改的影响.

Assume you are on main_branch, which you want to keep clean from experimental changes.

只需创建一个新分支即可存储您的实验更改.

Simply create a new branch in order to store your experimental changes.

git checkout -b temp_branch

假设您进行了一些更改并希望保存进度.只需提交,无需担心,一切都在temp_branch上:

Assume you do some changes and want to save your progress. Simply commit, nothing to worry about, it's all on the temp_branch:

git commit -a -m "first change"

假设您还要进行一些更改,并希望再次存储:

Assume you do some more changes and want to store again:

git commit -a -m "second change"

最后,假设您对实验性更改感到满意,并希望将其合并到主分支.有两种情况:

Finally, assume you are happy with your experimental changes and want to merge them to the main branch. There are two cases:

1)如果要合并所有更改,请执行以下操作:

1) If you want to merge all changes, do:

git fetch . temp_branch:main_branch

这会将temp_branch的所有更改都放入main_branch中,而无需切换到main分支,这意味着您的文件没有被修改,不需要重新编译.请注意,只有在此期间没有对main_branch进行任何其他更改时才有可能.如果main_branch已更改,则需要使用 git merge git rebase ,但是这种情况超出了问题的范围.

This brings all changes of temp_branch into the main_branch, without switching to the main branch, which means your files are not modified and no recompilation is required. Note it's possible only if you haven't done any other changes on main_branch in the meantime. If main_branch has changed you need to use git merge or git rebase, but this scenario is beyond what the question is asking.

2)假设您只想将temp_branch的部分提交合并到main_branch中.您可以使用 git cherry-pick 进行此操作.首先执行 git checkout main_branch 切换到main_branch(这会修改文件,但这是不可避免的,因为要删除某些更改),然后执行 git cherry-pick< SHA> (其中< SHA> 是要合并的提交的哈希值).您可以通过执行 git log temp_branch 来查看提交列表.请注意,仅合并某些更改可能会产生需要解决的冲突.

2) Assume you want to merge only some of the commits of temp_branch into main_branch. You can do this with git cherry-pick. First do git checkout main_branch to switch to the main_branch (this modifies the files but this is inevitable since you are dropping some of your changes), then do git cherry-pick <SHA> (where <SHA> is the hash of the commit you want to merge). You can see the list of commits by doing git log temp_branch. Note that merging only some of the changes might give conflicts which you 'll need to resolve.

这篇关于git-stash更改而无需还原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆