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.

  • Continue Working on a sub branch while waiting a pull
  • How to use git-svn properly, svn-ers checkout svnrepo and git-ers clone gitrepo, committing carelessly
  • Is there a “git touch” so I can push the same file with a new timestamp?
  • Visual Studio Tools for Git commit system
  • Change Git stash message
  • Using Pagelime CMS with Version Control set up (git)
  • 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://...

  • I've accidently pushed to a git repo with the wrong user account. How can I undo it or change the user?
  • Why is it safe to rebase under these conditions?
  • pip freeze reports version number instead of git sha for certain package
  • Jira issue number in git commit message
  • git on command line using github git
  • Deployment of website on Amazon AWS
  • 3 Solutions collect form web for “How to restore origin from local repository”

    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.

    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
    

    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

    Git Baby is a git and github fan, let's start git clone.