git refusing to fetch into current branch

微笑、不失礼 提交于 2019-11-28 06:40:57

What you're trying to do is to fetch the branch you're workin on. That is, you are on the master branch and you try to update it. That's not possible. It's more common to update the remotes/* branches and then pull it into your local ones. What you want is, perhaps,

git remote add otherrepo thehost:/the/path.git

That will setup repository to be fetched into remotes/otherrepo/*. git fetch otherrepo should do the trick. Alternativeley, you can manually edit your .git/config and set fetch for the remote to something like refs/heads/*:refs/remotes/otherrepo/*.

In case anyone finds this because they specifically want to fetch into the current branch, you can use the --update-head-ok flag. From the docs:

-u
--update-head-ok
By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check. This is purely for the internal use for git pull to communicate with git fetch, and unless you are implementing your own Porcelain you are not supposed to use it.

In some cases we do want to implement our own porcelain commands, e.g., automation and tooling.

Also this this should work if you are in master branch and wants to get latest try this

git pull origin master

I've had this problem when I thoughtlessly cloned a repository instead of fetching it, so that both repositories were masters. If you have done no work on the remote repository, you can fix things with the basic git commands as follows: (1) delete the remote repository, (2) copy the local repository to where the remote one was, (3) delete the local one, and then (4) set up the local repository using

git init; git fetch $REMOTE_GIT_REPOSITORY_URL  

Then git pull and git push will do the right things. The advantage of avoiding git remote, per Michael's more efficient and principled answer, is that you don't need to think about the semantics of tracking branches.

Are you actually typing Git commands into the command line, or are you running the Git executable from your own code?
If you're running it from code, are you sure that Git is trying to fetch into the correct local directory?

There are two possible ways to do this:

  1. Use the options provided by your programming language to set the correct working directory before executing Git
    (C# example, because that's what I'm using)

  2. Always pass the -C parameter to Git to specify the directory with the local repo.


I have a project where I'm calling the Git executable from C# code, and I got the same error message like in the question when I accidentally forgot to set the -C parameter.

fatal: Refusing to fetch into current branch refs/heads/BRANCHNAME of non-bare repository

I have Created a branch BRANCHNAME locally then executed command "git fetch upstream pull/ID/head:BRANCHNAME" and got fatal: Refusing to fetch into current branch refs/heads/BRANCHNAME of non-bare repository

then i deleted the branch and again call the same cmd it ways fine.

Actually i was checking out pull request branch.

I have the same problem. First I try to fetch using this

git fetch [remote_name] [branch_name] 

I had the same problem you mention. After that I tried this simple command.

git pull [remote_name] [branch_name]

Note will fetch and merge the changes. If you using terminal then file open, requiring to commit message. With this message you will push the latest commit. Try this command and finally you will be able to push the request.

git push [remote_name] [branch_name_local] 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!