git在Linux中的使用

git-Linux中的使用

  • 一、下载git
  • 二、https方式上传
  • 三、ssh秘钥方式上传


一、下载git

版本信息

[root@rocky ~]# cat /etc/rocky-release
Rocky Linux release 9.4 (Blue Onyx)
[root@rocky ~]# cat /etc/rocky-release-upstream
Derived from Red Hat Enterprise Linux 9.4
[root@rocky ~]# uname -r
5.14.0-427.13.1.el9_4.x86_64

安装git并查看版本

[root@rocky ~]# yum install git -y
[root@rocky shell]# git --version
git version 2.47.3


二、https方式上传

  1. 在gitee里新建仓库

  2. Linux中进入要项目目录

[root@rocky ~]# cd /shell

  1. 初始化

[root@rocky shell]# git init
提示: 使用 ‘master’ 作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中
提示: 配置使用初始分支名,并消除这条警告,请执行:
提示:
提示: git config --global init.defaultBranch <名称>
提示:
提示: 除了 ‘master’ 之外,通常选定的名字有 ‘main’、‘trunk’ 和 ‘development’。
提示: 可以通过以下命令重命名刚创建的分支:
提示:
提示: git branch -m
已初始化空的 Git 仓库于 /shell/.git/

  1. 在Linux客户机上配置好用户名和邮箱

[root@rocky shell]# git config --global user.name ‘Code Rhythm’
[root@rocky shell]# git config --global user.email ‘example@qq.com’

  1. 克隆gitee中新建的仓库到Linux中
    git clone “仓库地址”

[root@rocky shell]# git clone https://gitee.com/code-rhythm/shell.git
正克隆到 ‘shell’…
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
接收对象中: 100% (3/3), 完成.

  1. 将文件提交到暂存区

[root@rocky shell]# git add read.sh

  1. 提交暂存区文件到本地仓库

[root@rocky shell]# git commit -m “read传参”
[master(根提交) 41e27e8] read传参
1 file changed, 15 insertions(+)
create mode 100644 read.sh

  1. 关联远程仓库,可自己命名
    git remote add 别名 仓库地址

[root@rocky shell]# git remote add shell https://gitee.com/code-rhythm/shell.git

  1. 推送分支
    首次推送可push -f shell master强制推送或git push -u origin master 推送
[root@rocky shell]# git push shell
致命错误:当前分支 master 没有对应的上游分支。
为推送当前分支并建立与远程上游的跟踪,使用git push --set-upstream shell master为了让没有追踪上游的分支自动配置,参见 'git help config' 中的 push.autoSetupRemote。[root@rocky shell]#  git push --set-upstream shell master
Username for 'https://gitee.com': code-rhythm
Password for 'https://code-rhythm@gitee.com': 
To https://gitee.com/code-rhythm/shell.git! [rejected]        master -> master (fetch first)
错误:无法推送一些引用到 'https://gitee.com/code-rhythm/shell.git'
提示: 更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外
提示: 一个仓库已向该引用进行了推送。如果您希望先与远程变更合并,请在推送
提示: 前执行 'git pull'。
提示: 详见 'git push --help' 中的 'Note about fast-forwards' 小节。[root@rocky shell]# git pull shell master
来自 https://gitee.com/code-rhythm/shell* branch            master     -> FETCH_HEAD
提示: 您有偏离的分支,需要指定如何调和它们。您可以在执行下一次
提示: pull 操作之前执行下面一条命令来抑制本消息:
提示:
提示:   git config pull.rebase false  # 合并
提示:   git config pull.rebase true   # 变基
提示:   git config pull.ff only       # 仅快进
提示:
提示: 您可以将 "git config" 替换为 "git config --global" 以便为所有仓库设置
提示: 缺省的配置项。您也可以在每次执行 pull 命令时添加 --rebase、--no-rebase,
提示: 或者 --ff-only 参数覆盖缺省设置。
致命错误:需要指定如何调和偏离的分支。
[root@rocky shell]# git config pull.rebase false --global

强制推送

[root@rocky shell]# git push -f shell master
Username for 'https://gitee.com': code-rhythm
Password for 'https://code-rhythm@gitee.com': 
枚举对象中: 3, 完成.
对象计数中: 100% (3/3), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 425 字节 | 425.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0(来自  0 个包)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 6d4ad285
To https://gitee.com/code-rhythm/shell.git+ 097e4e5...41e27e8 master -> master (forced update)
  1. 再次上传
[root@rocky shell]# git add install_mysql.sh 
[root@rocky shell]# git commit -m "安装mysql"
[master ed4047c] 安装mysql1 file changed, 16 insertions(+)create mode 100644 install_mysql.sh[root@rocky shell]# git push shell master
Username for 'https://gitee.com': code-rhythm
Password for 'https://code-rhythm@gitee.com': 
枚举对象中: 4, 完成.
对象计数中: 100% (4/4), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 518 字节 | 518.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0(来自  0 个包)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 5b42008e
To https://gitee.com/code-rhythm/shell.git41e27e8..ed4047c  master -> master

痛点:每次上传都要输入用户名和密码,效率低
-> 配置秘钥


三、ssh秘钥方式上传

  • ssh-keygen:SSH 密钥生成工具的核心命令
  • -t ed25519:指定密钥算法为 ed25519(目前最安全、高效的算法之一,优于传统的 RSA,推荐优先使用)
  • -C “Gitee SSH Key”:为密钥添加注释(comment),方便区分不同密钥的用途

[root@rocky shell]# ssh-keygen -t ed25519 -C “Gitee SSH Key”
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519
Your public key has been saved in /root/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:2KmPYsCZPRSVuQRWj8ViH1iT3BmG+Ah8ERsQSmYC8gw Gitee SSH Key
The key’s randomart image is:
±-[ED25519 256]–+
|E +.=@++oo |
|.O o+ X
oo |
| + o… |
| . ooo. |
| . = . S |
| = o . |
| . … |
| o o |
| . … . |
±—[SHA256]-----+

查看秘钥

[root@rocky .ssh]# cat /root/.ssh/id_ed25519.pub

!!注意

!!!这里添加的密钥会被识别为 DeployKey,只读
复制生成后的 ssh key ,通过仓库主页 [管理] -> [部署公钥管理] -> [添加部署公钥],将生成的公钥添加到仓库中

DeployKey错误示例

[root@rocky .ssh]# ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.76.199.13)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
Hi Anonymous (DeployKey)! You've successfully authenticated, but GITEE.COM does not provide shell access.
Note: Perhaps the current use is DeployKey.
Note: DeployKey only supports pull/fetch operations[root@rocky .ssh]# ssh 'code-rhythm'@gitee.com
Hi Anonymous (DeployKey)! You've successfully authenticated, but GITEE.COM does not provide shell access.
Note: Perhaps the current use is DeployKey.
Note: DeployKey only supports pull/fetch operations
Connection to gitee.com closed.[root@rocky shell]# git add full_backup.sh 
[root@rocky shell]# git commit -m "全备"
[master bd2b821] 全备1 file changed, 52 insertions(+)create mode 100644 full_backup.sh
[root@rocky shell]# git push shell master
[session-754d88b7] Auth error: DeployKey does not support push code
致命错误:无法读取远程仓库。请确认您有正确的访问权限并且仓库存在。

右上角头像 →「设置」→「SSH 公钥」(这里添加的是用户密钥,有推送权限)
在这里插入图片描述

验证密钥是否生效

[root@rocky shell]# ssh -T git@gitee.com
Hi Code Rhythm(@code-rhythm)! You’ve successfully authenticated, but GITEE.COM does not provide shell access.
[root@rocky shell]# git push shell master
枚举对象中: 4, 完成.
对象计数中: 100% (4/4), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 925 字节 | 925.00 KiB/s, 完成.
总共 3(差异 1),复用 0(差异 0),包复用 0(来自 0 个包)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 0245859e
To gitee.com:code-rhythm/shell.git
b708fc8…bd2b821 master -> master

通过 HTTPS 克隆过仓库,需修改现有仓库的远程协议
或者在新目录中重新克隆ssh仓库

  • 查看远程地址

[root@rocky shell]# git remote -v
shell https://gitee.com/code-rhythm/shell.git (fetch)
shell https://gitee.com/code-rhythm/shell.git (push)

  • 修改远程地址为 SSH 协议
    git remote set-url 别名 SSH 协议的仓库

[root@rocky shell]# git remote set-url shell git@gitee.com:code-rhythm/shell.git
[root@rocky shell]# git remote -v
shell git@gitee.com:code-rhythm/shell.git (fetch)
shell git@gitee.com:code-rhythm/shell.git (push)

其他常用命令

git log 查看提交历史(详细)

[root@rocky shell]# git log
commit bd2b82109188dd24bf066f348cfd202897b26fd1 (HEAD -> master, shell/master)
Author: Code Rhythm <...@qq.com>
Date:   Thu Sep 4 21:08:46 2025 +0800全备commit b708fc88c98db79d4c55fbd8812f979260c44470
...

git log --oneline 查看提交历史(简化版,一行显示)

[root@rocky shell]# git log --oneline
bd2b821 (HEAD -> master, shell/master) 全备
b708fc8 安装xtrabackup
ed4047c 安装mysql
41e27e8 read传参

git log -n 1 查看最近 n 次提交

[root@rocky shell]# git log -n 1
commit bd2b82109188dd24bf066f348cfd202897b26fd1 (HEAD -> master, shell/master)
Author: Code Rhythm <...@qq.com>
Date:   Thu Sep 4 21:08:46 2025 +0800全备

git status 查看文件状态

[root@rocky shell]# git add for.sh
[root@rocky shell]# git status
位于分支 master
要提交的变更:(使用 "git restore --staged <文件>..." 以取消暂存)新文件:   for.sh未跟踪的文件:(使用 "git add <文件>..." 以包含要提交的内容).txtPS3.shaccess_web.pyback_log.sh

git restore --staged 取消暂存

[root@rocky shell]# git restore --staged for.sh
[root@rocky shell]# git status
位于分支 master
未跟踪的文件:(使用 "git add <文件>..." 以包含要提交的内容).txtPS3.shaccess_web.pyback_log.sh
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

git restore --staged + git restore 取消暂存和丢弃工作区的改动

[root@rocky shell]# vim install_mysql.sh 
[root@rocky shell]# git add install_mysql.sh 
[root@rocky shell]# git status
位于分支 master
要提交的变更:(使用 "git restore --staged <文件>..." 以取消暂存)修改:     install_mysql.sh未跟踪的文件:(使用 "git add <文件>..." 以包含要提交的内容).gitignorePS3.shaccess_web.py[root@rocky shell]# git restore --staged install_mysql.sh 
[root@rocky shell]# git status
位于分支 master
尚未暂存以备提交的变更:(使用 "git add <文件>..." 更新要提交的内容)(使用 "git restore <文件>..." 丢弃工作区的改动)修改:     install_mysql.sh未跟踪的文件:(使用 "git add <文件>..." 以包含要提交的内容).gitignorePS3.sh
修改尚未加入提交(使用 "git add" 和/或 "git commit -a"[root@rocky shell]# git restore install_mysql.sh 

创建新分支(不切换)

[root@rocky shell]# git branch testing
[root@rocky shell]# git branch
* master
testing

切换分支

[root@rocky shell]# git checkout testing
切换到分支 ‘testing’
[root@rocky shell]# git branch
master
/* testing

分支测试:git分支是相互独立的代码线

[root@rocky shell]# git add test.txt
[root@rocky shell]# git commit -m "test"
[testing 109925c] test1 file changed, 3 insertions(+)create mode 100644 test.txt
[root@rocky shell]# cat test.txt 
1
2
3
[root@rocky shell]# git push shell testing
枚举对象中: 4, 完成.
对象计数中: 100% (4/4), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 270 字节 | 54.00 KiB/s, 完成.
总共 3(差异 1),复用 0(差异 0),包复用 0(来自  0 个包)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 3e483fb5
remote: Create a pull request for 'testing' on Gitee by visiting:
remote: https://gitee.com/code-rhythm/shell/pull/new/code-rhythm:testing...code-rhythm:master
To gitee.com:code-rhythm/shell.git* [new branch]      testing -> testing[root@rocky shell]# git checkout master
切换到分支 'master'
[root@rocky shell]# cat test.txt
cat: test.txt: 没有那个文件或目录

创建并切换分支

[root@rocky shell]# git checkout -b test2
切换到一个新分支 ‘test2’
[root@rocky shell]# git branch
master
* test2
testing

删除分支

[root@rocky shell]# git checkout master
切换到分支 ‘master’
[root@rocky shell]# git branch -d test2
已删除分支 test2(曾为 bd2b821)。
[root@rocky shell]# git branch
* master
testing

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/95969.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/95969.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HMI(人机界面)

新晋码农一枚&#xff0c;小编定期整理一些写的比较好的代码&#xff0c;作为自己的学习笔记&#xff0c;会试着做一下批注和补充&#xff0c;转载或者参考他人文献会标明出处&#xff0c;非商用&#xff0c;如有侵权会删改&#xff01;欢迎大家斧正和讨论&#xff01;一、核心…

嵌入式解谜日志—多路I/O复用

多路 I/O复用&#xff08;Multiplexed I/O&#xff09;&#xff1a;1.定义&#xff1a;系统提供的I/O事件通知机制2.应用&#xff1a;是一种 I/O 编程模型&#xff0c;用于在单线程中同时处理多个&#xff08;阻塞&#xff09; I/O 操作&#xff0c;避免因等待某个 I/O 操作完成…

关于嵌入式学习——单片机4

ds18b20温度传感器的使用一、传感器分类&#xff1a;数字温度传感器&#xff0c;实现简单&#xff0c;不需要额外转换电路&#xff0c;采集过来的就是数字温度值模拟温度传感器->热敏电阻->AD转换电路->数字值二、传感器接口&#xff1a;GPIO接口&#xff1a;&#xf…

Kali搭建sqli-labs靶场

1.输入apt-get install docker.io即可下载靶场镜像。 下载好后&#xff0c;我们输入docker search sqli-labs搜索sqli-labs靶场。2.我们选择第一个&#xff0c;输入docker pull acgpiano/sqli-labs&#xff0c;将该靶场装到本地。此时输入docker images&#xff0c;发现本地有s…

电脑外接显示屏字体和图标过大

当外接显示屏的分辨率过高时&#xff0c;可以调整显示器设置来解决字体和图标过大的问题。具体操作包括在桌面右击选择显示设置&#xff0c;切换到外接显示器&#xff0c;将分辨率调至推荐的1920x1080&#xff0c;或根据个人偏好进行适当调节&#xff0c;然后保存更改。 原因&a…

Linux 网络流量监控 Shell 脚本详解(支持邮件告警)

前言 一、脚本功能 二、实现原理 三、Shell 脚本实现 四、关键知识点解析 1. Bash 关联数组 2. 命令组 { } 与子 Shell ( ) 3. 字符串拼接换行 4. 流量计算逻辑 五、测试方法 六、优化建议 七、总结 前言 在生产环境中&#xff0c;监控服务器的 网络流量 非常重要…

【牛客刷题-剑指Offer】BM18 二维数组中的查找:一题四解,从暴力到最优

文章目录 一、题目介绍 1.1 描述 1.2 示例1 1.3 示例2 1.4 给的部分代码 二、题解 方法一:暴力遍历 方法二:二分查找(逐行) 方法三:Z字形查找(最优解) 方法四:递归分治(拓展思路) 三、总结 心得体会 一、题目介绍 原题链接:https://www.nowcoder.com/practice/abc3…

使用pyspark对上百亿行的hive表生成稀疏向量

背景&#xff1a;一张上百亿行的hive表&#xff0c;只有id和app两列&#xff0c;其中app的去重量是8w多个&#xff08;原app有上百万枚举值&#xff0c;此处已经用id数量进行过筛选&#xff0c;只留下有一定规模的app&#xff09;&#xff0c;id的去重量大概有八九亿&#xff0…

【设计模式】关于学习《重学Java设计模式》的一些成长笔记

【设计模式】关于学习《重学Java设计模式》的一些成长笔记 没有几个人是一说就会的,掌握一些技能,不仅要用心,而且还需要从温故中知新。 为此,好记性不如烂笔头,我干脆一步一脚印地系统学习一遍设计模式! (关注不迷路哈!!!) 文章目录 【设计模式】关于学习《重学Jav…

【基础-判断】@Entry装饰的自定义组件将作为页面的入口。在单个页面中可以使用多个@Entry装饰不同自定义组件。

@Entry装饰的自定义组件将作为页面的入口。在单个页面中可以使用多个@Entry装饰不同自定义组件。 解释: @Entry 的核心作用与唯一性:@Entry 装饰器用于明确声明该组件是一个页面的入口组件,即整个页面的“根”和“起点”。当UIAbility实例加载并显示页面时,系统需要明确知道…

医学影像AI应用-实践:使用MONAI实现肺部CT图像分割的原理与实践

🧑 博主简介:CSDN博客专家、CSDN平台优质创作者,高级开发工程师,数学专业,10年以上C/C++, C#,Java等多种编程语言开发经验,拥有高级工程师证书;擅长C/C++、C#等开发语言,熟悉Java常用开发技术,能熟练应用常用数据库SQL server,Oracle,mysql,postgresql等进行开发应用…

如何训练一个简单的Transformer模型(附源码)李宏毅2025大模型-作业4

摘要&#xff1a;一、作业目标&#xff1a;使用只有2层transformer的GPT-2&#xff0c;生成完整宝可梦图像。二、源码&解析&#xff1a;使用提供的Transformer模型&#xff08;GPT-2&#xff09;进行训练&#xff0c;FID Score: 96.3425一、作业目标1&#xff09;目标使用T…

leetcode211.添加与搜索单词-数据结构设计

与208.前缀树的设计是一样的&#xff0c;关键点在于word中存在通配符“."&#xff0c;所以针对该特殊情况&#xff0c;在search时针对这里进行全子节点的深度搜索class WordDictionary {TrieNode root;private class TrieNode {char val;// 当前节点的值&#xff0c;冗余了…

项目中的一些比较实用的自定义控件

本文是记录项目开发中一些相对复杂但都比较实用的控件&#xff0c;这些控件都是基于自定义的方式去实现&#xff0c;如果有需要的朋友&#xff0c;这个可以作为一个参考&#xff0c;同时也做一个自我总结。 &#xff08;1&#xff09;子项大小不一致的RecyclerView&#xff08;…

[iOS] 折叠 cell

目录 前言 1.原理 2.折叠 cell 的点击选中 3.折叠 cell 高度的变化 4.实现效果 5.总结 前言 折叠 cell 是在 3GShare 中写过的一个小控件&#xff0c;这篇博客是一个小小的总结。 1.原理 在这里的核心就是我们可以通过改变按钮的 tag 值来判断我们是否应该展开还是回收…

MySQL的组复制(MGR)高可用集群搭建

一、MySQL 组复制&#xff08;MGR&#xff09;核心概念 MySQL Group Replication&#xff08;简称 MGR&#xff09;是 MySQL 官方推出的 高可用&#xff08;HA&#xff09; 强一致性 解决方案&#xff0c;基于改进的 Paxos 协议实现&#xff0c;核心能力可概括为 3 点&#xf…

使用Shell脚本实现Linux系统资源监控邮件告警

前言 1. 问题背景与需求 2. Bash 脚本监控资源 3. Bash 脚本判断阈值 4. 配置 msmtp 发送邮件 4.1 安装 msmtp 4.2 创建配置文件 /etc/msmtprc 5. 发送邮件 5.1 给别人发邮件 6. 完整示例脚本 7. 测试方法 8. 常见问题解答 9. 总结 前言 在运维过程中&#xff0c…

设计整体 的 序分(三“释”)、正宗分(双“门”)和流通分(统一的通行表达式) 之3 “自明性”(腾讯元宝 之2)

Q&AQ11、可能还需要补充 魂轴、体轴 和 中心轴 并行 上升 的内容Q11.1、我刚才说“可能还需要补充 魂轴、体轴 和 中心轴 并行 上升 的内容” 是指的 我们今天前面讨论 得出的整体设计 的一个概念整体 的一个双螺旋上升结构中的三个轴。 您刚才是这样 理解的吗&#xff1f;…

使用Ansible自动化部署Hadoop集群(含源码)--环境准备

现在我们有5台虚拟机&#xff0c;已经配置好了主机名和网络我们的目标是通过Ansible实现自动化部署hadoop集群。在此之前&#xff0c;我们先编写一个shell脚本来配置hadoop集群的环境&#xff0c;包括安装软件、安装配置Ansible&#xff08;一个主节点四个工作节点&#xff09;…

C#海康车牌识别实战指南带源码

C#海康车牌识别实战指南带源码前言车牌识别技术在智能交通、停车场管理等领域有着广泛的应用。海康威视作为国内领先的安防厂商&#xff0c;其车牌识别相机提供了丰富的SDK接口供开发者使用。本文将详细介绍如何使用C#语言结合海康威视SDK实现车牌识别功能&#xff0c;并解析关…