简体   繁体   中英

gitignore not working in spite of removing the files from the cache

I have the following files listed in my .gitignore file, for simplicity sake we'll focus on just one:

latin/files/lasla_db.pkl

That file was never added to my github repository but maybe someone how it got added into the cache or something like that so I ran:

Admins-MacBook-Pro-4:latin bobsmith$ git rm --cached latin/files/lasla_db2.pkl
rm latin/files/lasla_db2.pkl

I then ran the following commands:

Admins-MacBook-Pro-4:latin bobsmith$ git add .
Admins-MacBook-Pro-4:latin bobsmith$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    latin/files/lasla_db.pkl

I then ran the following:

Admins-MacBook-Pro-4:latin bobsmith$ git commit -m 'a'
[master 81b1e23] a
 12 files changed, 9 deletions(-)
 delete mode 100644 latin/files/lasla_db.pkl

I then ran

git push -u origin master

I got two error messages related to large files which I'm not worried about because once I ignore the offending files I assume the problem will go away:

remote: error: Trace: a46785362c02dd013da2eba7570e06c706a9f31357257ef6957b9f39b86c2efe
remote: error: See http://git.io/iEPt8g for more information.

But one I do understand but don't understand why I'm getting it since I'm ignoring that file:

remote: error: File latin/files/lasla_db.pkl is 217.16 MB; this exceeds GitHub's file size limit of 100.00 MB

As the file seems to have been tracked since the second last (non-pushed) commit, we'll reset the local branch to an older commit (the one its on origin).

  1. Define a new local branch or tag based on current state, that way we can find/use those discarded commits later:

    git branch before_reset

  2. Also make sure we don't lose any changes that weren't checked in yet (if any):

    git stash save -u
    (If it says that changes were found/saved, you can restore those changes later with git stash apply

  3. Reset the local branch to origin:

    git reset HEAD@{upstream}

  4. Now you can reapply your changes cleanly.
    The first step you probably want to do is get the version of .gitignore as it was before the reset:

    git checkout before_reset -- .gitignore


How to avoid this situation in future:

When committing I recommend against using -m to specify the commit message.

If you don't use this option, the editor will open and give you a chance to review your changes before finishing the commit.

If you notice anything wrong with the commit (eg files being added that should not be added), save the file without any commit message and the commit will be aborted.


Explanation regarding: "gitignore not working"

gitignore causes git commands to ignore files that are currently not tracked by git.

However if you already have 2 commits in your history, eg

commit 1: adds file
commit 2: deletes file

then gitignore cannot magically make the file disappear from the history. (Because commit contents are basically set in stone)

It will only prevent the file from being added again.

If you try to push your local branch, commit 1 and commit 2 are part of the history. And because they contain the file (once as add, once as delete) you got the size error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM