Yesterday, I posted a question on how to clone a Git repository from one of my machines to another, How can I 'git clone' from another machine?.
I am now able to successfully clone a Git repository from my source (192.168.1.2) to my destination (192.168.1.1).
But when I did an edit to a file, a git commit -a -m "test"
and a git push
, I get this error on my destination (192.168.1.1):
git push
hap@192.168.1.2's password:
Counting objects: 21, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1010 bytes, done.
Total 11 (delta 9), reused 0 (delta 0)
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error:
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error:
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git+ssh://hap@192.168.1.2/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://hap@192.168.1.2/media/LINUXDATA/working'
I'm using two different versions of Git (1.7 on the remote and 1.5 on the local machine). Is that a possible reason?
You can get around this "limitation" by editing the
.git/config
on the destination server. Add the following to allow a git repository to be pushed to even if it is "checked out":or
The first will allow the push while warning of the possibility to mess up the branch, whereas the second will just quietly allow it.
This can be used to "deploy" code to a server which is not meant for editing. This is not the best approach, but a quick one for deploying code.
The best way to do this is:
This will clone the repository, but it won't make any working copies in
.../remote
. If you look at the remote, you'll see one directory created, calledcurrentrepo.git
, which is probably what you want.Then from your local Git repository:
After you make changes, you can:
git config --local receive.denyCurrentBranch updateInstead
https://github.com/git/git/blob/v2.3.0/Documentation/config.txt#L2155
Use that on the server repository, and it also updates the working tree if no untracked overwrite would happen.
It was added in Git 2.3 as mentioned by VonC in the comments.
I've compiled Git 2.3 and gave it a try. Sample usage:
Output:
Yay,
b
got pushed!An article I found that might be useful to others is Git in 5 minutes.
I had an Xcode project under Git version control that I wanted to push up to a Virtual Distributed Ethernet (VDE) I have in a DC. The VDE runs Centos 5.
None of the articles I read about Git talked about bare repositories. It all sounded so simple until I tried what I thought should be easy coming from an SVN background.
The suggestions here to make the remote repository bare worked. Even better for my requirements was to clone the Xcode project to
projectname.git
, copy that to the remote server; then pushes magically worked. The next step will be getting Xcode to push without errors about commits, but for now I'm okay doing it from Terminal.So:
To push changes from your Xcode project after you've committed in Xcode:
I'm certain there is a smoother more sophisticated way of doing the above, but at a minimum this works. Just so everything is clear, here are some clarifications:
/xcode-project-directory
is the directory your xcode project is stored in. It's probably/Users/Your_Name/Documents/Project_Name
. projectname is literally the name of the project, but it can be anything you care to call it. Git doesn't care, you will.To use scp you need to have a user account on the remote server that's allowed SSH access. Anyone running their own server will have this. If you're using shared hosting or the like, you might be out of luck.
remotehost.com
is the name of your remote host. You could as easily use its IP address. Just for further clarity I'm using Gitosis on the remote host with SSH keys, so I'm not prompted for passwords when I push. The article Hosting Git Repositories, the Easy (and Secure) Way tells you how to set all that up.You can recreate your server repository and push from your local branch master to the server master.
On your remote server:
OK, from your local branch:
My solution (in use)
bingo