How to convert a normal Git repository to a bare one?

后端 未结 17 984
礼貌的吻别
礼貌的吻别 2020-11-22 08:05

How can I convert a \'normal\' Git repository to a bare one?

The main difference seems to be:

  • in the normal Git repository, you have a .git

相关标签:
17条回答
  • 2020-11-22 08:32

    Here's what I think is safest and simplest. There is nothing here not stated above. I just want to see an answer that shows a safe step-by-step procedure. You start one folder up from the repository (repo) you want to make bare. I've adopted the convention implied above that bare repository folders have a .git extension.

    (1) Backup, just in case.
        (a) > mkdir backup
        (b) > cd backup
        (c) > git clone ../repo
    (2) Make it bare, then move it
        (a) > cd ../repo
        (b) > git config --bool core.bare true
        (c) > mv .git ../repo.git
    (3) Confirm the bare repository works (optional, since we have a backup)
        (a) > cd ..
        (b) > mkdir test
        (c) > cd test
        (d) > git clone ../repo.git
    (4) Clean up
        (a) > rm -Rf repo
        (b) (optional) > rm -Rf backup/repo
        (c) (optional) > rm -Rf test/repo
    
    0 讨论(0)
  • 2020-11-22 08:32

    In case you have a repository with few local checkedout branches /refs/heads/* and few remote branch branches remotes/origin/* AND if you want to convert this into a BARE repository with all branches in /refs/heads/*

    you can do the following to save the history.

    1. create a bare repository
    2. cd into the local repository which has local checkedout branches and remote branches
    3. git push /path/to/bare/repo +refs/remotes/origin/:refs/heads/
    0 讨论(0)
  • 2020-11-22 08:33

    I think the following link would be helpful

    GitFaq: How do I make existing non-bare repository bare?

    $ mv repo/.git repo.git
    $ git --git-dir=repo.git config core.bare true
    $ rm -rf repo
    
    0 讨论(0)
  • 2020-11-22 08:35

    Simply read

    Pro Git Book: 4.2 Git on the Server - Getting Git on a Server

    which boild down to

    $ git clone --bare my_project my_project.git
    Cloning into bare repository 'my_project.git'...
    done.
    

    Then put my_project.git to the server

    Which mainly is, what answer #42 tried to point out. Shurely one could reinvent the wheel ;-)

    0 讨论(0)
  • 2020-11-22 08:36

    Wow, it's simply amazing how many people chimed in on this, especially considering it doesn't seem that not a single on stopped to ask why this person is doing what he's doing.

    The ONLY difference between a bare and non-bare git repository is that the non-bare version has a working copy. The main reason you would need a bare repo is if you wanted to make it available to a third party, you can't actually work on it directly so at some point you're going to have to clone it at which point you're right back to a regular working copy version.

    That being said, to convert to a bare repo all you have to do is make sure you have no commits pending and then just :

    rm -R * && mv .git/* . && rm -R .git
    

    There ya go, bare repo.

    0 讨论(0)
提交回复
热议问题