Shell 脚本小试牛刀(4) -- 创建 git 仓库

来源:互联网 发布:匡恩网络武汉 编辑:程序博客网 时间:2024/05/18 17:27

之前写过一个《Git 使用及进阶实战》,其中讲解了很多Git 的基本用法,包括创建仓库等使用以及一些错误排除,还是挺好的 GIT 普及博文。

我经常会在本地家用主机 /服务器上创建 git 仓库,可能一个语言的学习或者一个项目都需要一个git 仓库。不得不说创建仓库还是挺烦人的,有的时候会遗漏或者忘记命令,于是我写了一个简单的脚本来简化创建GIT 仓库的繁杂无趣的过程:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. # (C) 2014 Yunlong Zhou <reaper888@yeah.net>      
  3. # Under licence  GPL      
  4. # File :   mkgit.sh  
  5. # Introduction:      
  6. #       This script is using for create a git repository  
  7. # Useage :  
  8. #    Server Part :      
  9. #       $ ./mkgit.sh   
  10. #       Please input the git name you want to create: git-test  <-- Input git repository name   
  11. #       Now create a dir named "git-test"  
  12. #       Please config user name: zyl                <-- Input your name to recognise in git log  
  13. #       Got your user name : "zyl"  
  14. #       Please config user email address: reaper888@yeah.net    <-- your email address  
  15. #       Got your user email address : "reaper888@yeah.net"  
  16. #       Initialized empty Git repository in /home/pi/repository/git-test/.git/  
  17. #       [master (root-commit) 4e52852] For initialize the git repository -- git-test  
  18. #        1 file changed, 1 insertion(+)  
  19. #        create mode 100644 README  
  20. #       pi@192.168.2.226's password:                <-- Need to input your IP address  
  21. #       Everything up-to-date                       <-- Done  
  22. #       $ cat git-test/.git/config   
  23. #       ...  
  24. #       url = pi@192.168.2.226:/home/pi/repository/git-test    
  25. #                \                                        /  
  26. #                 ----> This part is important       <----  
  27. #    Client Part:  
  28. #       $ git clone pi@192.168.2.226:/home/pi/repository/git-test     
  29. #                      \--> git clone username@url-above, other username is ok(if authority is ok)  
  30. #       Cloning into 'git-test'...  
  31. #       pi@192.168.2.226's password:                         <-- input your password   
  32. #       remote: Counting objects: 3, done.  
  33. #       Receiving objects: 100% (3/3), 269 bytes, done.  
  34. #       remote: Total 3 (delta 0), reused 0 (delta 0)     
  35.   
  36. if read -t 15  -p "Please input the git name you want to create: "  
  37. then  
  38.     git_name=$REPLY  
  39.     echo "Now create a dir named \"$git_name\""  
  40. else  
  41.     echo -e "\nYou are too slow!"  
  42.     exit -1  
  43. fi   
  44.   
  45. if read -p "Please config user name: "  
  46. then  
  47.     user_name=$REPLY  
  48.     echo "Got your user name : \"$user_name\""  
  49. fi  
  50.   
  51. if read -p "Please config user email address: "  
  52. then  
  53.     user_email=$REPLY  
  54.     echo "Got your user email address : \"$user_email\""  
  55. fi   
  56. cur_user=`who | grep pts/0 | cut -d ' '  -f1`  
  57. mkdir $git_name  
  58. cd $git_name  
  59. cur_address=`pwd`  
  60. ip_addr=`sudo ifconfig | grep "inet addr" | cut -d":" -f2 | cut -d " " -f1 |sed  "/127*/d"`  
  61. if [ $? -ne 0 ]  
  62. then  
  63.     if read -p  "Sorry, get your IP address error, please input manually: "  
  64.     then   
  65.         ip_addr=$REPLY  
  66.         echo "Got your IP address : \"$ip_addr\""  
  67.     fi  
  68. fi  
  69. #git --bare init --shared  
  70. git init  
  71. echo "This is the first file of $git_name repository" > README  
  72.   
  73. git config --global user.name $user_name   
  74. git config --global user.email $user_email   
  75. git add README  
  76. git commit -m "For initialize the git repository -- $git_name"  
  77. git remote add origin $cur_user@$ip_addr:$cur_address  
  78. git push origin master  
  79. echo -e "[receive]\n\tdenyCurrentBranch = ignore" >> $cur_address/.git/config  

此脚本是用来创建GIT 仓库的,创建好后客户端克隆使用即可,具体如何使用在脚本前部分的Usage 中已有简单描述。最简单的办法是你简单试一下吧。


对于客户端的 push ,在服务器段不会即时显示,如果你想要在服务器端git 仓库查看最新的仓库信息,请使用 “git reset --hard” 命令。
0 0
原创粉丝点击