从遗忘恢复到.gitignore [英] Recovering from forgetting to .gitignore
问题描述
我被git newbies看起来常见的问题困扰了。
我忘了.gitignore一个特定的文件,并将其添加到。 gitignore提交后没有任何区别。
我发现解释了如何从存储库中删除一个文件而不从工作树中删除它(通过使用命令 git rm --cached< file>
,它工作正常,除非如果我然后尝试合并到另一个分支,工作树中的文件被删除。
重现空白文件夹的步骤:
git init
touch want.txt
touch wantnot.txt
git add。
git commit -mInitial
git checkout -b BranchForIgnore
git rm --cached wantnot。 txt
猫> .gitignore
wantnot.txt [Ctrl-D Ctrl-D此处退出猫]
git添加。
git commit -m忽略文件
到这里,一切都很好 p>
git checkout master
git merge BranchForIgnore
此时,我的wantnot.txt文件不再在我的主文件中,显然,检查BranchForIgnore也不会有帮助。
$ b $ 文件,并在提交后将它添加到
.gitignore
没有区别。 那当然不是。忽略未跟踪文件(这意味着文件不受版本控制)。
在gitready上解释了如何从存储库中删除一个文件而不从工作树中删除它(通过使用命令
git rm --cached< file>
好吧,除非如果我然后尝试合并到另一个分支,工作树中的文件被删除。
Git删除文件是因为您可以恢复它,因为它在一个分支中被跟踪。
解决方法是提交untracking文件(从版本中删除文件控制),使用 git cherry-pick
或制作 git rm - 缓存< file>&&&git commit -a
在单独的提交分支上,然后在所有分支中合并这个主题分支(然后从追踪版本恢复文件)。
I have been caught out by what appears to be a common enough problem for git newbies.
I forgot to .gitignore a particular file, and adding it to .gitignore after having committed makes no difference.
I found this page on gitready which explains how to remove a file from the repository without removing it from the working tree (by using the command git rm --cached <file>
, which works ok, except that if I then try to merge that back into another branch, the files in the working tree get deleted.
Steps to reproduce, on an empty folder:
git init
touch want.txt
touch wantnot.txt
git add .
git commit -m "Initial"
git checkout -b BranchForIgnore
git rm --cached wantnot.txt
cat > .gitignore
wantnot.txt [Ctrl-D Ctrl-D here to exit cat]
git add .
git commit -m "Ignoring file"
Up to here, everything is fine
git checkout master
git merge BranchForIgnore
At this point, my wantnot.txt files are no longer in my master, and obviously, checking out BranchForIgnore won't help either.
What to do?
I forgot to .gitignore a particular file, and adding it to
.gitignore
after having committed makes no difference.
Well, of course it doesn't. Ignoring is about untracked files (which means files not under version control).
I found a page on gitready which explains how to remove a file from the repository without removing it from the working tree (by using the command
git rm --cached <file>
, which works ok, except that if I then try to merge that back into another branch, the files in the working tree get deleted.
Git deletes file because you can recover it, as it was tracked in one branch.
The solution would be to commit "untracking" a file (removing file from version control) in all branches, using git cherry-pick
, or making git rm --cached <file> && git commit -a
on a separate commit branch, then merging this topic branch in all branches (and then recovering file from a tracked version).
这篇关于从遗忘恢复到.gitignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!