1

Please look at this example:

pi@rpi2:~/git/test $ mkdir remote_rep
pi@rpi2:~/git/test $ cd remote_rep/
pi@rpi2:~/git/test/remote_rep $ git init
Initialized empty Git repository in /home/pi/git/test/remote_rep/.git/
pi@rpi2:~/git/test/remote_rep $ touch readme.txt
pi@rpi2:~/git/test/remote_rep $ git add readme.txt
pi@rpi2:~/git/test/remote_rep $ git commit -m "initial commit"
[master (root-commit) 4dd0ba4] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme.txt
pi@rpi2:~/git/test/remote_rep $ git status
On branch master
nothing to commit, working tree clean
pi@rpi2:~/git/test/remote_rep $ git log
commit 4dd0ba4797989d39dd7bb42da8de58dfac3ebad8
Author: pi <pi@abc.com>
Date:   Mon Sep 21 20:13:19 2020 +0000

    initial commit
pi@rpi2:~/git/test/remote_rep $ cd ..
pi@rpi2:~/git/test $ mkdir local
pi@rpi2:~/git/test $ cd local/
pi@rpi2:~/git/test/local $ git clone ~/git/test/remote_rep/
Cloning into 'remote_rep'...
done.
pi@rpi2:~/git/test/local $ ls
remote_rep
pi@rpi2:~/git/test/local $ cd remote_rep/
pi@rpi2:~/git/test/local/remote_rep $ ls
readme.txt
pi@rpi2:~/git/test/local/remote_rep $ cat readme.txt
pi@rpi2:~/git/test/local/remote_rep $ echo "abc" > readme.txt
pi@rpi2:~/git/test/local/remote_rep $ cat readme.txt
abc
pi@rpi2:~/git/test/local/remote_rep $ git commit -m "updated readme.txt"
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
        modified:   readme.txt

no changes added to commit
pi@rpi2:~/git/test/local/remote_rep $ git push
Everything up-to-date
pi@rpi2:~/git/test/local/remote_rep $ git remote show
origin
pi@rpi2:~/git/test/local/remote_rep $ git remote -v
origin  /home/pi/git/test/remote_rep/ (fetch)
origin  /home/pi/git/test/remote_rep/ (push)
pi@rpi2:~/git/test/local/remote_rep $

How come that by issuing:

pi@rpi2:~/git/test/local/remote_rep $ git push
Everything up-to-date

I got a message "Everything up-to-date", but as you can see readme.txt has been changed.

If I continue with:

pi@rpi2:~/git/test/local/remote_rep $ git add readme.txt
pi@rpi2:~/git/test/local/remote_rep $ git commit -m "updated abc"
[master 136cdba] updated abc
 1 file changed, 1 insertion(+)
pi@rpi2:~/git/test/local/remote_rep $ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 241 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: 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.
remote:
remote: You can set 'receive.denyCurrentBranch' configuration variable to
remote: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /home/pi/git/test/remote_rep/
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/home/pi/git/test/remote_rep/'
pi@rpi2:~/git/test/local/remote_rep $

What am I missing? git version 2.11.0

I cannot submit this question due to stackoverflow error: "It looks like your post is mostly code; please add some more details.", so I am adding here this text just to change discrepancy. Please ignore this last paragraph.

user3225309
  • 1,183
  • 3
  • 15
  • 31
  • So you intend to have your own server running git instead of using e.g. Github? – Christoph Sep 22 '20 at 19:15
  • Does this answer your question? [Git Push error: refusing to update checked out branch](https://stackoverflow.com/questions/11117823/git-push-error-refusing-to-update-checked-out-branch) – flaxel Sep 22 '20 at 20:11

1 Answers1

2

Your first git push actually succeeded, but did nothing. The reason is that your git commit -m "updated readme.txt" did nothing either, and therefore there were no commits to send. (Note that git push sends commits, not files. The commits contain files, but in this case, there was no new commit to send.)

Your second git push failed because the other Git didn't like it. That's not something you can fix from the Git that runs git push. Well, that's not entirely true: you can push a different commit, or to a different name, in an effort to please the other Git—but that's not what you should do here.

Since you actually control both Git repositories, what you should do is switch stances. Instead of acting as the client, in the client Git, you would go over to the server and work in the server Git. The error message that the server Git sent over to the client Git, that the client Git prefixed with remote: in front of each line, is exactly the message you should deliver to the person in control over the server Git. In this case, that's yourself, so now, acting as the person in control of the server, now you should pay attention to all those remote: lines. Read them now, then think about how you want the server Git to behave:

  • The usual solution is to have a bare repository (created with git init --bare). A bare repository has no working tree. With no working tree in it, no one can do any work in it. This means there is no problem with messing up the current work being done in the server repository: with no one working in it, there is no current work being done, and nothing to mess up. Of course, this also means you can't do any work there, but if that's what you want, that's the right way to go.

  • The long error message offers some alternatives, if you do want to be able to do work on the server. (It probably should also mention the updateInstead mode, which was new in Git 2.10; I'm not sure why it does not.)

  • The long message does not mention that if you want the server to deploy a commit, you can do that. But Git makes a fairly lousy deployment system: if you want to automatically deploy certain updates, you probably should obtain or write a deployment system, rather than just using raw Git directly.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Actually I want my raspberry pi to act as a git server on which I will never do any work. In that case looks like I should have been created a bare repository on raspberry (to act as a my local github) and than do all my work from other computers. Thanks for an explanation. – user3225309 Sep 24 '20 at 19:24