Git 通常并非默认安装项,在设置 Git 环境之前,需要自行安装 git 软件包。
在 CentOS Linux 发行版中安装 Git 的过程如下:
$ sudo dnf install git
...
Complete!
$ which git
/usr/bin/git
在 Ubuntu Linux 发行版中安装 Git 的过程如下:
$ sudo apt install git
...
Processing triggers for man-db (2.9.1-1) ...
$ which git
/usr/bin/git
安装好 git 软件包之后,为新的脚本项目设置 Git 环境涉及以下 4 个基本步骤:
具体步骤如下:
1、首先,创建工作目录。在本地主目录下创建一个子目录即可:
$ mkdir MWGuard
$
$ cd MWGuard/
$
$ pwd
/home/christine/MWGuard
$
2、然后,在工作目录中初始化.git/子目录。这要用到 git init 命令:
$ git init
Initialized empty Git repository in /home/christine/MWGuard/.git/
$
$ ls -ld .git
drwxrwxr-x 7 christine christine 4096 Aug 24 14:49 .git
$
3、如果是首次使用,则设置name和email:
$ git config --global user.name "Christine Bresnahan"
$
$ git config --global user.email "cbresn1723@gmail.com"
$
$ git config --get user.name
Christine Bresnahan
$
$ git config --get user.email
cbresn1723@gmail.com
$
4、配置好本地 Git 环境之后,就可以建立项目的远程仓库了。建立好项目的远程仓库之后,需要把仓库地址记下来。随后向远程仓库发送项目文件时, 要 用到这个地址。
要查看这些文件中的各种配置信息, 可以使用 git config --list 命令:
$ git config --list
user.name=Christine Bresnahan
user.email=cbresn1723@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
$ cat /home/christine/.gitconfig
[user]
name = Christine Bresnahan
email = cbresn1723@gmail.com
$
$ cat /home/christine/MWGuard/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
$
尽管 Git 能够处理任意文件类型,但其相关工具主要针对的是纯文本文件,比如 bash shell 脚本。因此要注意,不是所有的 git 工具都能用于非文本文件。
建立好 Git 环境之后,就可以使用它的各种组织功能了。这也有 4 个基本步骤:
例子:
1、创建好脚本之后, 使用 git add 命令将其添加到暂存区(索引)。由于该脚本目前不在工作 目录/home/christine/MWGuard 中,因此需要先把它复制过来。然后切换到工作目录(通过 pwd 命令确认),执行 git add 命令:
$ pwd
/home/christine/scripts
$
$ cp MyGitExampleScript.sh /home/christine/MWGuard/
$
$ cd /home/christine/MWGuard/
$
$ pwd
/home/christine/MWGuard
$
$ ls *.sh
MyGitExampleScript.sh
$
$ git add MyGitExampleScript.sh
$
$ git status
[…]
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: MyGitExampleScript.sh
$
2、下一步是使用 git commit 命令将项目提交至本地仓库。可以使用-m 选项来添加注释,这有助于记录( documenting )提交。
$ git commit -m "Initial Commit"
[…] Initial Commit
1 file changed, 5 insertions(+)
create mode 100644 MyGitExampleScript.sh
$
$ cat .git/COMMIT_EDITMSG
Initial Commit
$
$ git status
[…]
nothing to commit, working tree clean
$
3、创建 README.md 文件,将其加入暂存区并提交到本地仓库。
$ pwd
/home/christine/MWGuard
$
$ ls
MyGitExampleScript.sh
$
$ echo "# Milky Way Guardian" > README.md
$ echo "## Script Project" >> README.md
$
$ cat README.md
# Milky Way Guardian
## Script Project
$
$ git add README.md
$
$ git status
[...]
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: README.md
$
$ git commit -m "README.md commit"
[...] README.md commit
1 file changed, 2 insertions(+)
create mode 100644 README.md
$
$ git status
[...]
nothing to commit, working tree clean
$
如果需要,可以将当前工作目录中的所有脚本同时添加到暂存区(索引)。只需使用 git
add .命令即可。注意,该命令结尾有个点号(.)! 这个点号相当于一个通配符,告诉 Git 把工作目录中的所有文件都加入暂存区。但是,如果不想把某些文件添加到暂存区,则可以在工作目录中创建一个.gitignore 文件,将不希望加入暂存区的文件或目录名写入该文件。这样, git add .命令就会忽略这些文件或目录,只把其他的文件或目录加入暂存区。
暂存区的索引文件是.git/index。如果对该文件使用 file 命令,则其类型会显示为 Git index。Git 会使用此文件跟踪变更:
$ file .git/index
.git/index: Git index, version 2, 1 entries
$
如果这是一个新的脚本项目,那么在注册过远程仓库账户后,需要创建一个称为 Markdown file 的特殊文件,其内容会显示在远程仓库的 Web 页面上,描述该仓库的相关信息。该文件使用 Markdown 语言编写。你需要将文件命名为 README.md。
可以随时查看 Git 日志,但最好在将脚本项目推送到远程存储库之前做这件事。每次提交都有一个对应的哈希值作为标识,这个值也会出现在日志中。此外,请注意各种注释以及日期和作者信息。
$ git log
commit 898330bd0b01e0b6eee507c5eeb3c72f9544f506[...]
Author: Christine Bresnahan
Date: Mon Aug 24 15:58:52 2020 -0400
README.md commit
commit 3b484638bc6e391d0a1b816946cba8c5f4bbc8e6
Author: Christine Bresnahan
Date: Mon Aug 24 15:46:56 2020 -0400
Initial Commit
$
在向远程仓库推送项目之前,需要先在系统中配置远程仓库地址。在使用 Git 服务提供商 (比如 GitHub)设置远程仓库时,它会向你提供此地址。
可以使用 git remote add origin URL 命令来添加地址, 其中 URL 就是远程仓库地址:
$ git remote add origin https://github.com/C-Bresnahan/MWGuard.git
$
$ git remote -v
origin https://github.com/C-Bresnahan/MWGuard.git (fetch)
origin https://github.com/C-Bresnahan/MWGuard.git (push)
$
配置好远程仓库地址之后,就可以向其推送脚本项目了。但在此之前,为简单起见,我们打算使用 git branch 命令把主分支重命名为main:
$ git branch -m main
$
$ git branch --show-current
main
$
将脚本复制到远程仓库。这需要在 push 命令中加入-u origin 选项来指定仓库位置和当前使用的分支名 main:
$ git remote add origin https://github.com/C-Bresnahan/MWGuard.git
$
$ git push -u origin main
Username for 'https://github.com ': C-Bresnahan
Password for 'https://C-Bresnahan@github.com ':
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 604 bytes | 60.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/C-Bresnahan/MWGuard.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
$
远程仓库真正的美妙之处在于, Linux 管理团队中参与此项目的任何人都可以使用 git pull 命令从中拉取最新版本的脚本。
$ git remote add origin https://github.com/C-Bresnahan/MWGuard.git
$
$ git pull origin main
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 584 bytes | 58.00 KiB/s, done.
From https://github.com/C-Bresnahan/MWGuard
* branch
* [new branch]
$
如果未参与该项目的人想得到脚本的最新版本, 那么当他们尝试使用 git remote add origin 命令时, 会收到类似于 fatal: not a git repository 的错误消息。对这些人而言, 最好先克隆该项目。
开发团队的新成员可以使用 git clone 命令将整个脚本项目从远程仓库复制到自己的本地 系统:
$ git clone https://github.com/C-Bresnahan/MWGuard.git
Cloning into 'MWGuard'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 584 bytes | 58.00 KiB/s, done.
$
$ ls
MWGuard
$
$ cd MWGuard/
$
$ ls -a
. .. .git MyGitExampleScript.sh README.md
$
$ git log
commit [...](HEAD -> main, origin/main, origin/HEAD)
Author: Christine Bresnahan
Date: Mon Aug 24 15:58:52 2020 -0400
README.md commit
commit 3b484638bc6e391d0a1b816946cba8c5f4bbc8e6
Author: Christine Bresnahan
Date: Mon Aug 24 15:46:56 2020 -0400
Initial Commit
$