在实际开发中,创建Git项目是常见的操作。本文将介绍四种创建Git项目的方式,包括使用git init初始化项目、git clone克隆项目、Shell脚本创建项目和扩展命令git new创建项目。
git init初始化项目
使用git init可以在本地初始化一个空的Git仓库,然后使用git add和git commit命令将代码添加到仓库并提交。
$ git init git-new
$ cd git-new
$ touch .gitignore .gitattributes LICENSE README.md git-new.sh
$ git add --all
$ git commit -m "Initial commit"
$ git status
$ git log
$ git ls-files
$ git remote add origin git@github.com:leitiannet/git-new.git
$ git push origin master
git clone克隆项目
使用git clone可以从远程仓库模板中复制项目到本地,使用git init重新初始化仓库,使用常规的Git命令来管理本地仓库。git clone支持通过文件系统或网络地址指定原始仓库,git clone会创建一个完整副本,即复制整个项目的内容,包括历史记录、分支、标签等。
$ git clone git@github.com:leitiannet/git-new.git
方式1:
$ PROJECTDIR=example; cp -R git-new $PROJECTDIR; rm $PROJECTDIR/.git -rf; git init $PROJECTDIR
方式2:
$ PROJECTDIR=example; rsync --exclude ".git/" -avh --no-perms git-new/ $PROJECTDIR; git init $PROJECTDIR
Shell脚本创建项目
将创建Git项目的命令封装成一个Shell脚本,方便后续用来快速创建其他项目。
$ vim git-new.sh
#!/usr/bin/env bash
#
# New git repository
# refer to https://github.com/tj/git-extras/blob/main/bin/git-setup
# see https://git-scm.com/docs/git-sh-setup
USAGE="\
[<options>] [<directory>]
options:
-m <msg> Use the given <msg> as the commit message
-h Show this help"
#SUBDIRECTORY_OK=
NONGIT_OK=Yes
. "$(git --exec-path)/git-sh-setup"
# global variable
COMMIT_MESSAGE='Initial commit'
if [ "$1" == "-m" ]; then
COMMIT_MESSAGE=$2
shift; shift
elif [ "$1" == "-h" ]; then
usage && exit 0
fi
gitdirexists() {
if [ -d ".git" ]; then
echo ".git directory already exists, aborting"
exit 1
fi
}
gitinit() {
git init
}
gittouch() {
for filename in "$@"; do
if [ ! -f "$filename" ]; then
touch "$filename" && git add "$filename"
fi
done
}
gitadd() {
git add .
}
gitcommit() {
git commit --allow-empty -m "$COMMIT_MESSAGE"
}
gitconfig() {
git config --global alias.gi '!f() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ ;}; f'
git config --global alias.ga '!f() { curl -sL https://gitattributes.com/api/$@ ;}; f'
echo "Use 'git gi Go > .gitignore' to set gitignore file."
echo "Use 'git ga go,vim > .gitattributes' to set gitattributes file."
}
dir=$(test -z "$*" && echo "." || echo "$*")
mkdir -p "$dir" \
&& cd "$dir" \
&& gitdirexists \
&& gitinit \
&& gittouch .gitignore .gitattributes LICENSE README.md \
&& gitadd \
&& gitcommit \
&& gitconfig
# 加上可执行权限
$ chmod a+x git-new.sh
# 将脚本文件的路径加入PATH中
$ export PATH=$PATH:`pwd`
# 切换到其他目录
$ cd ..
$ git-new.sh -h
usage: git new.sh [<options>] [<directory>]
options:
-m <msg> Use the given <msg> as the commit message
-h Show this help
$ git-new.sh -m "init example" example
Initialized empty Git repository in /home/example/.git/
[master (root-commit) c89a222] init example
4 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 .gitattributes
create mode 100644 .gitignore
create mode 100644 LICENSE
create mode 100644 README.md
Use 'git gi Go > .gitignore' to set gitignore file.
Use 'git ga go,vim > .gitattributes' to set gitattributes file.
说明:使用bash -x执行脚本来启用调试模式,它会输出每个执行的命令、参数列表以及相应的输出结果。
$ bash -x ./git-new/git-new.sh -m "init example" example
扩展命令创建项目
Git支持扩展子命令,使用git new方式创建新的Git项目,其实质就是让Git调用之前封装好的脚本文件。详见之前文章《Git知识点:功能扩展》。
# 创建硬链接,两者属性相同(不占用实际空间)
$ ln git-new.sh git-new
$ ls -ali git-new.sh git-new
1966289 -rwxr-xr-x 2 root root 1444 Aug 3 16:05 git-new
1966289 -rwxr-xr-x 2 root root 1444 Aug 3 16:05 git-new.sh
$ git add --all
$ git commit -m "Implement command"
$ git push origin master
# 将脚本文件的路径加入PATH中
$ export PATH=$PATH:`pwd`
# 切换到其他目录
$ cd ..
$ git new -h
usage: git new [<options>] [<directory>]
options:
-m <msg> Use the given <msg> as the commit message
-h Show this help
$ git new -m "new example" example
Initialized empty Git repository in /home/example/.git/
[master (root-commit) b697dba] new example
4 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 .gitattributes
create mode 100644 .gitignore
create mode 100644 LICENSE
create mode 100644 README.md
Use 'git gi Go > .gitignore' to set gitignore file.
Use 'git ga go,vim > .gitattributes' to set gitattributes file.