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

后端 未结 17 982
礼貌的吻别
礼貌的吻别 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:10

    Unless you specifically want or need to twiddle bits on the filesystem, it really is dead simple to create a bare version of a non-bare repository (mentioned in several other posts here). It’s part of git’s core functionality:

    git clone --bare existing_repo_path bare_repo_path

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

    Here is a little BASH function you can add to your .bashrc or .profile on a UNIX based system. Once added and the shell is either restarted or the file is reloaded via a call to source ~/.profile or source ~/.bashrc.

    function gitToBare() {
      if [ -d ".git" ]; then
        DIR="`pwd`"
        mv .git ..
        rm -fr *
        mv ../.git .
        mv .git/* .
        rmdir .git
    
        git config --bool core.bare true
        cd ..
        mv "${DIR}" "${DIR}.git"
    
        printf "[\x1b[32mSUCCESS\x1b[0m] Git repository converted to "
        printf "bare and renamed to\n  ${DIR}.git\n"
        cd "${DIR}.git"
      else
        printf "[\x1b[31mFAILURE\x1b[0m] Cannot find a .git directory\n"
      fi
    }
    

    Once called within a directory containing a .git directory, it will make the appropriate changes to convert the repository. If there is no .git directory present when called, a FAILURE message will appear and no file system changes will happen.

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

    Your method looks like it would work; the file structure of a bare repository is just what is inside the .git directory. But I don't know if any of the files are actually changed, so if that fails, you can just do

    git clone --bare /path/to/repo
    

    You'll probably need to do it in a different directory to avoid a name conflict, and then you can just move it back to where you want. And you may need to change the config file to point to wherever your origin repo is.

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

    The methods that say to remove files and muck about with moving the .git directory are not clean and not using the "git" method of doing something that's should be simple. This is the cleanest method I have found to convert a normal repo into a bare repo.

    First clone /path/to/normal/repo into a bare repo called repo.git

    git clone --bare /path/to/normal/repo
    

    Next remove the origin that points to /path/to/normal/repo

    cd repo.git
    git remote rm origin
    

    Finally you can remove your original repo. You could rename repo.git to repo at that point, but the standard convention to signify a git repository is something.git, so I'd personally leave it that way.

    Once you've done all that, you can clone your new bare repo (which in effect creates a normal repo, and is also how you would convert it from bare to normal)

    Of course if you have other upstreams, you'll want to make a note of them, and update your bare repo to include it. But again, it can all be done with the git command. Remember the man pages are your friend.

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

    I just wanted to push to a repository on a network path but git would not let me do that unless that repository was marked as bare. All I needed was to change its config:

    git config --bool core.bare true
    

    No need to fiddle with the files unless you want to keep it clean.

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

    i've read the answers and i have done this:

    cd repos
    mv .git repos.git
    cd repos.git
    git config --bool core.bare true # from another answer
    cd ../
    mv repos.git ../
    cd ../
    rm -rf repos/ # or delete using a file manager if you like
    

    this will leave the contents of repos/.git as the bare repos.git

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