Recovering from forgetting to .gitignore

后端 未结 2 2006
心在旅途
心在旅途 2020-12-19 01:21

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 hav

2条回答
  •  独厮守ぢ
    2020-12-19 02:24

    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 , 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 && git commit -a on a separate commit branch, then merging this topic branch in all branches (and then recovering file from a tracked version).

提交回复
热议问题