kfmfe04
kfmfe04

Reputation: 15327

A simple git configuration that allows for a push from local to remote?

I have two machines: remote and local. I want remote to be my main repository that I can push to from local.

I have a current project that lives on remote that I would like to store into git's repository.

Answer

Since the files were originally on remote and I also needed a bare repository that I could push to on remote, this was a little tricky. The procedure is essentially a bootstrap until I have the bare directory up.

The sequence I used to set-up and test, as suggested by Paolo Capriotti in his short answer below:

  1. on remote, create a bare repo on /home/me/depot/project
  2. on remote, also create a working .git repo
  3. on local, clone from the working repo on remote
  4. on local, edit .git/config so that [remote "origin"]'s url points to bare repo on remote
  5. on local, edit some file, commit to working repo
  6. on local, git push origin master to post to bare repo on remote
  7. on remote, edit .git/config to point to the bare repo on remote
  8. on remote, git pull to pull from bare repo

An even simpler sequence would involve ignoring the local directory completely. First set up the working and bare repo on remote and then finally, on local, clone from the bare repo on remote.

Original Question

I have tried the following:

  1. remote> git init && git add . && git commit -m "initial commit"
  2. local> git clone ssh://remote/path_to_remote_git
  3. edit a file on local
  4. local> git commit -a
  5. local> git pull
  6. local> git push origin master

fails

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.

So I start over: rm -rf .git directory on remote, rm -rf on local.

I try creating a bare directory on remote:

  1. remote> export GIT_DIR=/home/me/depot/project.git
  2. remote> git init && git add .

fails

fatal: This operation must be run in a work tree

What series of steps do I have to go through so I can push from the local machine to the remote machine, starting with a project but no repositories?

I prefer the depot in a /home/me/depot directory on remote if possible.

Does that mean I need both a bare and a non-bare repository on remote?

Upvotes: 2

Views: 1099

Answers (1)

Paolo Capriotti
Paolo Capriotti

Reputation: 4072

Create a bare repository on remote with git init --bare, don't add any files yet. Create a repository on local, add your files, then push to remote.

Upvotes: 4

Related Questions