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.

  • Migrating from SCCS to Git
  • git merge from subtree folder in master to branch root
  • Laravel Homestead Installation: bash not working
  • How do I remove a repository from git gui?
  • Revert to previous commit and ignore any uncommited changes in git
  • Is there a method to update all of my textmate bundles at once from the git repository?
  • 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://...

  • What does the option “Auto share projects located in git repository” of EGit mean?
  • Why are unstaged changes still present after checking out a different branch?
  • using git-flow with multiple git repositories per project/app?
  • Git cancel a revert
  • Why does git help add “git-” before every command name?
  • With “git svn”, Can I Ignore Specific Git Commits While Fetching?
  • 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
    # 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

    # 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
    # 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.