Git详解

目录

Git 简介

Git 的基本概念

安装Git        注:本虚拟机为Rocky_linux9.4进行下述操作

初次运行 Git 前的配置

查看Git帮助 

1. 使用 git help 命令

2. 使用 git --help 选项

3. 使用 man git-

4. 在线文档

5. 使用 git -h 选项

6. Git 命令常规操作及其参数总结 

获取 Git 仓库(初始化仓库)

1. 创建裸库

2. 克隆远程主机仓库 

3. 创建本地仓库

4. 从代码托管平台克隆

使用https方式克隆 

使用ssh方式克隆

5. git log 命令及其选项

添加代码到远程仓库

1. 常用操作示意图

2. 文件的状态变化周期

3. 添加代码到暂存区

4. 提交代码更改到本地仓库

5. 推送到远端仓库

验证

分支介绍 

1. 什么是分支?

2. 分支的基本操作

3. 分支管理策略


Git 简介

Git 是一个开源的分布式版本控制系统,由 Linus Torvalds 于 2005 年开发。它的设计目标是高效地管理各种规模的项目源代码,并允许多个开发者在同一个项目上进行协作。

Git 的基本概念

  1. 仓库(Repository):存储项目代码和版本历史的地方,可以是本地仓库(在你自己的电脑上)或远程仓库(如 GitHub、GitLab 等)。
  2. 工作区(Working Directory):当前正在进行修改的项目代码所在的目录。
  3. 暂存区(Staging Area/Index):临时存放你的修改,等待提交到仓库的地方。
  4. 提交(Commit):将暂存区的修改记录到仓库中的操作,每次提交都会生成一个唯一的提交 ID(哈希值),用于标识此次提交。
  5. 分支(Branch):在版本控制中,分支是项目的不同开发线路。Git 默认有一个名为 mainmaster 的主分支,你可以创建新的分支进行开发。

安装Git        注:本虚拟机为Rocky_linux9.4进行下述操作

[root@localhost ~]# yum install -y git

初次运行 Git 前的配置

命令集

git config --global user.name "username"  #配置git使用用户,需要自己改个用户名
git config --global user.email "email@mail.com"  #配置git使用邮箱,需要自己改个邮箱
git config --global color.ui true  #语法高亮



[root@localhost ~]# git config --list    # 查看全局配置
user.name=lzz
user.email=email@mail.com
color.ui=true
[root@localhost ~]# ls -a                #或者查看.gitconfig这个隐藏文件
.   .bash_history  .bash_profile  .cshrc      .lesshst           .ssh     .viminfo    anaconda-ks.cfg             rocky_linux.sh
..  .bash_logout   .bashrc        .gitconfig  .oracle_jre_usage  .tcshrc  .wget-hsts  jdk-8u151-linux-x64.tar.gz
[root@localhost ~]# cat .gitconfig 
[user]
	name = lzz
	email = email@mail.com
[color]
	ui = true

查看Git帮助 

1. 使用 git help 命令
  • 显示 Git 的总体帮助文档:

    git help
  • 查看特定命令的帮助文档:

    git help <command>

    例如,要查看 git commit 命令的帮助文档:

    git help commit

2. 使用 git <command> --help 选项

这种方法与 git help <command> 类似,可以查看特定命令的帮助文档:

git <command> --help

例如,要查看 git status 命令的帮助文档:

git status --help

3. 使用 man git-<command>

在 Unix 系统上,你可以使用 man 命令查看 Git 命令的手册页:

man git-<command>

例如,要查看 git log 命令的手册页:

man git-log

4. 在线文档

Git 的官方网站提供了详细的文档和教程,可以通过访问以下链接获取:

5. 使用 git <command> -h 选项

这种方法通常显示的是命令的简短帮助信息:

git <command> -h

例如,要查看 git push 命令的简短帮助信息:

git push -h

6. Git 命令常规操作及其参数总结 
命令 作用 常用参数及解释 示例
git init 初始化一个新的 Git 仓库 git init
git clone 克隆一个远程仓库到本地 <repository_url> git clone https://github.com/user/repo.git
git status 查看当前工作目录的状态 git status
git add 添加文件到暂存区 <file>.(添加所有文件) git add README.md
git commit 提交暂存区的内容到仓库 -m "<message>" git commit -m "Initial commit"
git log 查看提交历史 --oneline(简洁输出),-n <number>(限制显示数量) git log --oneline
git remote 管理远程仓库 add <name> <url>(添加远程仓库) git remote add origin https://github.com/user/repo.git
git push 推送本地提交到远程仓库 <remote> <branch> git push origin master
git pull 从远程仓库拉取更新并合并到本地 git pull
git branch 列出、创建或删除分支 -a(列出所有分支),<branch>(创建新分支) git branch new-feature
git checkout 切换分支或恢复文件 <branch>(切换分支),<file>(恢复文件) git checkout master
git merge 合并分支 <branch> git merge new-feature
git reset 重置当前分支的 HEAD --hard(丢弃所有更改),--soft(保留更改) git reset --hard HEAD~1
git stash 暂存当前工作目录的修改 save "<message>"(保存暂存) git stash save "Work in progress"
git stash apply 恢复最近的暂存内容 git stash apply
git fetch 从远程仓库获取最新数据 git fetch
git diff 查看工作目录和暂存区的差异 git diff
git rm 从工作目录和暂存区删除文件 <file> git rm unwanted-file.txt
git mv 重命名文件 <old_name> <new_name> git mv oldname.txt newname.txt
git tag 创建标签 <tagname>-a <tagname> -m "<message>"(附注标签) git tag v1.0

获取 Git 仓库(初始化仓库)

1. 创建裸库
创建普通用户并设置密码为1
[root@localhost ~]# useradd lisi
[root@localhost ~]# echo "1" |passwd --stdin lisi

[root@localhost ~]# mkdir /opt/git
[root@localhost ~]# cd /opt/git

创建一个名为 myproject.git 的裸库
[root@localhost git]# git init --bare myproject.git     #需要在名字后有.git的扩展名

授权给这个普通用户
[root@localhost git]# chown -R lisi:lisi myproject.git
[root@localhost git]# ll
total 0
drwxr-xr-x 7 lisi lisi 119 Jul  9 13:06 myproject.git

[root@localhost git]# ll myproject.git/
total 16
-rw-r--r-- 1 lisi lisi   23 Jul  9 13:06 HEAD
drwxr-xr-x 2 lisi lisi    6 Jul  9 13:06 branches
-rw-r--r-- 1 lisi lisi   66 Jul  9 13:06 config
-rw-r--r-- 1 lisi lisi   73 Jul  9 13:06 description
drwxr-xr-x 2 lisi lisi 4096 Jul  9 13:06 hooks
drwxr-xr-x 2 lisi lisi   21 Jul  9 13:06 info
drwxr-xr-x 4 lisi lisi   30 Jul  9 13:06 objects
drwxr-xr-x 4 lisi lisi   31 Jul  9 13:06 refs


在裸仓库(bare repository)中,只能通过 git log 和 git show 等命令查看提交历史和具体文件的变更内容。由于裸仓库没有工作目录,因此无法像在普通的工作目录中那样直接查看文件内容或目录结构。

一些目录功能介绍:
HEAD:
文件,记录了当前所在分支的引用。
示例内容可能是 ref: refs/heads/master,表示当前在 master 分支上。

branches:
目录,用于存放各个分支的引用文件。

config:
文件,存储了 Git 仓库的配置信息,如用户名、邮箱、远程仓库地址等。

description:
文件,对仓库的描述信息。

hooks:
目录,包含了客户端或服务端的钩子脚本,可以在特定事件发生时触发自定义操作。

index:
文件,存储了暂存区的内容,记录了即将提交到仓库的文件列表和状态信息。

info:
目录,包含了全局的 Git 配置文件和其他信息。

logs:
目录,存放了各种引用的更新记录。

objects:
目录,存储了 Git 的对象数据库,包括提交对象、树对象和 blob 对象。

packed-refs:
文件,包含了所有的引用信息。

refs:
目录,存放了所有的引用(分支和标签)。

这些文件和目录组成了 Git 仓库的核心结构,每个文件和目录都承担着不同的角色,用于存储和管理仓库的元数据、配置信息、历史记录以及实际的文件内容。
2. 克隆远程主机仓库 

起用一个新的虚拟机,下载git,进行克隆,为了区分两台主机,我将新增这个主机名改为bingou

[root@bingou ~]# git clone lisi@192.168.226.20:/opt/git/myproject.git  #回车执行命令后输入lisi用户的密码,在前面我设置密码为1
[root@bingou ~]# ll
total 4
-rw-------. 1 root root 815 Jun  6 14:00 anaconda-ks.cfg
drwxr-xr-x  3 root root  18 Jun 26 15:00 myproject

[root@bingou ~]# cd myproject/
[root@bingou myproject]# ll -a
total 4
drwxr-xr-x  3 root root   18 Jun 26 15:00 .
dr-xr-x---. 4 root root 4096 Jun 26 15:01 ..
drwxr-xr-x  7 root root  119 Jun 26 15:01 .git

[root@bingou myproject]# ls .git/
HEAD  branches  config  description  hooks  info  objects  refs

[root@bingou myproject]# cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = lisi@192.168.226.20:/opt/git/myproject.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

3. 创建本地仓库
[root@bingou myproject]# cd /opt
[root@bingou opt]# git init shuijiao      #制作本地仓库的文件名后不用加上.git

[root@bingou opt]# ll
total 0
drwxr-xr-x 3 root root 18 Jun 26 15:10 shuijiao

#进入该目录
[root@bingou opt]# cd shuijiao/
[root@bingou shuijiao]# ls -a
.  ..  .git

4. 从代码托管平台克隆

 游戏/娱乐 - 游戏/娱乐 - 开源软件 - Gitee.com

 从里面随便找一个项目点进去

这是平台提供的克隆代码方式

使用https方式克隆 

[root@bingou shuijiao]# cd
[root@bingou ~]# git clone https://gitee.com/GITLZ/PlantsVsZombies.git

[root@bingou ~]# ll
total 8
drwxr-xr-x  4 root root 4096 Jun 26 15:23 PlantsVsZombies
-rw-------. 1 root root  815 Jun  6 14:00 anaconda-ks.cfg
drwxr-xr-x  3 root root   18 Jun 26 15:00 myproject

PlantsVsZombies这个文件夹就是克隆的源码仓库,现在我们将其删除进行方便下一步测试
[root@bingou ~]# rm -rf PlantsVsZombies/

使用ssh方式克隆

使用该方式如果直接复制ssh的命令是执行不成功的,需要先在本机中创建一对密钥

生成密钥对,执行命令后,一路回车即可生成
[root@bingou ~]# ssh-keygen

查看公钥并复制
[root@bingou ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDYJb2216RRlN1EFahzYbchH3+7MkDXsW+EpzYQAivF2azw8A6zBwNDks3kxBMCZd3JkDbD5PMpQUMKC9PW42m59pIC4o0zY9dL8J/F7YdBLLLZI9QtZVWB/OdE58FkGUG/PPKN7M2F8qy4/8i4x2anqUMLEYOs+MBdWbHIL+xX8XdIETn/iooVWYCasLq4p6zqptfP8C7TQhD2XRyERRme2G2xAS0uK854h4NtBPkw9mSeJBj17PjbP/VIOHUOknVSQ0ydKFP0mgPgxLPuQ6tKaJKSY4R9XZMKmLx+Yuy7PW9qrt+CohGLDai83dtwI+3MNXewxpfmn/9s/ZMVHdU1OwZfEF+tA8W9Mh12xb4y2hoPXDYg4HwhFWnHDFdeisnA4n178gdQ6im1APFWam7/0HHRRxil+i3TAsmFYp2I/Bp+zK1acrhGlSXWZ0RH5duLTbs2OJW0s+OYQzRXDkyCfnlDK0JApyhCvPVMS3glZ9i0xRR/8ZAJW+eFpH9ZvbE= root@bingou

登录gitee在个人设置里,有个安全设置,ssh公钥,将复制的公钥填写进去,点保存会提示输入gitee用户的密码,然后就可以保存成功了。 

 

 

[root@bingou ~]# git clone git@gitee.com:GITLZ/PlantsVsZombies.git

[root@bingou ~]# ll
total 8
drwxr-xr-x  4 root root 4096 Jun 26 15:39 PlantsVsZombies
-rw-------. 1 root root  815 Jun  6 14:00 anaconda-ks.cfg
drwxr-xr-x  3 root root   18 Jun 26 15:00 myproject

[root@bingou PlantsVsZombies]# ll -a
total 2560
drwxr-xr-x   4 root root    4096 Jun 26 15:39 .
dr-xr-x---.  5 root root    4096 Jun 26 15:38 ..
drwxr-xr-x   8 root root     163 Jun 26 15:39 .git
-rw-r--r--   1 root root    2518 Jun 26 15:39 .gitattributes
-rw-r--r--   1 root root    5799 Jun 26 15:39 .gitignore
-rw-r--r--   1 root root  249687 Jun 26 15:39 ClassDiagram.png
-rw-r--r--   1 root root   11357 Jun 26 15:39 LICENSE
drwxr-xr-x  10 root root    4096 Jun 26 15:39 PlantsVsZombies
-rw-r--r--   1 root root    3059 Jun 26 15:39 PlantsVsZombies.sln
-rw-r--r--   1 root root    2999 Jun 26 15:39 README.md
-rw-r--r--   1 root root    3098 Jun 26 15:39 README_en_us.md
-rw-r--r--   1 root root    4747 Jun 26 15:39 README_ru_ru.MD
-rw-r--r--   1 root root  976498 Jun 26 15:39 example.png
-rw-r--r--   1 root root 1332271 Jun 26 15:39 example1.png

git log 命令用于查看 Git 仓库的提交历史记录。它可以展示每次提交的信息,包括提交的哈希值、作者、提交日期和提交信息。

[root@bingou PlantsVsZombies]# git log
commit b5386f3004e638ec8b089fc5a902887f8935a4d7 (HEAD -> master, origin/master, origin/HEAD)
Author: LZ <2117610943@qq.com>
Date:   Sun Jun 2 09:26:09 2024 +0000

    update PlantsVsZombies/Resources/resources/资源说明(重要).txt.
    
    Signed-off-by: LZ <2117610943@qq.com>

commit 7f56c8d6b51187135b57b734101adab476896714
Author: LZ <2117610943@qq.com>
Date:   Wed May 1 04:30:51 2024 +0000

    update README.md.
    
    Signed-off-by: LZ <2117610943@qq.com>

commit ae7b8e12a01686a39d4590fa399935fb212cd5b4
Author: LZ <2117610943@qq.com>
Date:   Wed May 1 04:29:35 2024 +0000

    update README.md.
    
    Signed-off-by: LZ <2117610943@qq.com>

commit bee4f529cb17dad60dfb1a311faf35733a90a741
Author: LZ <2117610943@qq.com>
Date:   Wed May 1 04:28:34 2024 +0000

    update README.md.
    
    Signed-off-by: LZ <2117610943@qq.com>

:

5. git log 命令及其选项

基本用法

  1. 查看简单的提交日志
    git log
    这个命令会显示仓库的完整提交历史记录,按时间顺序从新到旧排列。

常用选项

  1. 显示简短的提交信息

    git log --oneline

    这个命令会显示每个提交的一行简短信息,包括提交的哈希值和提交信息。

  2. 限制显示的提交数量

    git log -n <number>

    例如,查看最近的 5 次提交:

    git log -n 5
  3. 按作者过滤

    git log --author="<author_name>"

    例如,查看由 "Alice" 提交的所有提交:

    git log --author="Alice"
  4. 按日期过滤

    git log --since="yyyy-mm-dd" --until="yyyy-mm-dd"

    例如,查看从 2023 年 1 月 1 日到 2023 年 6 月 30 日之间的提交:

    git log --since="2023-01-01" --until="2023-06-30"
  5. 查看提交的差异

    git log -p

    这个命令会显示每个提交所引入的具体变化(diff)。

  6. 显示统计信息

    git log --stat

    这个命令会显示每次提交的简要统计信息,包括添加和删除的行数。

  7. 按路径过滤

    git log -- <file_path>

    例如,查看 README.md 文件的提交历史:

    git log -- README.md

高级用法

  1. 图形化显示分支历史

    git log --graph --oneline --all

    这个命令会图形化显示所有分支的提交历史,并且每个提交用一行表示。

  2. 显示补丁(patch)和文件变动统计信息

    git log --patch --stat

    这个命令结合了 -p--stat 选项,显示每次提交的具体变化和统计信息。

  3. 显示提交信息的详细格式

    git log --pretty=format:"%h - %an, %ar : %s"

    这个命令允许你自定义显示的格式,例如:

    • %h:提交的简短哈希值
    • %an:作者名字
    • %ar:提交的相对时间
    • %s:提交信息
  4. 查看分支合并情况

    git log --merges

    这个命令会显示所有的合并提交。

示例

结合多个选项来获取更有用的信息,例如查看最近 3 次由 "Alice" 提交的、更改 README.md 文件的简短日志:

git log -n 3 --author="Alice" --oneline -- README.md

通过这些命令和选项,你可以根据自己的需要灵活地查看和过滤 Git 仓库的提交历史。

添加代码到远程仓库

1. 常用操作示意图

2. 文件的状态变化周期

 

3. 添加代码到暂存区

以前面创建的远程仓库为例,在创建裸库的例子中,在名为localhost主机中/opt/git目录中创建了名为myproject.git的仓库,并将克隆到了名为bingou的主机中

[root@bingou ~]# ll
total 8
drwxr-xr-x  4 root root 4096 Jun 26 15:39 PlantsVsZombies
-rw-------. 1 root root  815 Jun  6 14:00 anaconda-ks.cfg
drwxr-xr-x  3 root root   18 Jun 26 15:00 myproject
[root@bingou ~]# cd myproject/

[root@bingou myproject]# mkdir houduan
[root@bingou myproject]# echo "后端开发完成" > houduan/index.jsp

[root@bingou myproject]# ll -a
total 4
drwxr-xr-x  4 root root   33 Jun 26 16:16 .
dr-xr-x---. 5 root root 4096 Jun 26 15:38 ..
drwxr-xr-x  7 root root  119 Jun 26 15:01 .git
drwxr-xr-x  2 root root   23 Jun 26 16:17 houduan
[root@bingou myproject]# git add .      #添加到暂存区


注:git add . 是一个 Git 命令,用于将当前目录下的所有更改(包括新文件、修改和删除)添加到暂存区(staging area),以便在下一次提交中包含这些更改。

查看当前工作目录的状态

git status 命令用于查看当前工作目录和暂存区的状态,显示哪些文件有更改、哪些文件已添加到暂存区、哪些文件未被跟踪等信息。它可以帮助你了解当前项目的状态,并确定下一步操作。

[root@bingou myproject]# git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   houduan/index.jsp

4. 提交代码更改到本地仓库

使用 git commit 命令来提交暂存区中的更改。记得在提交信息中描述此次提交的目的。

[root@bingou myproject]# git commit -m "添加index.jsp 到 houduan目录,后端开发完成"

 注: 也可以不使用-m参数,Git 会打开一个默认的文本编辑器,供你输入提交信息。这个编辑器通常是你系统上配置的默认编辑器,可能是 vimnanovi 或者其他。具体看见下图:

如果使用了-m参数指定了写入信息就不用进入上图模式了。 

提交后再次查看暂存区

[root@bingou myproject]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working tree clean

此时就不会显示要提交的了。 

5. 推送到远端仓库

使用 git push 命令将本地仓库中的更改推送到远程仓库。

[root@bingou myproject]# git push origin master

 其语法:

git push origin <分支名>

查看远程仓库

[root@bingou myproject]# git remote
origin

[root@bingou myproject]# git remote -v
origin	lisi@192.168.226.20:/opt/git/myproject.git (fetch)
origin	lisi@192.168.226.20:/opt/git/myproject.git (push)

 这会列出当前所有已配置的远程仓库的名称。如果没有配置任何远程仓库,此命令将不会有任何输出。使用 -v 参数可以显示远程仓库的 URL。

验证

使用一台新虚拟从远端仓库克隆下来

[root@localhost ~]# hostnamectl set-hostname tty
[root@localhost ~]# bash
[root@tty ~]# yum install -y git
[root@tty ~]# git clone lisi@192.168.226.20:/opt/git/myproject.git

[root@tty ~]# ll
total 4
-rw-------. 1 root root 815 Jun  6 14:00 anaconda-ks.cfg
drwxr-xr-x  4 root root  33 Jul  9 15:01 myproject
[root@tty myproject]# ll
total 0
drwxr-xr-x 2 root root 23 Jul  9 15:01 houduan
[root@tty myproject]# ll houduan/
total 4
-rw-r--r-- 1 root root 19 Jul  9 15:01 index.jsp
[root@tty myproject]# cat houduan/index.jsp 
后端开发完成



#查看当前仓库中所有的分支。* 符号用来标识当前所在的分支。
[root@tty myproject]# git branch
* master

分支介绍 

在 Git 中,分支(Branch)是用来开发新功能、修复错误或进行实验性工作的重要概念。每个 Git 仓库都包含默认的主分支(通常是 mastermain),同时也可以创建和管理多个并行的分支。以下是关于 Git 分支的详细讲解:

1. 什么是分支?

分支是指向 Git 提交历史中某个特定提交(commit)的一个可变指针。使用分支可以在同一个仓库中同时开发多个独立的功能或修复,并且不会影响到主分支或其他分支的工作进度。

2. 分支的基本操作

  • 创建分支:创建一个新的分支来进行开发或实验。

    git branch <分支名称>
  • 切换分支:切换到已存在的分支以开始工作。

    git checkout <分支名称>

    或者使用新版本的 Git:

    git switch <分支名称>
  • 创建并切换到新分支:一步完成创建新分支并立即切换到该分支。

    git checkout -b <新分支名称>

    或者:

    git switch -c <新分支名称>
  • 查看分支:查看当前仓库中所有的分支。

    git branch
  • 合并分支:将一个分支的更改合并到当前分支(通常是主分支)。

    git merge <要合并的分支名称>
  • 删除分支:删除不再需要的分支。

    git branch -d <分支名称>

3. 分支管理策略

  • 主分支:通常是 mastermain 分支,用于发布稳定版本的代码。
  • 特性分支:用于开发新功能或进行实验性工作的分支。
  • 发布分支:从主分支创建,用于准备发布新版本的代码。
  • 修复分支:从主分支创建,用于修复生产环境中发现的 bug。

相关推荐

  1. Git详解

    2024-07-10 20:58:03       40 阅读
  2. <span style='color:red;'>Git</span><span style='color:red;'>详解</span>

    Git详解

    2024-07-10 20:58:03      10 阅读
  3. 工具--Git详解

    2024-07-10 20:58:03       42 阅读
  4. 工具Git详解

    2024-07-10 20:58:03       39 阅读
  5. Git提交规范详解

    2024-07-10 20:58:03       43 阅读
  6. git stash 命令详解

    2024-07-10 20:58:03       67 阅读
  7. 工具--Git详解

    2024-07-10 20:58:03       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 20:58:03       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 20:58:03       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 20:58:03       4 阅读
  4. Python语言-面向对象

    2024-07-10 20:58:03       7 阅读

热门阅读

  1. Android12上实现双以太网卡共存同时访问外网

    2024-07-10 20:58:03       11 阅读
  2. c语言实战-极简扫雷

    2024-07-10 20:58:03       10 阅读
  3. 从零到一:构建股票预测模型的Python实战教程

    2024-07-10 20:58:03       9 阅读
  4. SpringBoot | 面试题

    2024-07-10 20:58:03       8 阅读
  5. Shell学习——Shell printf命令

    2024-07-10 20:58:03       9 阅读
  6. Linux实现CPU物理隔离

    2024-07-10 20:58:03       11 阅读
  7. Redis 中的跳表(Skip List)

    2024-07-10 20:58:03       11 阅读