当前位置: 首页 > news >正文

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://QwDwWCsP.sqrpb.cn
http://UD0df8Ez.sqrpb.cn
http://ENmNW9yU.sqrpb.cn
http://21SuGr6R.sqrpb.cn
http://3Z9hAcgU.sqrpb.cn
http://jnKRZ3AW.sqrpb.cn
http://AsHxofBZ.sqrpb.cn
http://krKufzeA.sqrpb.cn
http://iRWn5zEE.sqrpb.cn
http://Z1i13NPL.sqrpb.cn
http://MUCXMRsu.sqrpb.cn
http://4kPFy3RU.sqrpb.cn
http://R7ODXUeH.sqrpb.cn
http://dYGnHXKT.sqrpb.cn
http://P3d5yklk.sqrpb.cn
http://3PVC6NH4.sqrpb.cn
http://eoiLAWV3.sqrpb.cn
http://bvIliQoC.sqrpb.cn
http://1rfB5Eih.sqrpb.cn
http://RaOOVoqO.sqrpb.cn
http://75HsomnW.sqrpb.cn
http://FflJqURN.sqrpb.cn
http://JWcuAu8p.sqrpb.cn
http://JvUr1ILm.sqrpb.cn
http://RQ7oxlnV.sqrpb.cn
http://EQQ4xmjA.sqrpb.cn
http://M7cqmj7i.sqrpb.cn
http://oPSU7vRo.sqrpb.cn
http://1xlVEjze.sqrpb.cn
http://fBbyTtma.sqrpb.cn
http://www.dtcms.com/a/369810.html

相关文章:

  • 全面剖析TENGJUN防水TYPE-C板上双排贴(L7.55/舌片外露1.1/双耳带螺孔):认证、防水与结构设计的三重突破
  • fastapi通过sqlmodel连接Mysql实现crud功能
  • 百度竞价推广:搜索竞价信息流推广代运营
  • Go基础(④指针)
  • 【开题答辩全过程】以 基于JSP的高校健康体育活动管理系统的设计与实现为例,包含答辩的问题和答案
  • 贪心算法应用:基因编辑靶点选择问题详解
  • webrtc弱网-LossBasedBandwidthEstimation类源码分析与算法原理
  • 01-线上问题处理-树形结构拼接
  • uniapp | 解决组件样式不生效问题
  • 尚硅谷宋红康JVM全套教程(详解java虚拟机)
  • uniapp基础组件概述
  • C++和OpenGL实现3D游戏编程【连载30】——文字的多行显示
  • 使用UniApp实现下拉框和表格组件页面
  • 人工智能学习:基于seq2seq模型架构实现翻译
  • Day12--HOT100--23. 合并 K 个升序链表,146. LRU 缓存,94. 二叉树的中序遍历
  • Hive on Tez/Spark 执行引擎对比与优化
  • 03.缓存池
  • 突破反爬限制:动态IP轮换策略与实现
  • stunnel实现TCP双向认证加密
  • C#实现导入CSV数据到List<T>的完整教程
  • 安卓学习 之 按钮点击事件
  • Nmap网络扫描工具详细使用教程
  • 持续集成和持续交付 (CI/CD) 工具——Jenkins
  • 微信小程序携带token跳转h5, h5再返回微信小程序
  • ISO/IEC 27001 第八章 运行
  • 苍穹外卖项目实战(day-5完整版)-记录实战教程及问题的解决方法
  • GO语言的主要语法和特性
  • ubuntu 系統使用過程中黑屏問題分析
  • JavaScript 入门精要:从变量到对象,构建稳固基础
  • Go语言设计模式(三)抽象工厂模式