2

I accidently ran command git pull <wrong_remote_url> in my repository. During the pull process, I cancel it by click Ctrl+C.

$ du -h -d 1
20M .  # At first the repository is only 20M
$ git status
OK
$ git pull https://example.com/large_repository.git  # accidently pull the wrong repository
press Ctrl + C during the pull  # cancel it during the pull, because I find it is downloading a lot of large file
$ git status
On branch master
nothing to commit, working directory clean
$ du -h -d 0
2.1G .  # Now the repository is too large
$ du -h -d 0 .git
2.1G .git  # There may be a lot of cached and useless file in .git directory

The repository still works, I can still use git as usual and the extra 2G file will not be pushed to repository. But I know there is about 2G cached file in the .git directory. How can I delete these files? I tryied git gc and git clean, but they don't work. For some reason, there is some database data in the repository, so I don't want to delete the repository and clone it again.


I find the tmpfile in .git/objects/pack/ named as tmp_pack_9qTKaE, tmp_pack_CKZSuG, etc.
I think it is the large, useless cached file. It works if I delete them:

rm -f .git/objects/pack/tmp_*

Is there any command in git I can delete these file automatically and safely?

ramwin
  • 5,803
  • 3
  • 27
  • 29

2 Answers2

2

run command

git repack -Ad
git prune

I get this answer from this answer's comment. More documentation can been seen here. After running these two commands, the tmp files in .git/objects/pack have been deleted.

ramwin
  • 5,803
  • 3
  • 27
  • 29
0

Although it is slightly off-topic, I'll add this to the previous answers :

If you're realizing mid-pull you're pulling on the wrong local branch, or pulling the wrong remote one, do NOT interrupt it.

It'll be way easier to let it finish then git merge --abort it.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • Thank you for your advise. I tried your solution, it showed `fatal: refusing to merge unrelated histories`. When I ran `git merge --abort it`, it showed `fatal: There is no merge to abort`. Maybe you misunderstand my situation, I pull the wrong repository not just the wrong branch. – ramwin Jul 24 '18 at 01:35