Haiyang
Haiyang

Reputation: 1597

Git push doesn't ignore files in .gitignore

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?

Upvotes: 7

Views: 19431

Answers (4)

kevininhe
kevininhe

Reputation: 617

Another option, following @AD7six answer, is to create a new branch with no history, so you can push this branch to the remote server ignoring completely the large file.

You can accomplish this by doing:

git checkout --orphan <name_you_choose_for_orphan_branch>
git commit

And then push your changes with

git push <remote-name> <name_you_choose_for_orphan_branch>

Source: How can I purge all the history and push it

Upvotes: 0

matrik
matrik

Reputation: 114

The solutions here will work but you will lose other changes in the same commit. If you don't want to lose the other changes in the same commit, and you didn't push the commit yet, you have to change HEAD back to a previous commit, add the changed file and amend:

First find which commit you would like to return to:

$ git log --oneline

Then, it's needed to checkout another branch because you cannot change the HEAD in the active branch:

$ git checkout master

Then, change the head of the branch to an older commit:

$ git branch -f <branch_name> <commit_id>

Then, add the changed file to current commit:

$ git add <filename>

Then, amend the change into current commit:

$ git commit --amend --no-edit

That multi-step solution worked out for me to not lose changes and add new changes to a previous commit.

Upvotes: 0

AD7six
AD7six

Reputation: 66349

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'.

Upvotes: 11

atupal
atupal

Reputation: 17240

If you have commited the file . You can cancel the lastest commit bu using:

git reset --soft HEAD^

And then:

git reset filename

And then add the file to .gitignore:

or:

git update-index --assume-unchanged  filename

Upvotes: 2

Related Questions