Git switch to master and bring untracked files along
While I was working on a feature_开发者_JAVA百科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.
How can I switch to master and bring untracked files with me?
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 file in 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.
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.
One approach to this problem is to stash your changes, then restore them on 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
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.
精彩评论