3

I am absolutely new to git and got completely lost. I am sure I miss something basic.

The problem is as follows:

  1. I committed and pushed large files (~2Gb) to a repository. Then I removed them - committed - pushed, but they are still in the history. I need to shrink the repository somehow.
  2. First, I followed how to shrink a git repository. I could only shrink the local version, but when I tried to push, it said that I have to pull first. Pulling, however, would bring the big history files back.
  3. Similar to revert multiple git commits, I removed completely the last two commits by doing twice

    git reset --hard HEAD^
    git push -f origin master
    
  4. Now, I can not see the last two commits in git log, but the repository/history is still big.

  5. As the files are not in the history anymore, I can not remove deleted files from git history, as git does not see them.

Say, I am the only user of the repository and the history of no value, I just need to shrink the repository somehow. However, I can only perform changes locally and then push them to the repository, hence, fully deleting the repository will not work, as

    rm -rf .git

removes git and I won't be able to push.

How bad was doing 3? What can I do to achieve my overall goal? I would appreciate any suggestions. Many thanks in advance.

Community
  • 1
  • 1
Anastasia
  • 93
  • 5
  • When you write "Then I removed them", do you mean with `git rm` or with plain `rm`? – jub0bs Aug 15 '14 at 21:06
  • The files were removed using ordinary rm, but they are still stored in the history files. – Anastasia Aug 18 '14 at 16:59
  • Hey Jubobs, actually, I did not solve the problem yet. I described the problem I still have in the comment to your answer below. I would appreciate any help. – Anastasia Aug 18 '14 at 17:05

2 Answers2

1

@Jubobs Just to sum up, to make the solution above work I had to change some settings of the remote repo. In particular, I had to allow the following operation

git push -f origin master

by, first, ssh the remote repo and then setting there

git config receive.denyNonFastforwards true

In addition, to clim the space from the obsolete files on the remote repo, run

git gc --aggressive --prune=all

A natural question arises: isn't it easier to (1) ssh the remote repo, (2) remove .git folder, and (3) run git init to initialize a new repo. Well, it is also a solution.

Anastasia
  • 93
  • 5
0

Only if (make sure you agree to each bullet point below)

  • the history is of no value,
  • nobody else than you depends on the remote repository,
  • all the files you wish to keep in your working tree (but not necessarily in the repo) are currently in your working tree (and not buried in some commit),
  • you want to start afresh with a brand new repo locally and then push that repo to remote,

do the following. Delete the .git folder in your project directory, then initialise Git again.

rm -rf .git
git init

Add your remote to the repo's config:

git remote add origin <url-of-remote-in-question>

Take a snapshot:

git add <at-your-discretion>
git commit -m "initial commit"

Then force push your brand new master branch to the remote:

git push -f origin master

Edit: If that last force push command fails, it must mean that your remote repo is configured to deny non-fast-forward merges. See Source Forge repo gives "denying non-fast-forward refs/heads/master" error

Community
  • 1
  • 1
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • Thanks for the detailed response. Actually, it did not resolve my problem. In fact, when doing the last command (git push) I received the following error: – Anastasia Aug 18 '14 at 17:01
  • remote: error: denying non-fast-forward refs/heads/master (you should pull first) To ! [remote rejected] master -> master (non-fast-forward) error: failed to push some refs to '' – Anastasia Aug 18 '14 at 17:02
  • And when I tried to pull again afterwards, it pulled all my big files back. Any suggestions? – Anastasia Aug 18 '14 at 17:03
  • If you're the only owner of the remote repo and don't need the remote `master` branch, try following the procedure outlined above but, before doing the final push, do a `git push -f origin :master` (note the `:`); that should delete the `master` branch that resides in the remote repo. Then, if everything is fine, try to push your local `master` branch to the remote: `git push origin master`. When you've done that, report here. – jub0bs Aug 18 '14 at 18:33
  • As I said, I am the only user of the remote repo and there is no need in the current history/master/other git files. I tried `git push -f origin :master` but I lead to the following error – Anastasia Aug 19 '14 at 10:15
  • remote: error: By default, deleting the current branch is denied, because the next remote: error: 'git clone' won't result in any file checked out, causing confusion. remote: error: remote: error: You can set 'receive.denyDeleteCurrent' configuration variable t remote: error: 'warn' or 'ignore' in the remote repository to allow deleting th remote: error: current branch, with or without a warning message. remote: error: remote: error: To squelch this message, you can set it to 'refuse'. remote: error: refusing to delete the current branch: refs/heads/master – Anastasia Aug 19 '14 at 10:15
  • Then I tried to change this configuration as described here [link](http://danielcsgomes.com/tutorials/how-to-allow-remove-master-branch-from-git/#.U_MBVLySy7M) but I could not ssh with -l root because of `Permission denied (publickey).` Without this option I could only change `sudo -u git_user git config --global` but not `sudo -u git_user git config --system` and as a result I got the error as before. Does it mean that I have some access rights restrictions? Can it be that my ssh key is not set properly? – Anastasia Aug 19 '14 at 10:25
  • Sorry for the late reply. I contacted my system administrator (the repo is a corporate one) and will come back to this post when I find out the problem (it can take several weeks though). – Anastasia Aug 23 '14 at 08:56