jackt247
jackt247

Reputation: 43

Git Issue: "cannot update the ref 'HEAD': unable to append to '.git/logs/HEAD': Bad file descriptor"

I use Git on Windows 10 to track version control for projects I keep on my company's shared network drive. I have a project for which I have been using Git for months with no trouble. I recently created a new branch 'development' and have been using it as a testing environment for new changes before merging the changes back to master. This arrangement has been working fine for the last few commits until now.

I made changes in the testing branch then went to commit my changes but received the below error:

$ git commit -m "this is a commit description"
fatal: cannot update the ref 'HEAD': unable to append to '.git/logs/HEAD': Bad file descriptor

I now notice this error with many different operations, including git checkout <branch>. The error in this case is:

$ git checkout branch-name
error: unable to append to '.git/logs/HEAD': Bad file descriptor

I'm relatively new to this stuff so apologies if there is an obvious solution here. Any help is appreciated!.

Upvotes: 0

Views: 1051

Answers (2)

jackt247
jackt247

Reputation: 43

I was able to resolve this issue by performing the below actions:

  1. Create a local desktop directory

    $ cd '<local-directory>'

  2. Clone the repo into this directory

    $ git clone '<origin-directory>' .

  3. Make and commit the backlogged changes

    $ git commit -m "commit desc"

  4. Break link to remote origin source

    $ git remote remove origin

  5. Delete everything in original directory

  6. Copy the contents of the local directory back to the original directory

It seems to work for now but if this happens again I'll just repeat these steps.

Upvotes: 0

torek
torek

Reputation: 490118

The Bad file descriptor error is coming from your operating system. In C programs (which this particular Git program is), to write to a file, the program is required to first open the file, which produces a file descriptor: a number, usually a small integer such as 3 or 4. Then, to write to that file, the program should make an OS call of the form write(fd, data, len) where fd is an int variable holding the number returned earlier by open.

The Bad file descriptor error means that the number being passed to write is incorrect in some way: perhaps it is out of range or was never returned by an open call, or perhaps it refers to a file that is open, but not for writing. In general, if there are no programming mistakes, this should not happen.

You did, however, mention a shared drive. On Unix-like operating systems, when using networks to access files, things can go wrong that simply cannot happen if the drive is local.1 On these Unix-like systems you would normally get an ESTALE error ("Stale network file handle") rather than an EBADF ("Bad file descriptor") error here, but perhaps this is what is going wrong. In general it's unwise to attempt to use Git across any sort of sharing system: Git really wants full and total control over everything it does, and putting it on any kind of shared system—network drives, Dropbox, OneDrive, Google Drive, etc.—tends to be a bad idea.

This arrangement has been working fine for the last few commits until now.

This behavior is highly characteristic of attempts to use shared file systems with Git: it works great, until it doesn't. If you're lucky, the "doesn't" is merely a minor malfunction like this; if not, it wrecks the entire repository, and since you've been relying on the sharing instead of making clones, there may not be any backup clones from which to fix things.


1Technically, similar things can go wrong, if, e.g., the drive catches on fire. But usually by then the program has stopped running for other reasons. 😅

Upvotes: 2

Related Questions