社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

2022-01-30日记:学习《Pro Git》的第1天

四月不见 • 3 年前 • 260 次点击  

第1章 起步

1、git 的三种状态与三个工作区域:

git 有三种状态,你的文件可能处于其中之一:已提交(committed)、已修改(modified)和已暂存(staged)。 已提交表示数据已经安全的保存在本地数据库中。 已修改表示修改了文件,但还没保存到数据库中。 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。

由此引入 Git 项目的三个工作区域的概念: Git 仓库 工作目录 以及 暂存区域

Git 仓库目录是 Git 用来保存项目的元数据和对象数据库的地方。 这是 Git 中最重要的部分,从其它计算机克隆仓库时,拷贝的就是这里的数据。

工作目录是对项目的某个版本独立提取出来的内容。 这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改。

暂存区域是一个文件,保存了下次将提交的文件列表信息,一般在 Git 仓库目录中。 有时候也被称作“索引”,不过一般说法还是叫暂存区域。

基本的 Git 工作流程如下:

  • 在工作目录中修改文件。
  • 暂存文件,将文件的快照放入暂存区域。
  • 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 仓库目录。

如果 Git 目录中保存着的特定版本文件,就属于已提交状态。 如果作了修改并已放入暂存区域,就属于已暂存状态。 如果自上次取出后,作了修改但还没有放到暂存区域,就是已修改状态。

2、查看当前版本:

我的Windows上

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/tp601-249 (master)
$ git --version
git version 2.34.1.windows.1

我的Linux上

[nosee@noseecomputer ~]$ git --version
git version 2.25.1

3、配置文件

Git 自带一个 git config 的工具来帮助设置控制 Git 外观和行为的配置变量。 这些变量存储在三个不同的位置:
1)/etc/gitconfig 文件: 包含系统上每一个用户及他们仓库的通用配置。 如果使用带有 --system 选项的 git config 时,它会从此文件读写配置变量。
2)~/.gitconfig 或 ~/.config/git/config 文件:只针对当前用户。 可以传递 --global 选项让 Git 读写此文件。
3)当前使用仓库的 Git 目录中的 config 文件(就是 .git/config):针对该仓库。
每一个级别覆盖上一级别的配置,所以 .git/config 的配置变量会覆盖 /etc/gitconfig 中的配置变量。

在 Windows 系统中,Git 会查找 HOME 目录下(一般情况下是 C:\Users\ USER)的.gitconfig 文件。 Git 同样也会寻找 /etc/gitconfig 文件,但只限于 MSys 的根目录下,即安装 Git 时所选的目标位置。

如,以下也是修改配置文件信息(用户信息)的相关命令:

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

查看所有配置信息:

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/tp601-249 (master)
$ git config --list
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager-core
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
user.email=123nosee@123nosee.com
user.name=noseeWin
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.server.url=nosee@34.96.173.249:/home/nosee/git/tp601.git
remote.server.fetch=+refs/heads/*:refs/remotes/server/*
branch.master.remote=server
branch.master.merge=refs/heads/master
gui.wmstate=normal
gui.geometry=841x483+385+220 189 218

查看具体配置,如:

$ git config user.name
noseeWin

注:
git的配置文件一般是在你使用git的时候才生成的。所以,如果是刚安装好的git,可能找不上上面说的相关文件。

查看设置配置的命令语法: git help config man git-config

[nosee@noseecomputer ~]$ git help config
[nosee@noseecomputer ~]$ man git-config

第2章 Git 基础

1、获取 Git 仓库

方法1:现有项目或目录下导入所有文件到 Git 中, git init

[nosee@noseecomputer ~/git]$ git init
Initialized empty Git repository in /home/nosee/git/.git/
[nosee@noseecomputer ~/git]$ ls -alF
total 12
drwxrwxr-x 3 nosee nosee 4096 Jan 29 23:18 ./
drwxr-xr-x 5 nosee nosee 4096 Jan 29 23:16 ../
drwxrwxr-x 7 nosee nosee 4096 Jan 29 23:18 .git/
-rw-rw-r-- 1 nosee nosee    0 Jan 29 23:18 git学习-2.txt
[nosee@noseecomputer ~/git]$ git add .
[nosee@noseecomputer ~/git]$ git commit -m 'git学习第2章'

该命令将创建一个名为 .git 的子目录,这个子目录含有你初始化的 Git 仓库中所有的必须文件,这些文件是 Git 仓库的骨干。

方法2:从一个服务器克隆一个现有的 Git 仓库, git clone

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test
$ git clone nosee@192.168.112.129:/home/nosee/git git1
Cloning into 'git1'...
The authenticity of host '192.168.112.129 (192.168.112.129)' can't be established.
ED25519 key fingerprint is SHA256:4Te+R7T4c9T9B2LSwg6lQNZMkkySchgcwVFEPT4PfjE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.112.129' (ED25519) to the list of known hosts.
nosee@192.168.112.129's password:
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test
$ cd git1/
Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ ls -al
total 4
drwxr-xr-x 1 Administrator 197121 0 Jan 30 07:29 ./
drwxr-xr-x 1 Administrator 197121 0 Jan 30 07:29 ../
drwxr-xr-x 1 Administrator 197121 0 Jan 30 07:29 .git/
-rw-r--r-- 1 Administrator 197121 0 Jan 30 07:29 git学习-2.txt

Git 支持多种数据传输协议,上面的例子使用的是 SSH 传输协议。还可以使用 https:// 协议 或者 git:// 协议。

2、记录每次更新到仓库

工作目录下的每一个文件都不外乎这两种状态: 已跟踪(tracked) 未跟踪(untracked)

已跟踪的文件是指那些被纳入了版本控制的文件,已跟踪的文件又可以再分为三种状态:

  • 未修改(unmodified) ,或者叫 已提交(committed)
  • 已修改(modified)
  • 已暂存(staged)

工作目录中除已跟踪文件以外的所有其它文件都属于未跟踪文件,它们既不存在于上次快照的记录中,也没有放入暂存区。初次克隆某个仓库的时候,工作目录中的所有文件都属于已跟踪文件,并处于未修改状态。

①③:添加文件到“已暂存”状态。( git add
②:从“未修改(已提交)”状态到“已修改”状态。(修改文件)
④:从“未修改(已提交)”状态到“未跟踪”状态。( mv 移动 / 重命名文件,【这里可以理解为是两个步骤,删除原文件,然后新建一个名字与原文件不一样但是内容一样的新文件】)
⑤:从“已暂存”状态到“已提交(未修改)”。( git commit

1)检查当前文件状态
要查看哪些文件处于什么状态,可以用 git status 命令。

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-01

nothing added to commit but untracked files present (use "git add" to track)

如上就是我新建的未被跟踪的文件 file-01,使用命令 git add 开始跟踪这个文件。

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git add file-01

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   file-01

文件状态简览:
git status 命令的输出十分详细,如果使用 git status -s 命令或 git status --short 命令,将得到一种更为紧凑的格式输出。

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   file-02
        modified:   git-study-2.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   file-01

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-03


Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status -s
 M file-01
A  file-02
M  git-study-2.txt
?? file-03

如果 git status 命令的输出对于你来说过于模糊,你想知道具体修改了什么地方,可以用 git diff 命令。

2)查看已暂存和未暂存的修改
要查看尚未暂存的文件更新了哪些部分,不加参数直接输入 git diff ,如:

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git diff
warning: LF will be replaced by CRLF in file-01.
The file will have its original line endings in your working directory
diff --git a/file-01 b/file-01
index d1d06ad..8ad79bd 100644
--- a/file-01
+++ b/file-01
@@ -1 +1 @@
-1111
\ No newline at end of file
+1111111

此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容。

若要查看已暂存的将要添加到下次提交里的内容,可以用 git diff --cached 命令。(Git 1.6.1 及更高版本还允许使用 git diff --staged,效果是相同的,但更好记些。)

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git diff --staged
diff --git a/file-02 b/file-02
new file mode 100644
index 0000000..e69de29
diff --git a/git-study-2.txt b/git-study-2.txt
index e2ebe3d..c533178 100644
--- a/git-study-2.txt
+++ b/git-study-2.txt
@@ -1 +1 @@
-<B5><DA>һ<CC>죬<CF><D6><D4><DA>ѧ<B5><BD><B5>ڶ<FE><D5><C2><C1><CB>
\ No newline at end of file
+第一天,现在学到第二章了。
\ No newline at end of file

3)忽略文件

一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。例:

*.[oa]
*~

第一行告诉 Git 忽略所有以 .o 或 .a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。第二行告诉 Git 忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副本。此外,你可能还需要忽略 log,tmp 或者 pid 目录,以及自动生成的文档等等。要养成一开始就设置好 .gitignore 文件的习惯,以免将来误提交这类无用的文件。

文件 .gitignore 的格式规范如下:

  • 所有空行或者以 # 开头的行都会被 Git 忽略。
  • 可以使用标准的 glob 模式匹配。
  • 匹配模式可以以(/)开头防止递归。
  • 匹配模式可以以(/)结尾指定目录。
  • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。星号(*)匹配零个或多个任意字符;[abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。使用两个星号(*) 表示匹配任意中间目录,比如a/**/z 可以匹配 a/z, a/b/z 或 a/b/c/z等。

与 linux 命令 ls 的通配符模式类似。

我们再看一个 .gitignore 文件的例子:

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

4)提交更新

现在的暂存区域已经准备妥当可以提交了。在此之前,请一定要确认还有什么修改过的或新建的文件还没有 git add 过,否则提交的时候不会记录这些还没暂存起来的变化。这些修改过的文件只保留在本地磁盘。所以,每次准备提交前,先用 git status 看下,是不是都已暂存起来了,然后再运行提交命令 git commit

git commit 这种方式会启动文本编辑器以便输入本次提交的说明。(默认会启用 shell 的环境变量 $EDITOR 所指定的软件,一般都是 vim 或 emacs。当然也可以按照 Chapter 1 介绍的方式,使用 git config --global core.editor 命令设定你喜欢的编辑软件。)

编辑器会显示类似下面的文本信息(本例选用 Vim 的屏显方式展示):


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#       new file:   file-02
#       modified:   git-study-2.txt
#
# Changes not staged for commit:
#       modified:   file-01
#
# Untracked files:
#       file-03
#

注释行里,另外开头还有一空行,供你输入提交说明。你完全可以去掉这些注释行,不过留着也没关系,多少能帮你回想起这次更新的内容有哪些。(如果想要更详细的对修改了哪些内容的提示,可以用 -v 选项,这会将你所做的改变的 diff 输出放到编辑器中从而使你知道本次提交具体做了哪些修改。)退出编辑器时,Git 会丢掉注释行,用你输入提交附带信息生成一次提交。

另外,你也可以在 commit 命令后添加 -m 选项,将提交信息与命令放在同一行。

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git commit -v
[master 3b60146] 测试-v提交
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 file-02

可以看到,提交后它会告诉你,当前是在哪个分支(master)提交的,本次提交的完整 SHA-1 校验和是什么(463dc4f),以及在本次提交中,有多少文件修订过,多少行添加和删改过。

请记住,提交时记录的是放在暂存区域的快照。每一次运行提交操作,都是对你项目作一次快照,以后可以回到这个状态,或者进行比较。

5)跳过使用暂存区域

管使用暂存区域的方式可以精心准备要提交的细节,但有时候这么做略显繁琐。Git 提供了一个跳过使用暂存区域的方式,只要在提交的时候,给 git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤。

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   file-01

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-03

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git commit -a
warning: LF will be replaced by CRLF in file-01.
The file will have its original line endings in your working directory
[master 5ab1e65] 使用git commit -a提交
 1 file changed, 1 insertion(+), 1 deletion(-)

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-03

nothing added to commit but untracked files present (use "git add" to track)

6)移除文件

要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交。可以用 git rm 命令完成此项工作,并连带从工作目录中删除指定的文件,这样以后就不会出现在未跟踪文件清单中了。

如果只是简单地从工作目录中手工删除文件,运行 git status 时就会在 “Changes not staged for commit” 部分(也就是 未暂存清单)看到。如下可看到两者的区别:

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git rm file-02
rm 'file-02'

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ rm file-01

Administrator@PC-20210327ZZHG MINGW64 /e/phpstudy_pro/WWW/git-test/git1 (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    file-02

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    file-01

如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f。这是一种安全特性,用于防止误删还没有添加到快照的数据,这样的数据不能被 Git 恢复。

7)移动文件


---【END】--- `

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/126593
 
260 次点击