虽然Git从本质上讲是监控和跟踪文本的更改,但它的定位依然是版本控制系统。你可能已经以某种方式使用过git;由于其分布式特性,git成为了事实上的代码版本控制标准,与集中式Apache Subversion (SVN)截然相反。
安装Git
要检查是否在终端中安装了Git,请运行:
git version
# git version 2.25.1
https://git-scm.com/downloads
Ubuntu用户可以使用apt安装:sudo apt install git。
配置Git
我们需要配置的东西不多:
git config –global user.name "John Doe" && # your name
git config –global user.email johndoe@example.com && # your email
git config –global init.defaultbranch main # default branch name, to be compatible with GitHub
你可以通过以下方式查看当前的全局配置:
git config –global –list
# Type ":q" to close
git以纯文本形式存储配置,当然你也可以直接在~/.gitconfig或~/.config/git/config中编辑全局配置。
正如命令所建议的那样,删除–global将使这些命令的范围限定为当前文件夹。为了测试这一点,我们需要一个存储库。
创建新的存储仓库
存储仓库就是一个包含要跟踪的所有内容的文件夹。创建存储仓库请运行:
mkdir gitexample &&
cd gitexample &&
git init
# gitexample git:(main)
此命令在gitexample文件夹中创建了文件夹.git。隐藏的.git文件夹是一个存储仓库:所有本地配置和更改都存储在那里。
做一些变更
让我们在存储库中创建一些东西:
echo "Hello, Git" >> hello.txt
如果我们运行git status,我们将看到新创建的未被跟踪的文件:
git status
# On branch main
#
# No commits yet
#
# Untracked files:
# (use "git add <file>…" to include in what will be committed)
# hello.txt
#
# nothing added to commit but untracked files present (use "git add" to track)
接下来让我们来添加文件,可以直接这样做:
git add . # Or `git add hello.txt`, if we don't want all files
如果你现在检查存储库状态,你将看到文件已添加(也称为已暂存),但尚未提交:
git status
# On branch main
#
# No commits yet
#
# Changes to be committed:
# (use "git rm –cached <file>…" to unstage)
# new file: hello.txt
要记录更改,先提交:
git commit -m "Add hello.txt"
# [main (root-commit) a07ee27] Adds hello.txt
# 1 file changed, 2 insertions(+)
# create mode 100644 hello.txt
小提示:git commit -m <MESSAGE>是一个简写命令,你也可以使用git commit打开编辑器(主要是vim)并提供详细的提交描述。
让我们来检查更改记录:
git log
# type :q to close
将显示如下内容:
commit a07ee270d6bd0419a50d1936ad89b9de0332f375 (HEAD -> main)
Author: Your Name <your@email.address>
Date: Sun Jul 11 11:47:16 2021 +0200
Adds hello.txt
(END)
就这么容易!10 分钟入门 Git
虽然Git从本质上讲是监控和跟踪文本的更改,但它的定位依然是版本控制系统。你可能已经以某种方式使用过git;由于其分布式特性,git成为了事实上的代码版本控制标准,与集中式Apache Subversion (SVN)截然相反。 安装Git 要检查是否在终端中安装了Git,请运
本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/fuwuqi/xt/2021/1122/27310.html