Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git push doesn't ignore files in .gitignore

Tags:

git

github

I committed some files that I shouldn't commit. Then I added these files in .gitignore and I did this:

git rm -r --cached somefile

and I committed again.

git status shows:

# On branch experiment
nothing to commit, working directory clean

I do git ls-files and these files are not listed.

But when I do git push origin somebranch, these files are still being pushed (they're too large to push to github so it fails).

why is this? what should I do?

like image 780
Haiyang Avatar asked Dec 20 '22 19:12

Haiyang


1 Answers

The files are still in your git history. From what's in the question the git history is for example:

$ git log --oneline
aaaaaaa Update .gitignore and remove big files
bbbbbbb accidentally add big files to repo
ccccccc update foo
ddddddd update bar

And pushing fails because of pushing the contents of commit bbbbbbb, it doesn't matter that the file is not in your currently checked out version it's in the project history.

The above can also be confirmed by checking for the history of a specific file/path:

$ git log --all --format=%H -- big.file
aaaaaaa Update .gitignore and remove big files
bbbbbbb accidentally add big files to repo

If big.file is large enough to prevent pushing to github - you need that list of commits to be empty

Obliterating a file from history

There are a number of ways to prevent attempting to push big.file to the remote, but based on the info in the question the simplest of which is:

$ git reset --hard ccccccc

This will remove commits aaaaaaa and bbbbbbb thus there will be no attempt to push big.file to the remote.

The purpose of .gitignore

.gitignore files affect adding files to the repository, they do not affect files already in the index. This is the reason that adding files to .gitignore has had no effect on 'the problem'.

like image 150
AD7six Avatar answered Dec 22 '22 09:12

AD7six