Git 切换到 master 并带上未跟踪的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5053679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git switch to master and bring untracked files along
提问by Christian Schlensker
While I was working on a feature_branch I ended up fixing a bug for the whole site. I want to commit the bug fix to master, not my feature_branch. The fix involved adding some new files so when I try to checkout master it aborts and warns me that the untracked files would be overwritten.
当我在处理 feature_branch 时,我最终修复了整个站点的错误。我想将错误修复提交给 master,而不是我的 feature_branch。修复涉及添加一些新文件,因此当我尝试结帐主文件时,它会中止并警告我未跟踪的文件将被覆盖。
How can I switch to master and bring untracked files with me?
如何切换到 master 并随身携带未跟踪的文件?
回答by Jeremy Salwen
Something's not right. Normally when you switch branches the untracked files are brought along with you without any notice. That's why they're called "untracked". However, you say that the untracked files would be overwritten. This implies that you have created an untracked filein the new branch which already exists in the master branch. This means that you have to decide what you want to do: which one to keep. This isn't a matter of getting git to do something, it's a matter of you not being clear in what you want it to do.
有什么不对的。通常,当您切换分支时,未跟踪的文件会在没有任何通知的情况下随身携带。这就是为什么它们被称为“未跟踪”的原因。但是,您说未跟踪的文件将被覆盖。这意味着您已经在主分支中已经存在的新分支中创建了一个未跟踪的文件。这意味着您必须决定要做什么:保留哪一个。这不是让 git 做某事的问题,而是你不清楚你想要它做什么的问题。
Assuming you want to somehow resolve the conflict between the two copies of the same file, you can do
假设您想以某种方式解决同一文件的两个副本之间的冲突,您可以这样做
git checkout feature_branch
git stash
git checkout master
git stash pop
And then resolve the conflicts that will arise.
然后解决将出现的冲突。
回答by Greg Hewgill
One approach to this problem is to stash your changes, then restore them on master.
解决此问题的一种方法是隐藏您的更改,然后在 master 上恢复它们。
- Make sure you've committed everything else that's unrelated to this bug fix
- Run
git stash save "bug fix for master"
git checkout master
git stash pop
- Resolve any conflicts resulting from the stash pop
- Commit this fix to master
- Switch back to your feature branch
- 确保您已提交与此错误修复无关的所有其他内容
- 跑
git stash save "bug fix for master"
git checkout master
git stash pop
- 解决由 stash pop 导致的任何冲突
- 将此修复提交给 master
- 切换回您的功能分支
回答by brianjhong
Easiest solution i can think of is make a local copy of your untracked/modified files. Then checkout your master and reupload and add commit from there. If there are any conflicts, git will notify you and you can merge at that point.
我能想到的最简单的解决方案是制作未跟踪/修改文件的本地副本。然后检出你的主人并重新上传并从那里添加提交。如果有任何冲突,git 会通知你,你可以在那时合并。