1

I am studying git and at first it seems ok but when I try to push the changes in the master file I got these errors:

Counting objects: 3, done.
Writing objects: 100% (3/3), 232 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 to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
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 some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To C:\wamp\www\MyGit\myfirstgit
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'C:\wamp\www\MyGit\myfirstgit'

Ok here's what i did:

  1. I create my master folder

    -- C:\wamp\www\MyGit\myfirstgit

  2. Checkout the folder to \www

    -- C:\wamp\www\myfirstgit

  3. I created a README file

    -- C:\wamp\www\myfirstgit>echo 'TEST README' > README

  4. Check status C:\wamp\www\myfirstgit>git status

    On branch master

    Initial commit

    Untracked files: (use "git add ..." to include in what will be committed)

        README
    

    nothing added to commit but untracked files present (use "git add" to track)

  5. Add and commit the CHANGES

    C:\wamp\www\myfirstgit>git add README

    C:\wamp\www\myfirstgit>git commit -m "My first commit"

    [master (root-commit) 43bad4e] My first commit 1 file changed, 1 insertion(+) create mode 100644 README

  6. Then I push the changes:

    -- C:\wamp\www\myfirstgit>git push origin master

I also tried pointing out the directory but error:

C:\wamp\www\myfirstgit>git push origin C:\wamp\www\MyGit\myfirstgit
fatal: remote part of refspec is not a valid name in C:\wamp\www\MyGit\myfirstgit

Then I got the error above.

Can you help me with this? I am new in using this tool.

Thanks.

5
  • Did you git init that first folder? Commented Jul 21, 2015 at 4:16
  • The trouble is that you are pushing to a non-bare repository. The error message explains what the problem is with that. A more normal workflow is that you have one bare repository that is the "authoritative" one, and your others push and pull from that. Commented Jul 21, 2015 at 4:17
  • bitflop.com/tutorials/git-bare-vs-non-bare-repositories.html Commented Jul 21, 2015 at 4:17
  • @LennartRegebro, yes I did Commented Jul 21, 2015 at 5:05
  • @Jerielle I suspect that's the problem. Normally with git the origin repo is not standard got repo. The above link explains somewhat, but as usual git is immensely confusing... Commented Jul 21, 2015 at 8:25

3 Answers 3

3

The typical use case for remote repositories in git is that the remote repository is only used for exchanging code with other developers, and for backup. Such a central repository is often bare, meaning that it doesn't contain a working directory; it only contains the git history. Pushing changes to non-bare repositories is a bit problematic, as you have discovered.

If you want to keep the first repository as non-bare (so that it has a working directory and you can work directly with the files inside it), go to C:\wamp\www\MyGit\myfirstgit and run git checkout --detach HEAD. Now, the push should work. Note, though, that the files in the working directory C:\wamp\www\MyGit\myfirstgit will not change, as the repository won't automatically check out the changes that are pushed (the error message essentially said that you tried to push to the active branch, and that would have required the working directory in C:\wamp\www\MyGit\myfirstgit to change, and git doesn't support that).

Otherwise, you can recreate your remote repo as bare (assuming that the only contents was the initial commit that still exists in C:\wamp\www\myfirstgit): Delete everything under C:\wamp\www\MyGit\myfirstgit, including the hidden .git directory, and run git init --bare . there; then, push again from C:\wamp\www\myfirstgit.

Sign up to request clarification or add additional context in comments.

Comments

3

Error message says it all. You either need to use bare repo (recommended), or if you absolutely need nonbare - execute git config receive.denyCurrentBranch warn here and then push. Note that it will only update files in .git directory, working copy on remote will remain unchanged until someone executes git reset --hard here.

On newer git you can set receive.denyCurrentBranch to updateInstead and it will be updated on push automatically.

1 Comment

Although technically correct, this answer is useless unless you already know exactly how git works, in which case you would not ask this question. :-) Too many got-specific words, not enough explanation.
-4

Use

git push origin master

But I would advice you not to use master as your working branch so do this to create a new branch

git checkout -b name_of_branch

Make sure you add the branch remotely too. From here you can continue your normal process.

git add -A
git commit -m "commit message"
git push origin name_of_branch

1 Comment

While this may be good advice, it doesn't solve the OP's problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.