Delete large files from history / completely delete history locally and then drag and drop it back to its original position
I am completely new to git and am completely lost. I'm sure I missed something basic.
The problem is this:
- I committed and pushed large files (~ 2Gb) to the repository. Then I deleted them - committed - clicked, but they are still in history. I need to shrink the repository somehow.
- First, I followed how to compress a git repository . I could only reduce the local version, but when I tried to push it said I need to pull it first. However, the attraction would bring big history files.
-
Like undo multiple git commits , I completely removed the last two commits by doing twice
git reset --hard HEAD^ git push -f origin master
-
Now I don't see the last two commits in the git log, but the repository / history is still large.
- Since the files are no longer in history, I cannot delete the deleted files from git history as git does not see them.
Let's say I am the only user of the repository and history is irrelevant, I just need to shrink the repository somehow . However, I can make changes locally and then push them into the repository, hence completely deleting the repository won't work as
rm -rf .git
removes git and I can't push.
How bad did 3 do? What can I do to achieve my common goal? Any suggestions would be appreciated. Thank you very much in advance.
source to share
Only if (make sure you agree with each brand below)
- history doesn't matter
- nobody depends on the remote repository,
- all the files that you want to keep in the working tree (but not necessarily in the repo) are currently in the working tree (and not buried in any commit),
- you want to start over from a new repo locally and then push that repo to the remote,
do the following. Delete the folder .git
in your project directory and then initialize Git again.
rm -rf .git
git init
Add the remote to the repo config:
git remote add origin <url-of-remote-in-question>
Take a picture:
git add <at-your-discretion>
git commit -m "initial commit"
Then push your new branch master
on the remote:
git push -f origin master
Edit . If this last push command fails, it should mean that your remote repo is set to refuse unauthorized merges. See "Regenerating the original forge" gives a "refusal of immediate refs / heads / master transitions" "error
source to share
@Jubobs Just to summarize, to make the above solution work, I had to change some of the remote repo settings. Specifically, I had to allow the following operation
git push -f origin master
firstly ssh the remote repo and then setting there
git config receive.denyNonFastforwards true
Alternatively, to take the place of the obsolete files on the remote repo, run
git gc --aggressive --prune=all
A natural question arises: isn't it easier to (1) delete the remote repo, (2) delete the .git folder, and (3) run git init to initialize the new repo. Well, that's also a solution.
source to share