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

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

提交回复
热议问题