0

No example I have found shows how to do the simplest of tasks in git.

Could someone show me the git command needed to do the same as this this SVN job?

svn checkout svn://jeeves/testrepo
cd testrepo
echo >newfile.txt Wow I added a file
svn add newfile.txt
svn commit -m "How simple was that"

Assume I'm sitting on the machine with the git repository. Here's what I'm doing (along with hundreds of variations, it seems).

git clone ~/git/testrepo.git
cd testrepo
echo >newfile.txt Wow I added a file
git add newfile.txt
git commit -m "So far, so good. Everything has worked so far..."
git push origin master

I can't believe this is so difficult. I'm obviously missing something, because I keep getting messages like

jeeves:~/git/myCare geoffrey$ git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 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: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.

Help!

  • 3
    The problem is that `testrepo.git` was not correctly set up as a "server" repository for allowing pushing to. Instead you've cloned some repository that has code checked out – M.M Jul 12 '21 at 05:51
  • 1
    You either set up a server repository; or go into testrepo and pull from your copy. https://stackoverflow.com/a/31590993/1505939 for full example – M.M Jul 12 '21 at 05:57
  • https://stackoverflow.com/search?q=%5Bgit%5D+error%3A+refusing+to+update+checked+out+branch – phd Jul 12 '21 at 06:49

1 Answers1

2

Reason:You are pushing to a Non-Bare Repository

There are two types of repositories: bare and non-bare

Bare repositories do not have a working copy and you can push to them. Those are the types of repositories you get in Github! If you want to create a bare repository, you can use

git init --bare

Well, you can't push to the currently checked out branch of a repository. With a bare repository, you can push to any branch since none are checked out. Although possible, pushing to non-bare repositories is not common. What you can do, is to fetch and merge from the other repository. This is how the pull request that you can see in Github works. You ask them to pull from you, and you don't force-push into them.


in the latest git versions pushing to the checked out branch of a non-bare repository is possible. Nevertheless, you still cannot push to a dirty working tree, which is not a safe operation anyway.

itshosyn
  • 143
  • 2
  • 16
  • The original testrepo.git was imported from subversion. That old subversion repository does have working copies checked out. Could that be the problem? I've since found other SO Q's that seem to have dozens of steps to clone the SVN repo, whereas the one I did was one single command. I'll delete testrepo.git and try following a better guide. Thx. – Geoffrey Hoffmann Jul 13 '21 at 01:11