Waltham WECAN
Waltham WECAN

Reputation: 491

Why am I not able to push from a local repository to a remote repository, given that both are on my local machine ?

I am learning how to use git on my Mac OS Sierra and I created both a remote reprository and a local repository with both being on my local machine.

I git init the remote repo and git clone it to my local repo. When I made changes to my local repo I followed this procedure :

git diff
git status 
git add -A 
git commit -m ""
git pull origin master

Then I git push origin master and I got this error message.

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 340 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable t
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in som
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, se
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /Users/iivri.andre/local-repo/../git
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/Users/iivri.andre/local-repo/../git'

How can I correct this?

Upvotes: 0

Views: 2963

Answers (3)

Ben
Ben

Reputation: 1377

Your remote repository origin is not a bare repository, meaning it contains a checked-out version of your code. Specifically, the error message is telling you that the branch on origin that is checked out is the same as the branch you are trying to push to.

When you push, a number of things happen, specifically:

  • Commits are added to the history
  • Branch pointers are moved

The second point is specifically what the error message is about. Whoever owns that repo (in this case it is you, but it is not a requirement) has checked out a specific state of the repository as defined by the branch master at that time. If you push to their repo, their master pointer is going to move to include your new commits. If git allowed your push to go through as is, you would be changing files on someone else's working copy that they may or may not be immediately aware of.

To get your commits from one non-bare repo to another, you have a number of options available to you:

  • You can add a remote from the first repo to the second, and fetch the new commits
    • git remote add feature <path-to-second-repo>
    • git fetch feature
    • git merge feature/master (personally, I like to use the --ff-only flag with this)
  • You can rename the master branch of your non-origin repo and push that
    • git branch -m master feature_master
    • git push origin feature_master
    • cd <path-to-origin>
    • git merge feature_master (again, I would use --ff-only here)
  • You can do the same renaming of the master branch on your origin repository. I do not recommend this, because now you have to ensure that there is no history on origin/renamed_master that you need to rebase or merge onto the new master

Upvotes: 0

Schwern
Schwern

Reputation: 165586

How can I correct this?

There's two options. First is to make the repo you're pushing to a bare repository. This is the preferred way to have a remote, as a bare clone.

Normally you'd start with a bare repo, git init --bare, and clone from that. But since you have an existing repo, simplest thing to do is to make a bare clone of it.

git clone --bare path/to/that/repo

Now use the new bare clone as your remote you push to either by cloning it, or with git remote set-url origin path/to/the/bare/clone.


The other option is to follow the instructions and tell Git to let you push anyway.

cd the/remote/repo
git config receive.denyCurrentBranch ignore

This is a bad idea. Pushing will updated the repository, but not the checked out files (ie. the working directory). If anyone is working with that repository it will get very confusing. If your intent is to update that repo's checkout by pushing, that doesn't work.

Upvotes: 2

Ebrahim Poursadeqi
Ebrahim Poursadeqi

Reputation: 1816

you have to create remote git like this

git init --bare 

you can see this link

Upvotes: 0

Related Questions