codingsplash
codingsplash

Reputation: 5045

Unable to push in Git

I went through other similar questions extensively before ending up asking my own question on this.

I am beginning to learn Git and I tried something very simple. My objective was to create a repo in my local machine, clone it to a folder in my local machine. Make changes to files in the clone. Push the changes to the repo. Here is what I did:

  1. I created a folder 'test' with 'hi.txt' in it.
  2. I ran git init inside the folder 'test'
  3. git add .
  4. git commit -m "done"
  5. cd ..
  6. git clone test newTest

I was able to see a newTest clone getting created with a .git folder inside and the hi.txt cloned from the repo.

Then I made changes to the hi.txt inside the clone and ran the below commands:

  1. git add .
  2. git commit -m "done"

So far so good. Now when I try to push the changes to the repo 'test' folder, I get the following error:

    $ git push
Counting objects: 5, done.
Writing objects: 100% (3/3), 248 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
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 inconsist
ent
remote: error: with what you pushed, and will require 'git reset --hard' to matc
h
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:/test
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'c:/test'

So where am I going wrong? Have I misunderstood something? Will be grateful for your help.

Upvotes: 1

Views: 3172

Answers (1)

ffflabs
ffflabs

Reputation: 17481

The first "repo" you created is behaving as a working copy, i.e. a non bare repo.

Real repos, the ones you can push to, have no working copy in them. They just hold a reference to all commits in all branches.

To convert a normal repo into a bare repo

  1. Open the repo in your file explorer
  2. Make sure you're displaying hidden files (otherwise, you won't see the .git folder)
  3. Remove everything except for the .git folder
  4. Move everything inside the .git folder to the root
  5. Remove the empty .git folder
  6. Execute git config --bool core.bare true

Now you can push to your bare repo.

Edit: using the steps from your question

mkdir test
cd test
git init
echo '0.0.1' > version.txt
git commit -a -m "version file"

Now you have a non-bare repo, with the structure

enter image description here

Let's turn it into a bare repo

mv .git ../  
rm *
mv ../.git .
mv .git/* .
rm -rf .git
git config --bool core.bare true

now you have a bare repo, and its contents are

enter image description here

if at some point you get the error fatal: This operation must be run in a work tree just ignore it.

Now you can clone it as you originally did

cd ..
git clone test newTest
touch index.html
git commit index.html -m "index file"
git push

Upvotes: 4

Related Questions