vaso123
vaso123

Reputation: 12391

How to restore origin from local repository

One of the best in git, that it is distributed. Let's say, we working on a project, all the latest branches are pulled and merged to my local repository.

Then a server crash happens, and there was no backup, no mirroring, etc.. So the central repository has lost.

Is it possible to restore the central repository with all the branches, from my local repository?

Something like git init --bare --shared=group ./repo.git on server, and then git please --recover --with=allbraches mylocal.repository to ssh://...

Upvotes: 2

Views: 1181

Answers (3)

Vy Do
Vy Do

Reputation: 52716

Do these commands

# Check remote Git server.
git remote -v

# If Git server changed or doesn't exist.
git remote set-url origin your_remote_git_server

# For example.
git remote set-url origin https://github.com/donhuvy/elasticsearch.git

# Check your permission on Git server. You must be master user.
# Force push all branches to remote Git server.
git push origin --all --force

# Force push all tags to remote Git server.
git push origin --tags --force

# Check result after the above steps by few command.
git status
git branch --all
git log --oneline -10
git log --oneline --graph --decorate --all

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142632

Since your remote was removed you don't have any origin yet so you will have to create a new repo on the server and than update your remote url to point to the new one

If you dont want the local branches simply push the ones you need.

Here is a scrip which im using to checkout all branches and than push them to the new remote

#!/bin/bash

# update the new origin 
# (or leave the same one if its the same as the original)

git remote set-url origin <new-url> 
# loop over all the original branches
# 1. check them out as local branches 
# 2. set the origin as the track branch
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
    git branch --track ${branch#remotes/origin/} $branch
done

# now push all branches and tags
git push origin --all    
git push origin --tags

What does the script do?

git branch -a
get a list of all the local branches

| grep remotes The branch names are : 'remotes/origin/' so this will remove the remotes from the branch names

| grep -v HEAD | grep -v master
remove the master (current branch) and HEAD which is an alias to the latest commit

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83577

From your local repo, git push origin --all. I am assuming that the new repo on the restored server has the same URL. If the URL has changed, then do git remote set-url origin <new URL> first.

Upvotes: 3

Related Questions