1

After having renaming the 'master' branch (using Sourcetree), I can't fetch anymore the changes done in the remote repository.

I get the error:

error: cannot open .git/FETCH_HEAD: Permission denied

When I open this file, I see that the referenced branch is still set toward branch 'master'.

Taking a look at the remote repository, I saw that a branch with named 'master' still exists, in addition to the renamed branch where I merged my last changes. I think there have got a problem somewhere and the renaming was not replicated correctly in the remote repository.

I have tried to manually edit the FETCH_HEAD (changing the full SHA and the pointed branch), but I still get this error. Of course, I can not rename again as before my main branch, since a branch named 'master' is always present.

Is there a way around this problem?

P. Mergey
  • 313
  • 4
  • 18
  • 1
    This error does not come from renaming a branch, but rather, usually, from running various operations as some other user (usually as the super-user with `sudo`). This leaves files and/or directories owned by the super-user, so that you, as an ordinary user, no longer have permission to do anything with them. If so, the fix is to change the ownership back to yourself. – torek Aug 22 '17 at 20:34
  • As I am the unique user, I suppose this problem occurs because I have changed some settings (and indeed, I have updated username and email address). So, files are currently stated to be owned by the user defined with my old settings and I need to change this ownership, right? – P. Mergey Aug 23 '17 at 05:40
  • 1
    Changing your *configuration* (with `git config`) would not matter. Changing underlying file permissions with `chmod` or file ownership with `chown` would; and on some Linux variants (SELinux in particular) you can remove all kinds of permissions from yourself, to the point that no one can accomplish anything. (I believe you can also do this with ACLs on Windows, but I don't "do" Windows.) – torek Aug 23 '17 at 06:17
  • And do you think [this thread](https://stackoverflow.com/q/750172/4094098) can help? – P. Mergey Aug 23 '17 at 11:40
  • That thread is about using `git filter-branch`, which is about copying existing commits to new commits, changing some of the data inside the commits along the way. It seems to me you are still conflating the *contents* of various Git files with the (external to Git, not-controlled-by-Git) *file ownership*, which is managed by your operating system, not by Git itself. Git's author and committer names are simply data strings *stored in* files, regardless of OS-level ownership of those files. (And in any case, `.git/FETCH_HEAD` contains none of that data in the first place!) – torek Aug 23 '17 at 14:36
  • I think that I have finally found a (partial) resolution concerning my issue. I finaly didn't use `chown` (I got _invalid user_ errors each time I tested it), but your comment have helped me to find the command [`rm -f .git/FETCH_HEAD`](https://stackoverflow.com/a/29206450/4094098), which has let me fetch the remote changes. It is already a first step : now, it seems that only one of my local branches doesn't have the last changes. Still some efforts to have a clean repository. – P. Mergey Aug 30 '17 at 14:35

1 Answers1

2

In fact, it seems that this problem is (potentially) due to the combination of various factors:

  1. I have actually renamed a branch (but that doesn't seem to be the major causek)
  2. I have also updated username and email address (this can support the analysis that I have performed various operations as some other user)
  3. Some branches was unable to be fetched certainly because of a known bug of SourceTree (which have suddenly stopped tracking of some remote branches)

As using chown command doesn't seem to be effective, I have finally resolved my issue using the following process:

  1. Removing the denied file using the command rm -f .git/FETCH_HEAD (as described in this answer)
  2. Reconnecting the untracked branches using the command git branch -u (as described in this comment)

After that, I was able to fetch all the remote branches, and now, all is correctly synchronized with my local repository.

P. Mergey
  • 313
  • 4
  • 18