202

I had the same question as asked here: New git repository in root directory to subsume an exist repository in a sub-directory

I followed this answer here: New git repository in root directory to subsume an exist repository in a sub-directory

Now, gitk --all shows two histories: one culminating in the current master, and one named original/refs/heads/master.

I don't know what this second history is, or how to remove it from the repo. I don't need two histories in my repository.

How do I get rid of it?

To reproduce yourself:

mkdir -p project-root/path/to/module
cd project-root/path/to/module
mkdir dir1 dir2 dir3 
for dir in * ; do touch $dir/source-file-$dir.py ; done
git init
git add .
git commit -m 'Initial commit'

Now we have the original poster's problem. Let's move the root of the git repo to project-root using the answer linked above:

git filter-branch --tree-filter 'mkdir -p path/to/module ; git mv dir1 dir2 dir3 path/to/module' HEAD
rm -rf path
cd ../../../ # Now PWD is project-root
mv path/to/module/.git .
git reset --hard

Now, see my current problem:

gitk --all &
git show-ref

How do I get rid of refs/original/heads/master and all associated history?

Community
  • 1
  • 1
goofeedude
  • 2,123
  • 2
  • 13
  • 4
  • 1
    possible duplicate of [How to delete the old history after running git filter-branch?](http://stackoverflow.com/questions/5984428/how-to-delete-the-old-history-after-running-git-filter-branch) – user Mar 31 '15 at 00:31

3 Answers3

365

refs/original/* is there as a backup, in case you mess up your filter-branch. Believe me, it's a really good idea.

Once you've inspected the results, and you're very confident that you have what you want, you can remove the backed up ref:

git update-ref -d refs/original/refs/heads/master

or if you did this to many refs, and you want to wipe it all out:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

(That's taken directly from the filter-branch manpage.)

This doesn't apply to you, but to others who may find this: If you do a filter-branch which removes content taking up significant disk space, you might also want to run git reflog expire --expire=now --all and git gc --prune=now to expire your reflogs and delete the now-unused objects. (Warning: completely, totally irreversible. Be very sure before you do it.)

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • After doing update-ref, reflog expire and gc, both of these files still reference the original ref: .git/info/refs and .git/packed-refs Looks like maybe that should say this instead: `git update-ref -d refs/original/refs/heads/master` – lhunath May 23 '12 at 07:24
  • 1
    I guess you accidentally mixed the words: If I am right, it should be `git update-ref -d original/refs/heads/master`? However, only your second command worked for me. – JJD Jul 06 '12 at 14:44
  • 4
    It’s `git update-ref -d refs/original/refs/heads/master` btw. You can see the full name using `git show-ref`. – poke Jul 24 '12 at 08:48
  • 4
    What about the shorter `git show-ref refs/original/* --hash | xargs -n 1 git update-ref -d`? – CharlesB Jul 30 '13 at 09:09
  • using gitextensions there is View > Show reflog commits, which totally obviates this backup; you always have a clearly visible backup whatever you do in git this way. – TamaMcGlinn Oct 23 '20 at 11:31
11

And if you're in Windows PowerShell:

git for-each-ref --format="%(refname)" refs/original/ | foreach-object -process { git update-ref -d $_ }
Christian Rondeau
  • 4,143
  • 5
  • 28
  • 46
Guilherme Duarte
  • 3,371
  • 1
  • 28
  • 35
10

filter-branch keeps backups so repository need to clean up reflogs and garbage collect. Make sure that have no need in this backups before deletion:

rm -Rf .git/refs/original
git gc --aggressive --prune=now
Kiryl Plyashkevich
  • 2,157
  • 19
  • 18