GIT gitlub的使用

Git 是一种分布式版本控制系统,用于跟踪和管理代码的变更。它是由 Linus Torvalds 创建的,最初被设计用于 Linux 内核的开发。Git 允许开发 人员跟踪和管理代码的版本,并且可以在不同的开发人员之间进行协作。

一、Git的功能特性

1. 克隆数据库版本: 从服务器上克隆版本数据库(包括代码和版本信息)到本 机上;

2. 提交代码: 在本机上自己创建的分支上提交代码;

3. 合并分支: 在本机上合并分支;

4. 拉取合并分支: 新建一个分支,把服务器上最新版的代码 Fetch 下来, 然后跟自己的主分支合并;

5. 代码冲突解决: 一般开发者之间解决冲突的方法,开发者之间可以使用 pull 命令解决冲突,解决完冲突之后再向主开发者提交补丁。

二、Git 的安装及配置

1、安装

yum -y install git


2、常见命令

开始一个工作区(参见:git help tutorial)
   clone     克隆仓库到一个新目录
   init      创建一个空的 Git 仓库或重新初始化一个已存在的仓库
在当前变更上工作(参见:git help everyday)
   add       添加文件内容至索引
   mv        移动或重命名一个文件、目录或符号链接
   restore   恢复工作区文件
   rm        从工作区和索引中删除文件
检查历史和状态(参见:git help revisions)
   bisect    通过二分查找定位引入 bug 的提交
   diff      显示提交之间、提交和工作区之间等的差异
   grep      输出和模式匹配的行
   log       显示提交日志
   show      显示各种类型的对象
   status    显示工作区状态
扩展、标记和调校您的历史记录
   branch    列出、创建或删除分支
   commit    记录变更到仓库
   merge     合并两个或更多开发历史
   rebase    在另一个分支上重新应用提交
   reset     重置当前 HEAD 到指定状态
   switch    切换分支
   tag       创建、列出、删除或校验一个 GPG 签名的标签对象
协同(参见:git help workflows)
   fetch     从另外一个仓库下载对象和引用
   pull      获取并整合另外的仓库或一个本地分支
   push      更新远程引用和相关的对象


3、配置git环境

[root@YH1 ~]# git config --global user.name "admin"     
配置用户名
[root@YH1 ~]# git config --global user.email 
"yh123456@qq.com"      
# 配置邮箱


三、Git基本流程

1、创建空仓库

[root@YH1 ~]# mkdir /test       
# 创建一个空目录
[root@YH1 ~]# cd /test/
 [root@YH1 test]# git init   # 将当前空目录做为master仓库
提示:使用 'master' 作为初始分支的名称。这个默认分支名称可能会更改。
要在新仓库中
已初始化空的 Git 仓库于 /test/.git/
 [root@YH1 test]# ls -a      
# 查看git隐藏的相关的配置文件
.  ..  .git
 [root@YH1 test]# cd .git/


2、创建并提交文件到仓库

[root@YH1 test]# touch test.c
[root@YH1 test]# ls -a
 .  ..  .git  test.c
[root@YH1 test]# git add test.c
[root@YH1 test]# git commit -m "add new file "test.c""
 [master(根提交) 9e89cac] add new file test.c
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.c


3、查看历史提交信息

[root@YH1 test]# git log
 commit 9e89cace2f47e12178fc54f964d01b176b25c968 (HEAD -> 
master)        
# 40位的哈希算法算出的id
 # head->master表示提交到master主仓库,如果是head->其他分支,表示提
交到其他分支仓库
Author: admin <yh123456@qq.com>     
Date:   
Wed Nov 15 21:00:07 2023 +0800      
add new file test.c     


四、分支管理

1、创建分支过程

# 确保你当前在主分支(通常是master或main分支)上工作
[root@YH1 ~]# cd /test  # 先切换到之前创建master主分支
[root@YH1 ~]# git checkout master   # 确保当前操作位置是在主分
支上
# 创建一个新的分支
[root@YH1 ~]# git branch feature-branch
# 切换分支
[root@YH1 ~]# git checkout feature-branch
[root@YH1 test]# git branch     
* feature-branch        
  master
# -b 选项相当于执行两条命令,git checkout 和 git branch
[root@YH1 ~]# git checkout -b feature-branch
[root@YH1 test]# git branch         
* feature-branch
  master

2、在新分支编写提交

[root@YH1 test]# vim test.c   # 添加新内容  
hello
test
[root@YH1 ~]# git add .         
# 提交到缓存区,这里也可以写
master分支的目录名,比如test
[root@YH1 ~]# git commit -m "提交消息"      
 
# 切换回主分支
[root@YH1 ~]# git checkout master
# 分支合并到主分支
[root@YH1 ~]# git merge feature-branch
[root@YH1 test]# cat test.c     
hello
test
# 删除分支
[root@YH1 ~]# git branch -d feature-branch
# 强制删除
[root@YH1 ~]# git branch -D feature-branch

3、解决分支冲突

分支冲突主要是因为两个分支内容不一致时,合并造成冲突

办法:将形成冲突的文件手动解决冲突问题

[root@YH1 test]# git checkout -b y123       
分支切换到一个新分支 'y123'
[root@YH1 test]# vim test.c         
# 写入点内容
hello
 test
 yyyy
[root@YH1 test]# git add test.c     
缓存
[root@YH1 test]# git commit -m "yy"     
[root@YH1 test]# git checkout master        # 切换到主分支
[root@YH1 test]# cat test.c     # 此时还未合并,所以只显示当前master分支的内容
hello
 test
[root@YH1 test]# vim test.c         # 向master分支内写入些内容
hello
 test
 hhhh
[root@YH1 test]# git add test.c         # 提交master分支
[root@YH1 test]# git commit -m "hh"     # 正式提交,注释信息为“hh”
[root@YH1 test]# git merge y123     # 合并新分支
自动合并 test.c
冲突(内容):合并冲突于 test.c     # 提示冲突了
自动合并失败,修正冲突然后提交修正的结果。
[root@YH1 test]# git status     # git状态信息也提示两个分支冲突
了
位于分支 master
您有尚未合并的路径。
  (解决冲突并运行 "git commit")
  (使用 "git merge --abort" 终止合并)
未合并的路径:
  (使用 "git add <文件>..." 标记解决方案)
    双方修改:   test.c
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@YH1 test]# cat test.c         # git使用记号帮我们标记除了冲突位置和新分支的内容
hello
 test
 <<<<<<< HEAD
 hhhh
 =======
 yyyy
 >>>>>>> y123
[root@YH1 test]# vim test.c     
hello
 test
 hhhh
 yyyy
[root@YH1 test]# git add test.c
[root@YH1 test]# git commit -m "zuizhong"

五、git的拉取

 [root@YH2 ~]# ssh-keygen
 [root@YH2 ~]# ssh-copy-id root@192.168.33.11
 [root@YH2 ~]# yum -y install git
 [root@YH2 ~]# mkdir /yh2        
# 新建git仓库
[root@YH2 ~]# cd /yh2/
 [root@YH2 yh2]# git init        
# 仓库初始化,当前目录作为
master
提示:使用 'master' 作为初始分支的名称。这个默认分支名称可能会更改。
要在新仓库中
提示:配置使用初始分支名,并消除这条警告,请执行:
提示:
提示: git config --global init.defaultBranch <名称>
提示:
提示:除了 'master' 之外,通常选定的名字有 'main'、'trunk' 和 
'development'。
提示:可以通过以下命令重命名刚创建的分支:
提示:
提示: git branch -m <name>
已初始化空的 Git 仓库于 /yh2/.git/
 [root@YH2 yh2]# git clone 192.168.33.11:/test/.git      
将YH1的test分支克隆到本机
正克隆到 'test'...
 remote: 枚举对象中: 15, 完成.
 remote: 对象计数中: 100% (15/15), 完成.
 remote: 压缩对象中: 100% (5/5), 完成.
 remote: 总共 15(差异 0),复用 0(差异 0),包复用 0
接收对象中: 100% (15/15), 完成.
 [root@YH2 yh2]# ls
 test
 [root@YH2 yh2]# ls test/
 test.c

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值