Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore Git bare repository from local one

Tags:

git

restore

Here is the initial situation:

  • A bare Git repository on a remote server
  • A development Git repository on a local machine

Then the remote server crashes.

I keep committing changes into my local repository but I cannot push them until we fix the server. In a day or so it becomes apparent that the server cannot be recovered due to a hardware problem, so the remote repository is lost. Meanwhile, I had already made several commits into my local repository.

Now I am trying to restore the repository on the server.

  1. I copy the files from my local .git folder to c:\repositories\myproject
  2. I run git --git-dir=myproject config core.bare true command

It looks good, however, the problem now is that I have two branches in the bare repository, one is master (server branch on the moment of crash), another is remote/origin (the newer branch with my local commits).

So essentially I would like to push the outstanding commits to master and remove the remote/origin branch at all as it is not needed in the bare repository.

How do I do that?

Feel free to share a better way to accomplish such task.

like image 983
Alex Avatar asked Apr 30 '14 20:04

Alex


People also ask

How do I restore all files in a git repository?

Another option is to provide the . character, thereby restoring all files in the current directory. Removes the file from the Staging Area, but leaves its actual modifications untouched. By default, the git restore command will discard any local, uncommitted changes in the corresponding files and thereby restore their last committed state.

How to create a bare repository in Git?

Practically speaking everything in the repository apart from .git is a part of working tree. To create a bare repository, navigate to the chosen directory in bash (for linux users) or command prompt (for windows users) and type: The file structure of the bare repository should look like this:

What is the default repository in Git?

It stores the hashes of commits made in the branches and a file where the hash of the latest commit is stored. As you can see, the .git folder contains all the required files for tracking the project folder. The default repository is always used for local repositories. What is a bare repository?

How to push files to bare repository?

The only possible operations on the Bare Repository are Pushing or Cloning. A bare repository is linked with a local repository, hence the files in .git of local repo should match with the files in the bare repo. First, create a bare repository (See section for the code snippet). Cloning into 'BareRepo'...


1 Answers

One way would be to re-initialize the remote repository completely cleanly and then do a push from local.

On the server, in the directory where the bare repository should live, do

 git init --bare

On the client (local repo), set the new origin if it has changed, and push to the server, and set up tracking again.

git remote set-url origin <Remote_URL>
git push origin master
git branch --set-upstream-to=origin/master master
like image 189
merlin2011 Avatar answered Sep 23 '22 19:09

merlin2011