Muhammad Touseef
Muhammad Touseef

Reputation: 4465

accidently added huge file to my git commit now how to remove just that file from my commit

in my visual studio project I have a repo from visual studio team services and I am using git for my version controlling, I made some changes to some files and then also I added a huge video file to my assets, I accidently forgot to add that file extension to my gitignore file and I made a commit locally and then I pushed it.

I had total 13 changes and on the 5th change being pushed ( the video file ) it is taking too long, and I don't want to push that video file in my vsts. I tried to do revert to that commit but it failed, I only want to keep those changes in my files and want to push them but I don't wanna push the video file, now I am stuck, what should I do to achieve this?

Upvotes: 2

Views: 1651

Answers (4)

Muhammad Touseef
Muhammad Touseef

Reputation: 4465

I solved this issue with following steps:

  1. copy the changes files/folders of the project and paste them out of repo locally.
  2. delete the repo from your local system completely.
  3. clone the repo from vsts on your system again.
  4. now replace the files/folders which you copied earlier in first step so that now you have the new ones.
  5. add the extension like ".mp4" and ".mkv" to your gitignore.
  6. commit and push the changes.

Upvotes: 1

Jingnan Jia
Jingnan Jia

Reputation: 1309

Run the following command from the project root directory.

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path/to/your/video' --prune-empty --tag-name-filter cat -- --all

Reference: https://www.deployhq.com/git/faqs/removing-large-files-from-git-history#git39s-codefilter-branchcode-to-the-rescue

Upvotes: 0

torek
torek

Reputation: 490098

Git doesn't push changes, nor does it push files. Git pushes—and fetches, for that matter—commits. The unit of each transaction is some whole number of commits (well, zero is also allowed; perhaps it would be better to say natural number of commits). Now, each commit is a snapshot of files, so files come along for the ride with the commits. But what git push pushes is the commits, not the files. (After pushing the commits, git push also sends a set of "change or create this branch name" requests as well, but we need not worry about that here.)

What you must do, then, is replace your bad series of commits with a series of new, improved commits instead.

For a linear sequence of commits, the easiest way to remove the bad file from the point where you added it onward is to use interactive rebase. There are many other ways to do this, though, and there is a post that covers all of this in great detail in many answers: How to remove/delete a large file from commit history in Git repository? See Greg Bacon's answer in particular to use interactive rebase to fix the problem.

Upvotes: 3

Varun Garg
Varun Garg

Reputation: 2654

Since you have only commited and not pushed yet, try this

git reset --soft HEAD~1
git rm --cached VideoFile.mp4
git commit ...
git push ....

Upvotes: 2

Related Questions