Git 中分支之间未跟踪的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2187000/
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
Untracked files between branches in Git
提问by Clarence
I've been searching around here for to look for an answer and it seems I may just be making incorrect assumptions on how git branches are supposed to work.
我一直在这里四处寻找答案,看来我可能只是对 git branch 应该如何工作做出了错误的假设。
I have my master
branch and I've created a feature branch called profiles
where I'm doing some specific work to profiles. While working on profiles I've changed 5 or 6 files and added another 5 or 6 new files. I needed to switch back to the master
branch to fix a quick bug and noticed all the new files and modified files where in there as well. I guess this makes sense since git isnt going to remove untracked files from the master
branch and bring them back for my profiles
branch since they are, in fact, untracked. But what about the changes to existing files. Why are they showing up in the master branch.
我有我的master
分支,我创建了一个功能分支,称为profiles
我正在对配置文件做一些特定的工作。在处理配置文件时,我更改了 5 或 6 个文件并添加了另外 5 或 6 个新文件。我需要切换回master
分支来修复一个快速错误,并注意到所有新文件和修改过的文件也在那里。我想这是有道理的,因为 git 不会从master
分支中删除未跟踪的文件并将它们带回我的profiles
分支,因为它们实际上是未跟踪的。但是对现有文件的更改呢?为什么它们会出现在 master 分支中。
Whats the best practice here. I'm not ready to commit the changes locally yet. Should I just be stashing all these changes, switch to master
, make the small fix, switch back to profiles
then reapply the stash?
这里的最佳做法是什么。我还没有准备好在本地提交更改。我是否应该只是隐藏所有这些更改,切换到master
,进行小修复,切换回profiles
然后重新应用存储?
Any help is appreciated. Thanks
任何帮助表示赞赏。谢谢
回答by Randal Schwartz
"I'm not ready to commit the changes locally yet."
“我还没有准备好在本地提交更改。”
Commits in git are local things that can be undone, redone and re-re-done at will. It's only when you push the commit somewhere that you need to pay attention.
git 中的提交是可以随意撤消、重做和重新做的本地事物。只有当你将提交推送到你需要注意的地方时。
Plus, commits are visible to local tools like gitk
, and can have diffs taken of them and can be rebased onto other commits, etc. It's a very powerful tool. Learn to use it.
另外,提交对本地工具是可见的,比如gitk
,并且可以获取它们的差异,并且可以重新基于其他提交等。这是一个非常强大的工具。学习使用它。
I frequently do:
我经常这样做:
git add .; git commit -a -m 'WIP'
just to stash away everything in the current work tree if I think I might be interrupted. If I make a few more changes, I use:
如果我认为我可能会被打断,只是为了隐藏当前工作树中的所有内容。如果我再做一些更改,我会使用:
git add .; git commit --amend
to update my "WIP" commit in-place. When I'm finally ready for the real commit, I just:
就地更新我的“WIP”提交。当我终于准备好进行真正的提交时,我只是:
git reset --soft HEAD~; git reset
and now I can carefully control what the final commit will be.
现在我可以仔细控制最终提交的内容。
回答by Cascabel
They're not showing up in the master branch - if you did a hard reset and a clean, they'd disappear. Git is merely preserving your local modifications when you switch branches.
它们没有出现在主分支中 - 如果您进行了硬重置和清理,它们就会消失。Git 只是在您切换分支时保留您的本地修改。
This is commonly useful; you might have realized you want to commit those modifications to a different branch than the one you're currently on. If the modifications conflicted with the difference between the two branches, git would refuse to switch branches.
这通常很有用;您可能已经意识到要将这些修改提交到与您当前所在的分支不同的分支。如果修改与两个分支之间的差异发生冲突,git 将拒绝切换分支。
You're right about the best approach, though - switching branches cleanly is in my experience one of the most common uses of git stash
.
不过,您对最佳方法的看法是正确的 - 根据我的经验,干净地切换分支是git stash
.
回答by Randall
It sounds like you created the branch with git branch profiles
, but didn't switch to it, so you stayed in master
, and it got the file changes when you did a commit.
听起来您使用 创build了分支git branch profiles
,但没有切换到它,所以您留在了master
,并且在您进行提交时它会更改文件。
After creating the branch, you need to explicitly switch to it with git checkout
(just create new branch on-the-fly and switch to it in one step using git checkout -b
).
创建分支后,您需要显式切换到它git checkout
(只需即时创建新分支并使用 一步切换到它git checkout -b
)。
If you have changes you don't want to lose (or commit to the current branch), but instead put into the other branch, do:
如果您有不想丢失(或提交到当前分支)的更改,而是放入另一个分支,请执行以下操作:
git add -A
git stash
git checkout <other branch>
git stash pop
More info on git stash
is available from git-scm.com
更多信息git stash
可从git-scm.com 获得