Is there a git command that can resolve an error: remote branch 'refs/heads/foo/bar' not found?

1.3k views Asked by At

In my situation I was tracking a remote hotfix branch which someone else merged and removed.

I have deleted my local branch but when I pull I still get the above error because I have the following lines in my git config file:

[remote "origin"]
...
    fetch = +refs/heads/foo/bar:refs/remotes/origin/foo/bar
    push = +refs/heads/foo/bar:refs/for/foo/bar
...

The error can be resolved by deleting these lines from the file but is there a way to do it from the command line?

1

There are 1 answers

0
torek On BEST ANSWER

There is, but it's generally a lot more difficult than manual editing, if you want to get rid of just the one matching line.

The command to modify a git configuration file is git config. It has --unset, --unset-all, and --remove-section options for deleting one, all matching, or all entries in some particular section or key.

The section, in this case, is remote.origin (because it's in [remote "origin"] in the file) and the key is remote.origin.fetch. You would not want to remove the entire section, in this case, so that leaves --unset and --unset-all as possibilities.

There can be more than one fetch line, and more than one push line. In other words there can be multiple copies of one key, with different values.

If there is just one remote.origin.fetch line, and you want to remove it, you can simply:

git config --unset remote.origin.fetch

If you try this on a repository with multiple fetch lines, you get:

warning: remote.origin.fetch has multiple values

and you will find that nothing is removed (at least that is what I got with git 2.1.0). In this case, you need the more complex variants, either --unset-all (remove all the remote.origin.fetch keys) or --unset key value_regex (remove those keys whose value matches value_regex).

Since you presumably only want to unset the one matching line, you must come up with a regular expression that matches that one line, and only that one line. Read through the git config documentation to come up with a suitable regular expression; note that since these are POSIX "extended" REs, a bare + character means "one or more of the previous character", so +refs/... does not work as the "previous character" part is missing. (These are also clearly un-anchored, based on the documentation.) In this case, :refs/remotes/origin/foo/bar is probably sufficient, but you could get fancier.

To remove the push entry, repeat the above steps but with remote.origin.push as the key.

(Or just use your favorite editor and interactively delete the right lines, which is a lot easier, albeit not very suitable for a script...)