您的位置:首页 > 运维架构 > Shell

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

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

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

#!/bin/bash
# (C) 2014 Yunlong Zhou <reaper888@yeah.net>    
# Under licence  GPL    
# File :   mkgit.sh
# Introduction:    
#       This script is using for create a git repository
# Useage :
#    Server Part :    
#       $ ./mkgit.sh 
#       Please input the git name you want to create: git-test	<-- Input git repository name 
#       Now create a dir named "git-test"
#       Please config user name: zyl				<-- Input your name to recognise in git log
#       Got your user name : "zyl"
#       Please config user email address: reaper888@yeah.net	<-- your email address
#       Got your user email address : "reaper888@yeah.net"
#       Initialized empty Git repository in /home/pi/repository/git-test/.git/
#       [master (root-commit) 4e52852] For initialize the git repository -- git-test
#        1 file changed, 1 insertion(+)
#        create mode 100644 README
#       pi@192.168.2.226's password: 				<-- Need to input your IP address
#       Everything up-to-date						<-- Done
#       $ cat git-test/.git/config 
#       ...
#       url = pi@192.168.2.226:/home/pi/repository/git-test  
#                \                                        /
#                 ----> This part is important       <----
#    Client Part:
#       $ git clone pi@192.168.2.226:/home/pi/repository/git-test	
#                      \--> git clone username@url-above, other username is ok(if authority is ok)
#       Cloning into 'git-test'...
#       pi@192.168.2.226's password: 						 <-- input your password	
#       remote: Counting objects: 3, done.
#       Receiving objects: 100% (3/3), 269 bytes, done.
#       remote: Total 3 (delta 0), reused 0 (delta 0)	

if read -t 15  -p "Please input the git name you want to create: "
then
	git_name=$REPLY
    echo "Now create a dir named \"$git_name\""
else
    echo -e "\nYou are too slow!"
	exit -1
fi 

if read -p "Please config user name: "
then
	user_name=$REPLY
    echo "Got your user name : \"$user_name\""
fi

if read -p "Please config user email address: "
then
	user_email=$REPLY
    echo "Got your user email address : \"$user_email\""
fi 
cur_user=`who | grep pts/0 | cut -d ' '  -f1`
mkdir $git_name
cd $git_name
cur_address=`pwd`
ip_addr=`sudo ifconfig | grep "inet addr" | cut -d":" -f2 | cut -d " " -f1 |sed  "/127*/d"`
if [ $? -ne 0 ]
then
	if read -p  "Sorry, get your IP address error, please input manually: "
	then 
		ip_addr=$REPLY
		echo "Got your IP address : \"$ip_addr\""
	fi
fi
#git --bare init --shared
git init
echo "This is the first file of $git_name repository" > README

git config --global user.name $user_name 
git config --global user.email $user_email 
git add README
git commit -m "For initialize the git repository -- $git_name"
git remote add origin $cur_user@$ip_addr:$cur_address
git push origin master
echo -e "[receive]\n\tdenyCurrentBranch = ignore" >> $cur_address/.git/config


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

对于客户端的 push ,在服务器段不会即时显示,如果你想要在服务器端git 仓库查看最新的仓库信息,请使用 “git reset --hard” 命令。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: