Users not able to delete branches error -- packed-refs.new: File exists
Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
When attempting to delete or create a branch an error occurs shows an error that states:
remote: error: unable to create file <bitbucketHome>/shared/data/repositories/<repoID>/./packed-refs.new: File exists
Diagnosis
This an example screen shot:
Cause
The file packed-refs.new exists in the git repository. This file is used by git to provide a temporary file for the new pack and warn other git process that the pack is running. Git will create this file, packed-refs.new, put all of the references in this file, remove all of the loose references, them move the file to packed-refs. The file format is <CommitHash>, then followed by the branch name.
If the normal process is interrupted, this file can be left around. This can happen if a git pack-refs process was killed while packing.
Solution
Remove the file. Here is the process to remove the file without any further data loss. This assumes that all Bitbucket Data Center Nodes have confirmed that the file is not being accessed. Run this command on each Bitbucket Data Center Node. No output means that the file is not being used if the full path of the file is like the above error. OR do a rolling restart of each Bitbucket Data Center Node if you are unsure.
sudo lsof | grep "packed-refs.new"
packed-refs.new file is empty
It is safe to delete this file.
packed-refs.new file is not empty
If you are unsure about any of these steps, please stop and contact support before step 9.
Stop all Bitbucket Server or each Bitbucket Data Center Node.
- Move the file packed-refs.new to a temp directory:
-
mv <path>/packed-refs.new /tmp
-
- Copy the packed-refs file from the repository and check to see if there are any valid refs in
packed-refs.new
that are not inpacked-refs
-
cp <pathToRepo>/packed-refs /tmp diff /tmp/packed-refs.new /tmp/packed-refs
-
- Make a copy of the git repository.
-
cp -r <path> /tmp
-
- For each hash in packed-refs.new make sure the reference is a valid commit. Make a list of lines that have valid hashes. If git log does not find a corresponding commit hash, you should not use that hash in the updated file.
-
cd /tmp/<dir>/ git log <hash>
-
Add the missing lines to the original
packed-refs
file in /tmp.- Copy the corrected packed-refs into the repository copy.
-
cp /tmp/packed-refs /tmp/<dir>/
-
- Run the pack refs and git fsck command to make sure the packed-refs file is valid. If any errors, do not continue. This step must pass to continue.
-
cd /tmp/<dir>/ git pack-refs --all git fsck
If you get any invalid reflog entry from the git fsck please use Repository has invalid reflog entry after step 9.
-
- Move the updated pack-refs file to the original repository.
-
cp /tmp/packed-refs <pathToRepo>/
-
- Start Bitbucket.