rhombidodecahedron
rhombidodecahedron

Reputation: 7922

Syncing a very out-of-date github repository

I have a massive github repository that has fallen badly out of sync. I have about 100 MB of source files (that should be pushed) and ~10 TB of data (that should not be pushed).

I have done

git add .
git commit -m 'Resync'

but then I noticed many files that should not be pushed have been added to the commit. I added these files to my .gitignore file, but now when I do those above lines again, it seems that nothing is added except the new .gitignore file.

I think I am supposed to do

git rm -r --cached .

but I am terrified of losing all of my work in the last year. What am I supposed to do?

Upvotes: 1

Views: 261

Answers (2)

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

Because you have locally committed things, and are worried about losing work, start by creating a tag for the "bad" commit. When all is corrected you can delete the tag, but in the mean time you can always get back to where you are.

git tag bad_commit
# try some things, but oops, the work tree ends up corrupted
git checkout bad_commit
# carry on

That's just a safety net; the odds are nothing you're going to do will cost you your local state anyway. But precautions against the odd condition you forget to think about never hurts.

To that end: you also might want to make sure that the commit contains everything about your local state.

git status --ignored

should report "nothing to commit, working tree clean" and should not list any files as "ignored".

Which is counter-intuitive, because you have files in gitignore. As you've discovered, git's ignore rules only apply to untracked files, so it isn't ignoring the files it already knows about (even though they match gitignore patterns).

If it's ok to have those files added to the repo and then deleted in the subsequent commit, then you can

git rm --cached files.that.should.have.been.ignored

and because of the --cached keyword it will not affect your worktree copies. These files will become untracked in the current staging area (for the next commit) and so the ignore rules will filter them out.

Or if you want to clean it up "like it never happened", as long as you haven't pushed the bad commit, you can use git reset

git reset --mixed HEAD^

(assuming the bad commit is the most recent one). Note I'm recommending a mixed reset, because this will leave your work tree alone. From here you would re-stage what should be committed (including the .gitignore file) and proceed from there. And actually since no changes are staged and the .gitignore is present

git add .

should work this time (though using git status to double-check is a good idea).

Another option is to do a soft reset instead of a mixed one, in which case your previous staging work will remain in effect / ready to commit again. In that case you'll have to git rm --cached the files you don't want to commit. (And you still need to make sure the ignore file is staged.)

Upvotes: 2

Schleis
Schleis

Reputation: 43700

If you haven't pushed the commit yet. You can undo it via git reset HEAD~. This will undo the commit and you can add the files properly.

Upvotes: 1

Related Questions