GitHub的使用

文章目录


注:此使用主要是针对Centos系统

一、什么是Git

Git 是一个分布式版本控制系统,可以帮助您和您的团队有效协作,同时保证项目历史记录的安全

1.1、与其他版本控制系统的区别

概念上的差异

Git与其他工具的最大不同之处在于它对数据的思考方式,Git不是存储文件的更改,而是将数据视为项目快照的系列,这意味着每次进行更改并保存(提交)时,Git都会在那一刻对所有文件进行快照,如果文件没有更改,Git只是保留到先前相同文件的链接

本地操作

使用Git时,大多数操作都不需要连接服务;因为你在电脑上拥有整个项目的历史记录,所以操作非常快速;你可以浏览项目历史记录或查看不同版本之间的更改,而无需等待服务器

数据的完整性

Git 确保没有任何内容丢失或损坏,每个文件和目录都经过校验和处理,Git 会知道是否有任何更改

Git 使用 SHA-1 哈希,这是每个文件版本的唯一代码,如果对内容进行了任何更改,即使是一个字符,都会导致不同的 SHA-1 哈希值

附加模型

在 Git 中,几乎所有东西都会向项目添加数据,因此很难意外丢失信息,一旦您提交更改,它们就会被安全地存储;使用 Git 进行实验的风险较小

1.2、三种状态和基本Git工作流程

Git有三种状态对于有效版本控制至关重要

  • 已修改:对工作树中尚未提交的文件所做的更改
  • 已暂存:标记为暂存区域中的下一次提交的修改将包含在下一次提交中
  • 已提交:更改永久存储在本地Git 目录中

Git的基本工作流程

  • 修改工作树中的文件
  • 暂存想要包含在下一个提交中的更改
  • 提交更改,将快照永久保存到Git目录中

二、首次Git设置

首次设置 Git 需要自定义 Git 环境以满足您的偏好。但首先需要从Git - Downloads下载 Git或使用 Chocolatey 包,然后只需按照安装说明进行操作即可

Git下载地址:https://git-scm.com/downloads
在这里插入图片描述

2.1、Git的安装(Linux)

# 下载并安装编译工具
[root@centos /]# yum -y groupinstall Development Tools
# 安装依赖包
[root@centos /]# yum -y install zlib-devel perl-ExtUtils-MakeMaker asciidoc xmlto openssl-devel
[root@centos /]# yum install libcurl-devel
[root@centos /]# yum install expat-devel

# 下载Git源码
[root@centos /]# wget https://www.kernel.org/pub/software/scm/git/git-2.31.1.tar.gz
[root@centos /]# tar -zxvf git-2.31.1.tar.gz 
# 配置并编译
[root@centos /]#cd /git-2.31.1
[root@centos git-2.31.1]# make prefix=/usr/local/git all
[root@centos git-2.31.1]# make prefix=/usr/local/git install
# 配置全局路径
[root@centos git-2.31.1]# export PATH=/usr/local/git/bin:$PATH
# 查看版本信息
[root@centos git-2.31.1]# git --version
git version 2.31.1

2.2、Git的安装(Windows)

在这里插入图片描述
这里直接选择合适的版本下载,一步一步安装即可

2.3、Git配置

使用git config工具定制Git 环境,这个工具允许检索和设置决定 Git 如何运行的配置变量,这些变量可以存储在三个不同的位置:

系统范围的配置:
存储在/etc/gitconfig文件中,这些设置适用于系统上的所有用户和所有存储库。可以使用选项与该文件–system交互git config

用户特定配置:
存储在/.gitconfig或中/.config/git/config,这些值特定您作为用户,可以使用选项与此文件进行交互–global,git config影响您在系统上使用的所有存储库

特定于存储库的配置:
这些设置存储在.git/config特定存储库内的文件中,覆盖全局配置并仅适用于该存储库

查看所有配置设置和路径

# windows
yuanl@DESKTOP-APGBGF5 MINGW64 ~/Desktop
$ git config --list --show-origin
file:D:/gitee/Git/etc/gitconfig diff.astextplain.textconv=astextplain
file:D:/gitee/Git/etc/gitconfig filter.lfs.clean=git-lfs clean -- %f
file:D:/gitee/Git/etc/gitconfig filter.lfs.smudge=git-lfs smudge -- %f
file:D:/gitee/Git/etc/gitconfig filter.lfs.process=git-lfs filter-process
file:D:/gitee/Git/etc/gitconfig filter.lfs.required=true
file:D:/gitee/Git/etc/gitconfig http.sslbackend=openssl
file:D:/gitee/Git/etc/gitconfig http.sslcainfo=D:/gitee/Git/mingw64/etc/ssl/certs/ca-bundle.crt
file:D:/gitee/Git/etc/gitconfig core.autocrlf=true
file:D:/gitee/Git/etc/gitconfig core.fscache=true
file:D:/gitee/Git/etc/gitconfig core.symlinks=false
file:D:/gitee/Git/etc/gitconfig pull.rebase=false
file:D:/gitee/Git/etc/gitconfig credential.helper=manager
file:D:/gitee/Git/etc/gitconfig credential.https://dev.azure.com.usehttppath=true
file:D:/gitee/Git/etc/gitconfig init.defaultbranch=master
file:C:/Users/yuanl/.gitconfig  credential.https://gitee.com.provider=generic
file:C:/Users/yuanl/.gitconfig  user.name=17710238158
file:C:/Users/yuanl/.gitconfig  user.email=12588116810@163.com
[root@centos git-2.31.1]# git config --list --show-origin
file:/root/.gitconfig   user.name=17710238158
file:/root/.gitconfig   user.email=12588116810@163.com

2.4、配置Git用户

[root@centos /]#git config --global user.name "17710238158"
[root@centos /]#git config --global user.email "12588116810@163.com"

2.5、配置默认文本编辑器

配置身份后,在 Git 中设置默认文本编辑器非常重要,当 Git 需要输入消息时,例如编写提交消息或解决合并冲突时,将使用此文本编辑器

默认情况下,Git 使用系统的默认文本编辑器,如果需要更换文本编辑器,例如 Emacs,按如下方式进行设置:

[root@centos git-2.31.1]# git config --global core.editor"emacs"

在 Windows 系统上,设置不同的文本编辑器需要指定其可执行文件的完整路径,如果想使用 Notepad++,您可以使用如下命令:

yuanl@DESKTOP-APGBGF5 MINGW64 ~/Desktop
$ git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

注:"-multiInst -notabbar -nosession -noPlugin"是用于自定义 Notepad++ 在 Git 启动时的行为的选项

Emacs介绍

Emacs全称为Editor MACroS(宏编辑器),最初由Richard Stallman于1975年在MIT与Guy Steele共同开发。它不仅是一个文本编辑器,还具有许多其他功能,使其更像是一个集成开发环境(IDE)

多功能性:除了基本的文本编辑功能,Emacs还提供了大量的编程工具,如代码编译、调试、版本控制等。它还支持多种编程语言和环境。

可扩展性:Emacs使用Emacs Lisp编写,这是一种强大的编程语言,允许用户自定义和扩展编辑器的功能。

分支版本:自Emacs诞生以来,已经发展出多个版本。其中最著名的两个分支是GNU Emacs和XEmacs,两者都保持着较高的兼容性。

社区文化:Emacs作为GNU计划的第一个项目,对于自由软件运动有着特殊的象征意义。它不仅仅是一个工具,也是开源文化的一部分。

内置功能:Emacs内置了网络浏览器、IRC客户端、计算器甚至游戏(如俄罗斯方块),使其成为一个多功能的工作环境。

2.6、更改Git中默认的分支名称

默认情况下,当使用 初始化新存储库时git init,Git 会创建一个名为 的分支master,但从 Git 版本 2.28 开始,可以选择为初始分支设置不同的名称

# windows
yuanl@DESKTOP-APGBGF5 MINGW64 ~/Desktop
$ git config --global init.defaultBranch main
# centos
[root@centos git-2.31.1]# git config --global init.defaultBranch main

注:这里是将原有的分支名称master改为main

2.7、查看配置

# windows
yuanl@DESKTOP-APGBGF5 MINGW64 ~/Desktop
$ 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=D:/gitee/Git/mingw64/etc/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
credential.https://gitee.com.provider=generic
user.name=17710238158
user.email=12588116810@163.com
core.editor='C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin
init.defaultbranch=main

yuanl@DESKTOP-APGBGF5 MINGW64 ~/Desktop
$ git config user.name
17710238158

# centos
[root@centos git-2.31.1]# git config --list
user.name=17710238158
user.email=12588116810@163.com
init.defaultbranch=main

[root@centos git-2.31.1]# git config user.name
17710238158

注:git config --list是列出Git目前可以找到的所有配置

三、在Git中获取帮助

可以通过三种等效方法获取任何 Git 命令的详细帮助:

  • Git 帮助命令:git help
  • 使用–help选项:git --help
  • 手册页(manpages):man git-

实例:

[root@centos git-2.31.1]# git help config
[root@centos git-2.31.1]# git config --help
[root@centos git-2.31.1]# man git-config

这些命令也可以离线工作,这很方便。

如果需要有关 Git 命令的可用选项的快速、简洁的信息,可以使用以下-h选项:

yuanl@DESKTOP-APGBGF5 MINGW64 ~/Desktop
$ git add -h
usage: git add [<options>] [--] <pathspec>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    --renormalize         renormalize EOL of tracked files (implies -u)
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run
    --sparse              allow updating entries outside of the sparse-checkout cone
    --chmod (+|-)x        override the executable bit of the listed files
    --pathspec-from-file <file>
                          read pathspec from file
    --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character

四、如何获取Git存储库

要开始使用 Git,通常需要获取一个 Git 存储库。基本上有两种主要的开始方式

4.1、在现有目录中初始化存储库

打开终端或命令提示符,使用cd命令将目录更改为的项目位置

进入项目目录后,通过运行以下命令来初始化 Git 存储库

[root@centos git]# git init
Initialized empty Git repository in /test/git/.git/

然后创建一个名为的新子目录,.git,Git 在其中存储 Git 存储库的所有必需文件;此时,项目文件尚未被跟踪

如果希望Git开始跟踪这些文件

[root@centos git]# git add xxs.py
[root@centos git]# git add xxs.md
[root@centos git]# git commit -m 'xxs.py,xxs.md'
[main (root-commit) 4e21299] xxs.py,xxs.md
 2 files changed, 20 insertions(+)
 create mode 100644 xxs.md
 create mode 100644 xxs.py

注:git add将文件添加到暂存区域,表明希望将它们包含在下一次提交中,然后提交更改;-m参数表示允许提交添加描述性消息

4.2、在Git中克隆现有存储库

获取 Git 存储库的第二种方法是克隆现有的存储库;当想要处理其他地方已经存在的项目时,非常有用

注:克隆存储库时,Git 会检索服务器拥有的几乎所有数据的完整副本。这包括项目历史记录的每个文件的每个版本,这意味着将在本地计算机上拥有存储库历史记录的完整副本

克隆存储库使用git clone命令,后跟要克隆的存储库的 URL,例如,要克隆xiaoyu_mall存储库,可以使用:

[root@centos git]# git clone https://gitee.com/yxz3128/xiaoyu_mall.git
Cloning into 'xiaoyu_mall'...
remote: Enumerating objects: 300, done.
remote: Counting objects: 100% (300/300), done.
remote: Compressing objects: 100% (272/272), done.
remote: Total 300 (delta 48), reused 240 (delta 23), pack-reused 0
Receiving objects: 100% (300/300), 9.96 MiB | 8.88 MiB/s, done.
Resolving deltas: 100% (48/48), done.
[root@centos git]# ls
xiaoyu_mall  xxs.md  xxs.py
[root@centos git]# ls xiaoyu_mall/
carts     goods      orders   static     users  verifications
contents  manage.py  payment  templates  utils  xiaoyu_mall

这将创建一个名为xiaoyu_mall的目录,初始化.git其中的目录,并提取该存储库的所有数据

如果要克隆到具有不同名称的目录,可以指定它。要将 gxiaoyu_mall存储库克隆到名为“xxs”而不是“xiaoyu_mall”的目录中,请执行以下操作:

[root@centos git]# git clone https://gitee.com/yxz3128/xiaoyu_mall.git xxs
Cloning into 'xxs'...
remote: Enumerating objects: 300, done.
remote: Counting objects: 100% (300/300), done.
remote: Compressing objects: 100% (272/272), done.
remote: Total 300 (delta 49), reused 240 (delta 23), pack-reused 0
Receiving objects: 100% (300/300), 9.96 MiB | 4.82 MiB/s, done.
Resolving deltas: 100% (49/49), done.
[root@centos git]# ls
xiaoyu_mall  xxs  xxs.md  xxs.py
[root@centos git]# ls xxs
carts     goods      orders   static     users  verifications
contents  manage.py  payment  templates  utils  xiaoyu_mall

Git 提供了各种可用于克隆存储库的传输协议。上面的示例使用了https协议,但是也可能会用到git://或user@server:path/to/repo.git,它使用 SSH 传输协议

五、如何记录存储库的更改

5.1、在Git中检查文件的状态

Git 将文件分为两种类型:跟踪的和未跟踪的。跟踪的文件是 Git 识别的文件,因为它们是最后一个快照(提交)的一部分或已暂存;未跟踪的文件是指 Git 当前未监控的其他文件,要检查存储库的状态

[root@centos git]# git status
# On branch main
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       xiaoyu_mall/
#       xxs/
nothing added to commit but untracked files present (use "git add" to track)

此命令提供有关当前分支、其同步状态以及文件状态的全面信息

## 放弃工作目录中的更改
[root@centos git]# git checkout xiaoyu_mall/

5.2、在Git中跟踪新文件

当您在项目中创建新文件时,Git 最初会认为它是未跟踪的,要开始跟踪新文件,您需要使用命令将其添加到暂存区域git add

## 暂存
[root@centos git]# git add xiaoyu_mall/

5.3、在 Git 中暂存修改后的文件

修改现有的跟踪文件,则需要使用git add,首先对文件进行修改,然后进行暂存,git status检查文件状态

[root@centos git]# git add xxs.md 
[root@centos git]# git status 
# On branch main
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   xxs.md
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       xxs/

5.4在Git中忽略文件

通常项目中存在不适合 Git 跟踪的文件或目录。这些可能包括日志文件、构建工件或敏感信息,例如本地环境设置(例如 *.env 或 config.json),可以使用文件指定要忽略的这些文件;如.gitignore

[root@centos git]# vi .gitignire 
[root@centos git]# echo '*.log' >> .gitignire 
[root@centos git]# echo '*.build' >> .gitignire 
[root@centos git]# vi .gitignire 
[root@centos git]# cat .gitignire 
*.log
*.build

注:上述中创建的.gitignire文件是一个空白文件,echo则是将要忽略的后缀格式输入到.gitignire文件中

注:在添加到文件之前已被 Git 跟踪的文件.gitignore将保持跟踪状态,要删除它们,需要使用 Git 命令手动取消跟踪它们

5.5、查看Git中的更改

想在提交之前查看对文件所做的确切更改,可以使用该git diff命令

## 查看为暂存的更改
[root@centos git]# git diff
## 分阶段查看变化
[root@centos git]# git diff --cached xxs.md 
diff --git a/xxs.md b/xxs.md
index d79b629..dd80453 100644
--- a/xxs.md
+++ b/xxs.md
@@ -7,4 +7,6 @@ file_name = hashlib.md5(url.encode('utf-8')).hexdigest()
 right_dot_index = url.rfind('.')
 resp = requests.get(url=url)
 with open(file_name + url[right_dot_index:],'wb') as file:
+
+
     file.write(resp.content)

git diff提供实际修改的详细视图。用于git diff 关注特定文件内的更改

5.6、提交变更

当准备好提交更改时,请使用该git commit命令。这将打开文本编辑器,提供提交消息,或者,可以使用该-m标志直接添加提交消息

一旦暂存了想要包含在提交中的更改,可以使用以下命令提交它们

[root@centos git]# git commit -m ".gitignire"
[main af9c212] .gitignire
 411 files changed, 16020 insertions(+)
 create mode 100644 xiaoyu_mall/.gitignore
 create mode 100644 xiaoyu_mall/.idea/.gitignore
 create mode 100644 xiaoyu_mall/.idea/dataSources.xml
 create mode 100644 xiaoyu_mall/.idea/inspectionProfiles/profiles_settings.xml
 create mode 100644 xiaoyu_mall/.idea/misc.xml
 create mode 100644 xiaoyu_mall/.idea/modules.xml
 create mode 100644 xiaoyu_mall/.idea/vcs.xml
 create mode 100644 xiaoyu_mall/.idea/xiaoyu_mall.iml

注:-m后面的是备注,可以随意添加字符或信息

5.7、删除Git中的文件

如果需要从 Git 的跟踪中删除文件,您可以使用git rm.它将从存储库和工作目录中删除文件

[root@centos git]# git rm xxs.md
rm 'xxs.md'
[root@centos git]# git rm -r xiaoyu_mall/
rm 'xiaoyu_mall/.gitignore'
rm 'xiaoyu_mall/.idea/.gitignore'
rm 'xiaoyu_mall/.idea/dataSources.xml'
rm 'xiaoyu_mall/.idea/inspectionProfiles/profiles_settings.xml'
rm 'xiaoyu_mall/.idea/misc.xml'
rm 'xiaoyu_mall/.idea/modules.xml'
rm 'xiaoyu_mall/.idea/vcs.xml'
rm 'xiaoyu_mall/.idea/xiaoyu_mall.iml'
rm 'xiaoyu_mall/carts/__init__.py'
rm 'xiaoyu_mall/carts/admin.py'
rm 'xiaoyu_mall/carts/apps.py'
rm 'xiaoyu_mall/carts/migrations/__init__.py'
rm 'xiaoyu_mall/carts/models.py'
rm 'xiaoyu_mall/carts/tests.py'

注:这里的删除用法和linux的一样,-r是递归删除

如果只想将其从存储库中删除但将其保留在工作目录中,使用–cached选项

[root@centos git]# git rm --cached xxs.py
rm 'xxs.py'

5.8、在Git中移动或重命名文件

Git 不会明确跟踪文件移动,但可以使用git mv来重命名或移动存储库中的文件

[root@centos git]# git add xxs.py
[root@centos git]# git mv xxs.py new_xxs.py
[root@centos git]# git status
# On branch main
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    xxs.py -> new_xxs.py

六、查看Git中的提交历史记录

创建多个提交或克隆存储库后,git log命令可以检查提交历史记录

默认情况下,它按时间倒序列出提交,显示每个提交及其 SHA-1 校验和、作者姓名和电子邮件、日期和提交消息

[root@centos git]# git log
commit af9c212a2ae1e170474dd960e0aabd52bb3a7a6b
Author: 18821349269 <12588116810@163.com>
Date:   Fri Apr 19 14:16:11 2024 +0800

    .gitignire

commit 4e212999ea0344a9ec3ff121d035364fe5f7021c
Author: 17710238158 <13699227901@163.com>
Date:   Thu Apr 18 16:54:01 2024 +0800

    xxs.py,xxs.md

6.1、查看Git中的提交差异

要查看每次提交中引入的差异,可以使用-p或–patch选项

[root@centos git]# git log -p -2

注:-2用于查看最近两次提交中引入的差异

6.2、在Git中显示统计信息

–stat选项提供每次提交的汇总统计信息,包括修改的文件、添加/删除的行以及摘要

[root@centos git]# git log --stat

6.3、自定义Git日志输出格式

–pretty选项允许您更改日志输出格式。不同的格式有不同的选项:

  • oneline:每次提交的简洁单行摘要。
  • short:默认格式,包含作者、日期和消息。
  • full:包含提交哈希、作者、日期、消息和差异的详细格式。
  • fuller:更详细的格式,包括完整的文件路径。
  • format:使用格式说明符自定义输出。
[root@centos git]# git log --pretty=oneline
af9c212a2ae1e170474dd960e0aabd52bb3a7a6b .gitignire
4e212999ea0344a9ec3ff121d035364fe5f7021c xxs.py,xxs.md
[root@centos git]# git log --pretty=format:"%ad %s %h %an %ae"
Fri Apr 19 14:16:11 2024 +0800 .gitignire af9c212 17710238158 12588116810@163.com
Thu Apr 18 16:54:01 2024 +0800 xxs.py,xxs.md 4e21299 17710238158 12588116810@163.com

使用–graph还可以可视化分支和合并历史记录

[root@centos git]# git log --pretty=format:"%ad %s %h %an %ae" --graph
 * Fri Apr 19 14:16:11 2024 +0800 .gitignire af9c212 17710238158 12588116810@163.com
 * Thu Apr 18 16:54:01 2024 +0800 xxs.py,xxs.md 4e21299 17710238158 12588116810@163.com

注:因为系统原因无法可视化,智能和上一条命令一样显示

6.4、限制Git日志输出

除了格式化选项之外,git log还提供各种限制选项来细化显示的提交历史记录

  • -:仅显示最后 n 次提交。
  • –since, --until:将提交限制为指定日期之后/之前所做的提交。
  • –author:仅显示特定作者的提交。
  • –grep:按提交消息中的关键字过滤提交。
  • -S:显示提交更改

示例:查看作者 17710238158 自特定日期以来的最后 3 次提交,以及补丁详细信息

[root@centos git]# git log --author="17710238158" --since="2024-04-01" -p -5

七、撤销Git中的操作

7.1、在Git中撤销提交

如果提交得太早或需要对上次提交进行其他更改,请使用以下命令

[root@centos git]# git commit --amend
[main a1cbad0] .gitignire
 2 files changed, 10 deletions(-)
 rename xxs.md => new_xxs.py (100%)
 delete mode 100644 xxs.py

这将打开提交消息编辑器,允许修改消息。如果自上次提交以来未进行任何更改,则仅允许编辑提交消息。

注:仅修改仍在本地且尚未推送的提交,以避免协作者出现问题

7.2、取消暂存文件

要取消暂存意外包含的文件,可以使用该git reset HEAD 命令

[root@centos git]# git reset HEAD xxs.py

7.3、取消修改已修改的文件

文件进行了一些修改,后来意识到不想保留这些修改,使用git checkout – 放弃对文件所做的更改并将其恢复到之前的状态

[root@centos git]# git checkout xxs.py
error: pathspec 'xxs.py' did not match any file(s) known to git.

注:因为我这里取消了暂存,所以才会报错暂存中不存在xxs.py文件

八、Git中的远程存储库

远程存储库是托管在互联网或网络上的项目版本。与他人协作涉及管理这些远程存储库,包括添加、删除和检查它们

8.1、在Git中显示远程存储库

[root@centos git]# git remote 

此命令列出了指定的所有远程句柄的短名称,例如,如果克隆了一个存储库,通常会看到origin Git 分配给的克隆的服务器的默认名称

添加该-v选项可提供其他详细信息

[root@centos git]# git remote -v

这会显示每个远程的获取和推送 URL,能够了解项目的托管位置以及如何与其交互

8.2、在Git中添加远程存储库

要显式添加新的远程存储库,请使用git remote add

[root@centos git]# git remote add example https://github.com/example/example.git

在这里,添加了一个example以指定 URL 命名的远程。这允许example在命令中使用短名称来引用此远程存储库

8.3、如何在Git中从远程中获取和拉取

要从远程存储库获取数据,我们使用git fetch命令后跟远程名称

[root@centos git]# git fetch origin // Here we are not specifying any particular branch.

它将任何新的更改从origin远程存储库下载到的本地存储库

或者,如果想一步从远程分支获取更改并将其合并到当前分支中,可以使用以下git pull命令

[root@centos git]# git pull origin master

专门将更改从远程存储库master的分支拉origin入当前分支

8.4、在Git中将更改推送到存储库

为了与其他人分享工作,使用以下方法将更改推送到远程存储库

[root@centos git]# git push origin master

在此示例中,将本地更改推送到远程存储库main的分支origin

8.5、在Git中检查存储库

[root@centos git]# git remote show origin

此命令显示详细信息,例如获取和推送 URL、跟踪的分支以及与origin远程存储库关联的本地分支配置

8.6、在Git中重命名存储库

[root@centos git]# git remote rename example new-example

8.7、在Git中删除存储库

[root@centos git]# git remote remove new-example
[root@centos git]# git remote rm new-example

删除后,远程跟踪分支和关联的配置设置也会被删除

九、在Git中标记

Git 中的标记是一项基本功能,允许开发人员将存储库历史记录中的特定点标记为重要。通常,标签用于表示发布点,例如 v1.0、v2.0 等

9.1、列出Git中的现有标签

[root@centos git]# git tag

此外,您还可以使用或选项搜索与特定模式匹配的标签–list(简写-l)

[root@centos git]# git tag -l "v2.0*"

9.2、如何在 Git 中创建标签

Git 支持两种类型的标签:轻量级标签和带注释的标签

轻量级标签

想要标记特定提交而不添加任何其他信息时,请使用轻量级标签

[root@centos git]# git tag v1.1-lw

要查看与此标签关联的提交信息,请使用

[root@centos git]# git show v1.1-lw

带注释的标签

另一方面,带注释的标签包含附加信息,例如标签信息、日期和标签消息。

创建带注释的标记涉及使用命令-a的选项git tag以及标记消息

[root@centos git]# git tag -a v2.0 -m "Release version 2.0"

要查看有关此标记的详细信息,包括它指向的提交和标记消息

[root@centos git]# git show v2.0

在 Git 中将标签推送到远程存储库

要将特定标签推送到远程服务器,可以使用

[root@centos git]# git push origin <tagname>

如果有多个标签并希望一次推送所有标签,可以使用以下–tags选项

[root@centos git]# git push origin --tags

9.3、删除Git中的标签

要在本地删除标签(从本地存储库中删除)如:

[root@centos git]# git tag -d v1.4-lw

可以通过两种方式从远程服务器删除标签

使用git push带有 refspec 的命令

[root@centos git]# git push origin :refs/tags/v1.1-lw

注:该命令不会将任何内容 ( 😃 推送到远程标记v1.1-lw,从而有效地将其删除

使用–delete选项git push

[root@centos git]# git push origin --delete v1.1-lw

9.4、如何查看 Git 中的标签

[root@centos git]# git checkout v2.0

十、Git别名

Git 别名是可以创建的快捷方式或自定义命令,以简化和简化Git 工作流程。

要创建 Git 别名,请使用git config带有–global标志的命令,以使别名在所有 Git 存储库中可用

10.1、常用命令的基本别名

可以为常用的 Git 命令创建别名,以便更容易记住和输入它们。例如:

[root@centos git]# git config --global alias.co checkout
[root@centos git]# git config --global alias.br branch
[root@centos git]# git config --global alias.ci commit

还可以为经常执行的操作或提高命令可读性创建自定义别名,如:

[root@centos git]# git config --global alias.unstage 'reset HEAD --'

现在,可以使用git unstage 而不是git reset HEAD – 取消暂存文件

十一、Git分支

Git 中的分支提供了一种管理项目代码库的强大方法,允许并行开发和实验而不影响主代码库。

Git 分支允许脱离开发主线,处理功能或修复,然后将更改合并回去。与许多其他版本控制系统不同,Git 的分支模型轻量级且高效,使得分支操作几乎是即时的

11.1、Git中的分支是什么

分支是一个轻量级的、可移动的提交指针。默认分支名称通常是“master”,但它并不特殊——它就像任何其他分支一样。

创建分支和在分支之间切换允许您同时处理不同的功能

11.2、在Git中创建新分支

当想要开始开发新功能或尝试一个想法时,可以在 Git 中创建一个新分支。这个新分支充当单独的开发线,允许在不影响主分支的情况下进行更改

[root@centos git]# git branch new_feature

此命令创建一个名为“new-feature”的新分支,指向与当前分支相同的提交。分支可以共存,Git 保留一个特殊的指针来HEAD指示当前分支

11.3、了解分支

当初始化 Git 存储库时,会从一个默认分支开始,通常名为“master”或“main”。分支本质上是指向提交的指针,使其能够独立处理不同的功能或修复

查看存储库中的所有分支

[root@centos git]# git branch

显示带有星号 (*) 的分支列表,指示当前签出的分支

[root@centos git]# git branch -v

11.4、切换到Git中的另一个分支

切换到现有的不同分支,请使用git checkout

[root@centos git]# git checkout new_feature

此命令将“HEAD”指针切换到“new-feature”分支,使其成为当前活动的分支

通过一项操作创建并切换到新分支

[root@centos git]# git checkout -b new_feature

在 Git 版本 2.23 及以上版本中,可以使用另外两条命令

  • 切换到现有分支:git switch existing-branch
  • 创建并切换到新分支:git switch -c new-branch

十二、如何在Git中管理分支

12.1管理合并分支

随着项目的发展,一旦分支的更改完成,将把分支合并回主分支。要识别合并的分支,请使用

[root@centos git]# git branch --merged

此命令列出已成功合并到当前分支的分支。通常可以安全地使用以下方法删除这些分支

[root@centos git]# git branch -d branch_name

对于包含未合并工作的分支,请使用

[root@centos git]# git branch --no-merged

12.2、重命名分支

重命名本地分支

[root@centos git]# git branch --move old_branch_name new_branch_name

要反映远程存储库上的更改,请推送重命名的分支
使用:

[root@centos git]# git push --set-upstream origin new_branch_name

验证:

[root@centos git]# git branch --all

确保删除存储库上的旧分支

[root@centos git]# git push origin --delete old_branch_name

12.3、更改默认分支名称

重命名默认分支(通常为“master”)需要谨慎和协调,因为它会影响项目集成和协作者

[root@centos git]# git branch --move master main

重命名后,将更新的分支推送到远程存储库

[root@centos git]# git push --set-upstream origin main

确保记住跨依赖项、测试、脚本和存储库主机更新引用和配置。完成后,删除远程上的旧主分支

[root@centos git]# git push origin --delete master

相关推荐

  1. github Copilot基本使用

    2024-04-24 20:24:03       31 阅读
  2. github注册和使用

    2024-04-24 20:24:03       11 阅读
  3. Git 和 Github 使用

    2024-04-24 20:24:03       6 阅读
  4. Github Copilot 使用方法和快捷键

    2024-04-24 20:24:03       39 阅读
  5. Github Copilot 使用方法和快捷键

    2024-04-24 20:24:03       53 阅读
  6. Github Copilot 使用方法和快捷键

    2024-04-24 20:24:03       47 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 20:24:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 20:24:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 20:24:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 20:24:03       20 阅读

热门阅读

  1. 异步并发怎么做?

    2024-04-24 20:24:03       12 阅读
  2. 第三方包的info文件夹作用

    2024-04-24 20:24:03       8 阅读
  3. 服务运维篇-通过防火墙抵御渗透扫描

    2024-04-24 20:24:03       11 阅读
  4. K8s: 持久化存储之卷, NFS卷

    2024-04-24 20:24:03       11 阅读
  5. linux安装SSH

    2024-04-24 20:24:03       13 阅读
  6. 构建数据安全体系:详解数据治理的建设思路

    2024-04-24 20:24:03       11 阅读
  7. [Unity]打包Android后xxx方法丢失。

    2024-04-24 20:24:03       12 阅读
  8. IntelliJ IDEA个人可一直使用方法参考

    2024-04-24 20:24:03       11 阅读
  9. 任务修复实例(4)

    2024-04-24 20:24:03       11 阅读
  10. ubuntu 23.04 Dell T3660 听歌没声音的尝试

    2024-04-24 20:24:03       13 阅读
  11. Qt实现XYModem协议(八)

    2024-04-24 20:24:03       14 阅读