问题描述
我最近开始研究一个使用Git进行版本控制的项目。我必须修复两个缺陷, Defect1
和 Defect2
。修复 Defect1
和 Defect2
需要更改为 File1
和 File2
分别。这两个文件彼此不相关。
我想在单独的Git分支中处理每个缺陷。我创建了两个分支, fix_Defect1
和 fix_Defect2
,并且希望独立处理它们。修复很复杂,所以我无法完成一个修复并在切换到另一个之前提交它。我观察到,当我从 fix_Defect1
切换到 fix_Defect2
时,对 File1
也会出现(反之亦然)。有没有办法避免这种情况发生?
从Git的帮助中,我找不出任何方法。我也在SO上搜索了合作,并找到和,这与我的问题很接近但有点不同。
我可以在一个单独的目录中克隆每个分支的存储库的多个副本,但似乎我会因此而错过Git分支的全部功能,除了浪费磁盘空间。你能否提出一个处理这种情况的好方法?
你的问题源于你改变工作目录中的文件(例如 file1
和 file2
)但不提交或存储这些更改。最好的解决方案是在您想要切换分支时提交更改:
git add file1 file2;#add'已修复
git commit;#提交正在进行的工作
git checkout fix_Defect2;#现在检出其他分支
当然,厨房水槽提交,包括正在进行的工作和可能破碎的代码,对您的历史来说并不是特别好。您可以随时使用 git rebase
清理历史记录。有关详细信息,请参阅或为背后的哲学。
最后,如果你不不喜欢提交工作的想法,您还可以使用 git stash
来得到类似的结果:
git stash;#当你准备好切换
时git checkout fix_Defect2;#在这个分支上工作一段时间
git commit -A;#确保在'fix_Defect2'
提交你的工作
git checkout fix_Defect1;#检出原始分支
git stash pop;#重新应用你从工作目录中储存的工作
一般来说,我不推荐存储解决方案。藏起来的机器不是非常强大,而且更适合需要修补某些东西的情况,而不是平衡两个同样复杂的功能分支。
有关更多详细信息,请查看在文档。
I recently started working on a project which uses Git for version control. I have to fix two defects, Defect1
and Defect2
. Fixes for Defect1
and Defect2
need changes to File1
and File2
respectively. These two files are unrelated to each other.
I want to work on each defect in a separate Git branch. I created two branches, fix_Defect1
and fix_Defect2
, and want to work on them independently. The fixes are complex, so I cannot complete one fix and commit it before switching to the other. I observe that when I switch from fix_Defect1
to fix_Defect2
, any changes made to File1
also appear (and vice versa). Is there some way to avoid this from happening?
From the Git help, I could not figure out any way. I also searched on SO for git work with multiple branches, and found Git and working on multiple branches and Using git with multiple branches at once, which are close but somewhat different from my question.
I can clone multiple copies of the repository per branch in a separate directory, but it seems like I would be missing the full power of Git branching by doing so, besides wasting disk space. Could you please suggest a good approach to handle this scenario?
Your problem arises from the fact that you're changing files in the working directory (e.g. file1
and file2
) but are not committing or stashing those changes. The best solution is to commit the changes when you feel like switching branches:
git add file1 file2 ;# add whichever ones you've fixed
git commit ;# commit the work in progress
git checkout fix_Defect2 ;# now checkout the other branch
Of course, kitchen sink commits, those that include work in progress and possibly broken code, aren't particularly good for your history. You can always use git rebase
to clean up your history later. See, for example, this answer for details or this answer for the philosophy behind it.
Finally, if you don't like the idea of committing your work, you can also use git stash
to get something like the same result:
git stash ;# when you're ready to switch
git checkout fix_Defect2 ;# work on this branch for a while
git commit -A ;# be sure to commit your work on 'fix_Defect2'
git checkout fix_Defect1 ;# checkout the original branch
git stash pop ;# reapply the work you stashed from the working directory
I don't recommend the stashing solution, generally. The stashing machinery isn't terribly robust, and is more designed for situations where you need to hotfix something, not balance two equally complex feature branches.
For more details, check out the "File Status Lifecycle" diagram in the git-scm docs.
这篇关于在不同的git分支上处理两个不相关的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!