0

I have a Git working directory that I would like to use to create a new master, keeping the existing commit history (etc.) intact. The reason for this is that my original remote master has become corrupt, and the working directory has had updates made to it since.

I've tried a Git clone of the working directory, but I am unable to push to the cloned directory. A get a similar problem if I do a straight copy and paste of the working directory to my Git server.

How do I convert my local working directory to a new master on a remote server?

I'm a newbie when it comes to Git, so a simple explanation without too much jargon would be really helpful.

James Baker
  • 1,143
  • 17
  • 39
  • Did you take a look at [this question](http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git)? – David Robinson Oct 07 '13 at 11:06
  • 2
    What error message do you get when pushing? – VonC Oct 07 '13 at 11:06
  • I don't really understand exactly what you mean by "create a new master". Do you simply want to update the remote master to refer to the same commit as the local master? – Jonathan Wakely Oct 07 '13 at 11:21
  • I assume you want to create a pushable **bare** repo based on the repo you're currently working on. *making a working directory a master* is a bit confusing, because `master` usually denotes the default branch... – eckes Oct 07 '13 at 11:50
  • You cannot push to a working branch. Simply checkout to another branch on the "master" repo/folder and you'll find that you can now push to your new "master". – slebetman Oct 07 '13 at 12:29

3 Answers3

0

If I understand your question correctly you are looking for this

git remote set-url origin https://blabla.com/newrepo.git
git push origin master

This will retain all your current history but allow you to push to a different remote repository.

cleggy
  • 116
  • 5
0

I guess you are getting the error that you cannot push to the remote repo because branch whatever is currently checked out (like here: Git push receiving "error: refusing to update checked out branch").

To create a bare repo (this is a repo with full history but without checked out working copy), do a

git clone --bare yourrepo barerepo.git

Then, take the barerepo.git directory and move it to the machine that should host it.

One done, add the repo as a remote to yourrepo

git remote add origin /path/to/barerepo.git

Then, you should be able to

git fetch origin
git push origin

However, the question sounds like you want to restore a crashed remote repository from a local one. Had the same issue one week ago: How do I restore a remote crashed Git repo?

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
0

Maybe you can force the push to the remote master branch from your local one, using the --force parameter, but I wouldn't recommend it if you don't know what you are doing.

-f --force Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.

https://www.kernel.org/pub/software/scm/git/docs/git-push.html

So something like this :

git push --force origin master:master

The first master represents the local branch you want to push, and the other master represents the remote branch you are pushing to.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66