git Git从分支中删除一个文件,将其保存在master中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37422221/
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 remove a file from a branch, keep it in the master
提问by Eugenio
I create a new branch
我创建了一个新分支
git checkout -b mybranch
then I delete a file from it
然后我从中删除一个文件
git rm --cached myfile.txt
but I want to keep it in the master; why when I checkout to the master
但我想把它留在主人那里;为什么当我结帐给主人时
git checkout master
I get an "error: The following untracked working tree files would be overwritten by checkout: file.txt" and if I force the checkout
我收到“错误:以下未跟踪的工作树文件将被结帐覆盖:file.txt”,如果我强制结帐
git checkout master -f
the file is deleted from the file system?
该文件是否从文件系统中删除?
I am sure I am missing something, but I just wanted to remove the file from a branch and not from the master while it seems that git wants to merge the branch when I checkout master.
我确定我遗漏了一些东西,但我只是想从分支而不是从 master 中删除文件,而当我结帐 master 时,git 似乎想要合并分支。
The reason why I used git rm
and not gitignore
was that the file was already been committed.
我使用git rm
而不是的原因gitignore
是文件已经提交。
采纳答案by Jeff Puckett
If you want to keep tracking myfile.txt on master but deleted from mybranch, then you simply need to delete it and commit the delete.
如果您想继续跟踪 master 上的 myfile.txt 但从 mybranch 中删除,那么您只需要删除它并提交删除。
git checkout -b mybranch
rm myfile.txt
git commit -am "delete myfile.txt"
Now when you checkout master, you'll see your file returned and when you checkout mybranch it will be gone again.
现在,当您结帐 master 时,您将看到您的文件返回,而当您结帐 mybranch 时,它会再次消失。
Caution: if you merge mybranch into master, it will be deleted on master then too.
注意:如果你将 mybranch 合并到 master,它也会在 master 上被删除。
回答by Rik
git will see the file as untracked because it is not committed yet. You have to commit the change before you switch branches.
git 会将文件视为未跟踪,因为它尚未提交。您必须在切换分支之前提交更改。
So you created a branch and made changes to that branch. When you try to switch back to master
git will disallow this, because there are changes that are not committed yet (or explicitly ignored; (that is actually what the untracked
means)
因此,您创建了一个分支并对该分支进行了更改。当您尝试切换回master
git 时将不允许这样做,因为有些更改尚未提交(或明确忽略;(实际上就是这个untracked
意思)
The reason the file is restored when you force checkout (git checkout -f master
) master
is because the file isin master
当你强迫结账(该文件被恢复的原因git checkout -f master
) master
是因为该文件是在master
If you want to do this you will have to do the following
如果您想这样做,您必须执行以下操作
Create and check out branch
创建并签出分支
git checkout -b somebranch
Remove the file from somebranch
(--cached
will actually remove the file from the index, but will leave the file intact on your filesystem. See git-rm reference docs)
Remove the file from somebranch
(--cached
实际上会从索引中删除文件,但会将文件完整地保留在您的文件系统上。请参阅git-rm 参考文档)
git rm --cached somefile
Commit the change
提交更改
git commit -m "Remove somefile"
This will remove the file from git, but not from disk, so now to git you have an untracked change (somefile is on disk but not in git)
这将从 git 中删除文件,但不会从磁盘中删除,所以现在到 git 你有一个未跟踪的更改(某个文件在磁盘上但不在 git 中)
If you want other contributors to be able to checkout the branch, link it to a remote
如果您希望其他贡献者能够签出分支,请将其链接到远程
git push --set-upstream origin somebranch
Now the file is removed from origin/somebranch
and not master
现在文件被删除了,origin/somebranch
而不是master
When you add a file to .gitignore
it is not removed from the repo if it is already committed. Changes to the file, howeve, will be ignored (so also deleting it). If you do that you still have to remove the file and commit. After that git will not track the file anymore when recreated (or any other action on the file).
如果文件.gitignore
已经提交,则当您向其中添加文件时,不会从存储库中删除该文件。但是,对文件的更改将被忽略(因此也将删除它)。如果这样做,您仍然必须删除文件并提交。之后 git 在重新创建(或对文件的任何其他操作)时将不再跟踪文件。
回答by Charles Durham
Commit the change on mybranch before swapping back to master.
在交换回 master 之前提交 mybranch 上的更改。