1

After having split the repository in my workspace into two by filtering, I cloned them "bare" into new origins, and then continued to work in a cloned workspace on the current branch without a problem.

Several commits later I merged my branch into master, then pushed it successfully. Next I deleted the obsolete branches in my workspace.

However when I tried to get rid of obsolete branches in the "origin" repository too, I have problems like these:

  • git branch lists branches that I cannot delete using git branch -d due to fatal: Couldn't look up commit object for HEAD.
  • git fsck complains: notice: HEAD points to an unborn branch (bitmap-generic)

(I was hoping git fsck would identify those "dead" branches and clean them up, but obviously it did not)

Unfortunately I did not record the exact commands, but roughly this is what I did (based on the instructions given in section 19.1 of the Git book by Preißel and Stachmann, 1st edition (German)):

  1. Make a backup: git clone --no-hardlinks --bare orig.git backup.git

  2. Remove unwanted files: git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch ...' --tag-name-filter cat --prune-empty -- --all

  3. Reorganize: git clone --no-hardlinks --bare backup.git new.git

  4. Drop backup: rm -rf backup.git

  5. Check out new.git and work on it

  6. Remove unneeded obsolete branches in new.git: git branch -d ... How can I fix these issues? Git version being used is 2.26.2.

U. Windl
  • 3,480
  • 26
  • 54
  • "Next I deleted the obsolete branches in my workspace." Can you tell us how (command(s))? – Romain Valeri Jul 02 '21 at 14:01
  • What is the output of `git status`? – dan1st Jul 05 '21 at 06:16
  • Aside from that, bare repositories don't have a branch checked out. If you want to delete a remote branch, you could use `git push --delete`. – dan1st Jul 05 '21 at 06:17
  • @dan1st `git status` does not make sense in the repository, and `git push --delete` has the problem that the branches are already deleted in the workspace. Or will it work anyway? – U. Windl Jul 05 '21 at 07:31
  • It [should work because it is not dependent on the local repository.](https://git-scm.com/docs/git-push#Documentation/git-push.txt---delete) Aside from this, you can use [`git push --prune`](https://git-scm.com/docs/git-push#Documentation/git-push.txt---prune). – dan1st Jul 05 '21 at 07:36
  • @dan1st `git push --delete` (with and without a branch name argument) always outputs "*fatal: --delete doesn't make sense without any refs*". – U. Windl Jul 06 '21 at 05:50
  • @dan1st `git push --prune` reports "*Everything up-to-date*", but I still have "*HEAD points to an unborn branch*" (I was able to get rid of the other branches using `git branch -D ...` before). – U. Windl Jul 06 '21 at 05:54
  • You need to provide what to delete when using --delete – dan1st Jul 08 '21 at 19:03

1 Answers1

1

(This answer is based on suggestions made by René Scharfe (used with author's permission))

First try to assign a valid commit to the branch name.

% git branch --force bitmap-generic
fatal: Not a valid object name: 'bitmap-generic'.
% git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (173/173), done.
notice: HEAD points to an unborn branch (bitmap-generic)
...

Backup the repository (I used cp -rp ...) just in case you mess things up.

Assign some non-existing commit to the branch (the file didn't exist before; if it does, it's not needed):

% echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >refs/heads/bitmap-generic

git branch indicated that bitmap-generic would be the current branch, so that needed to be changed next:

# HEAD references the current branch
% cat HEAD
ref: refs/heads/bitmap-generic

Edit HEAD, replacing bitmap-generic with master (or any different branch).

% git branch --delete --force bitmap-generic
error: Couldn't look up commit object for 'refs/heads/bitmap-generic'

I did not expect that, but it does not seem to be important (I focused on the branch name while git focused on the commit ID); continue (make git happy):

# Re-assign branch "bitmap-generic" to HEAD commit:
% git branch --force bitmap-generic

Eventually:

% git branch --delete bitmap-generic
Deleted branch bitmap-generic (was 03aa7ca).

After that git fsck did no longer complain.

U. Windl
  • 3,480
  • 26
  • 54