在Git中处理临时更改(不提交) [英] Handling temporary changes (not to be committed) in Git
问题描述
关于这些临时更改:
目前我只保留它们作为unstaged,并且在每次提交时手动跳过它们。然而,我不能留在这个解决方案,因为:
- 我必须记住哪些文件需要跳过,
- 有一天,我将最终在一个文件中做出2个更改,其中一个是临时的,一个是提交的,而且会非常麻烦。
我该如何处理它?<b>
gitignore
显然没有问题,因为我不想忽略整个文件,而且我仍然对其他提交者的更改感兴趣(我需要重新分配分支以便从时间到时间)。
我通常使用以下方式处理此问题:
git add -p
...到阶段变化大块逐大块。那么你只需确保按 n 进行调试更改。
如果我有更多涉及这种类型的变化,我会创建一个叫做 local-changes
的分支,并在其提示部分介绍调试代码。在创建几个提交后,我会使用:
git rebase -i master
...重新排列它们,以便再次提示本地更改。然后更新master并返回到本地更改分支,您可以这样做:
git checkout master
git merge local -changes ^
git checkout local-changes
Often while working on a branch I need to introduce some "temporary" changes (such as additional debugging info, or a change which lets me better observe the thing i'm actually working on).
About these "temporary" changes:
- I want them in my working copy of my branch, because they help me to work on the actual change,
- I don't want them committed to the branch, because the branch is going to be merged into master some time and they're not production code.
Currently I just keep them as unstaged and I skip them manually when staging every commit. However I can't stay with this solution because:
- All the time I have to remember which files I need to skip,
- Someday I'm going to end up with 2 changes in one file, one being temporary, one to be committed, and it's going to be really troublesome.
How should I deal with it?
gitignore
is obviously out of the question because I don't want to ignore the whole files and I'm still interested in changes from other committers (I need to rebase the branch to master from time to time).
I typically deal with this by using:
git add -p
... to stage changes hunk-by-hunk. Then you just have to make sure to press n for your debugging changes.
If I have more involved changes of this type, I'll create a branch called something like local-changes
with a commit at its tip that introduces the debugging code. After creating a few more commits, I'd then use:
git rebase -i master
... to reorder them so that the commit with the local changes is at the tip again. Then to update master and return to the local changes branch, you can do:
git checkout master
git merge local-changes^
git checkout local-changes
这篇关于在Git中处理临时更改(不提交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!