目录

ssh免密登陆

利用root账号密码登录ssh服务端

利用root账号免密登录ssh服务端

利用非root账号免密登录ssh服务端

处理git用户的shell为git-shell

出现的问题

git详细教程

搭建git本地服务器

配置ssh远程访问

创建git本地仓库

git clone与git pull的区别 

git如何拉取指定分支的代码

git fetch与git merge

实例


ssh免密登陆


参考文章:http://blog.itpub.net/70017844/viewspace-2909916/
linux系统CentOS Linux release 7.9.2009 (Core)
zabbix-server    192.168.220.128
zabbix-proxy     192.168.220.108

利用root账号密码登录ssh服务端

1.zabbix-server开启ssh服务
[root@zabbix-server ~]# rpm -qf `which ssh`
openssh-clients-7.4p1-21.el7.x86_64
[root@zabbix-server ~]# rpm -qf `which sshd`
openssh-server-7.4p1-21.el7.x86_64
[root@zabbix-server ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2023-04-20 22:35:42 EDT; 4h 27min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 2563 (sshd)
   CGroup: /system.slice/sshd.service
           └─2563 /usr/sbin/sshd -D
........

2.zabbix-proxy账号密码登录zabbix-server
[root@zabbix-proxy ~]# ssh root@192.168.220.128
The authenticity of host '192.168.220.128 (192.168.220.128)' can't be established.
ECDSA key fingerprint is SHA256:WWjD+PCmlUmq2jJJF32DbaHWE32uVKO/h66k32jf9Ns.
ECDSA key fingerprint is MD5:92:ea:a5:d4:a6:ad:10:de:97:a5:bd:4f:67:ce:87:d6.
Are you sure you want to continue connecting (yes/no)? yes    ####输入yes后会添加一条192.168.220.128的信息到客户端~/.ssh下的known_hosts文件
Warning: Permanently added '192.168.220.128' (ECDSA) to the list of known hosts.
root@192.168.220.128's password: 
Last login: Thu Apr 20 23:34:23 2023 from 192.168.220.1
[root@zabbix-server ~]# 


客户端
[root@zabbix-proxy ~]# cat known_hosts 
192.168.220.125 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE7++JhhOslmX5T1/B2knqMR84TycWTSLUyQ2HYpoKMnKtVm2oqDhQuGksf19KjFHYxB7jFT4wpkAHGBwst8RtY=
192.168.220.128 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE7++JhhOslmX5T1/B2knqMR84TycWTSLUyQ2HYpoKMnKtVm2oqDhQuGksf19KjFHYxB7jFT4wpkAHGBwst8RtY=

利用root账号免密登录ssh服务端

###手动复制客户端公钥到服务端authorized_keys文件内实现免密登录的方式有时候不好用,强烈建议使用ssh-copy-id命令
[root@zabbix-proxy ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Hb3aqAwQvPhc4nL1Tf8beXzvL4coAMrtMn7Q4dgVPbc root@zabbix-proxy
The key's randomart image is:
+---[RSA 2048]----+
|         .       |
|   .    . o..    |
|    o    ..o..   |
|   . oo .. .E.   |
|  ..+Bo+S o .    |
|   +===..o =   o |
|  . =o. ..+ o.o.+|
|   oo oo .. ..oo+|
|   ..+  o  .  o==|
+----[SHA256]-----+
将/root/.ssh/id_rsa.pub的内容复制到服务端zabbix-server下的/root/.ssh/authorized_keys文件内        ###将ssh客户端公钥复制到服务端
[root@zabbix-proxy .ssh]# ssh root@192.168.220.128       ###客户端实现免密登录
Last login: Fri Apr 21 03:05:43 2023 from 192.168.220.108
[root@zabbix-server ~]# 

或者通过公钥拷贝指令# ssh-copy-id username@ip
###ssh-copy-id命令会在username用户家目录下创建.ssh/authorized_keys文件,文件内容为免密登录客户机的公钥信息
[root@zabbix-proxy ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Hb3aqAwQvPhc4nL1Tf8beXzvL4coAMrtMn7Q4dgVPbc root@zabbix-proxy
The key's randomart image is:
+---[RSA 2048]----+
|         .       |
|   .    . o..    |
|    o    ..o..   |
|   . oo .. .E.   |
|  ..+Bo+S o .    |
|   +===..o =   o |
|  . =o. ..+ o.o.+|
|   oo oo .. ..oo+|
|   ..+  o  .  o==|
+----[SHA256]-----+
[root@zabbix-proxy .ssh]# ssh-copy-id root@192.168.220.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.220.128's password:         #此处输入密码登录服务端

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.220.128'"
and check to make sure that only the key(s) you wanted were added.

[root@zabbix-proxy .ssh]# ssh 'root@192.168.220.128'
Last login: Fri Apr 21 03:20:32 2023 from 192.168.220.108
[root@zabbix-server ~]# 
[root@zabbix-server ~]# 

利用非root账号免密登录ssh服务端

ssh服务端
[root@zabbix-server .ssh]# useradd -m git
[root@zabbix-server .ssh]# passwd git
Changing password for user git.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@zabbix-server .ssh]# mkdir -p /home/git/.ssh/
[root@zabbix-server .ssh]# vi /home/git/.ssh/authorized_keys    ###将ssh客户端公钥添加到服务端/home/git/.ssh/authorized_keys文件内
[root@zabbix-server .ssh]# chown -R git. /home/git/.ssh/        #重点在修改文件的属性和权限,否则ssh git@192.168.220.128,任然会提示输入git用户密码
[root@zabbix-server .ssh]# chmod -R 700 /home/git/.ssh/
ssh客户端通过非root用户连接服务端
[root@zabbix-proxy .ssh]# ssh git@192.168.220.128
git@192.168.220.128's password: 
[git@zabbix-server ~]$ 
[git@zabbix-server ~]$ 
或者通过公钥拷贝指令ssh-copy-id
ssh服务端
[root@zabbix-server .ssh]# useradd -m git
[root@zabbix-server .ssh]# passwd git
Changing password for user git.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@zabbix-server git]# ls -l /home/git/.ssh/authorized_keys    #git用户目录下没有此文件下面用ssh-copy-id命令生成
ls: cannot access /home/git/.ssh/authorized_keys: No such file or directory
ssh客户端通过非root用户连接服务端
[root@zabbix-proxy ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Hb3aqAwQvPhc4nL1Tf8beXzvL4coAMrtMn7Q4dgVPbc root@zabbix-proxy
The key's randomart image is:
+---[RSA 2048]----+
|         .       |
|   .    . o..    |
|    o    ..o..   |
|   . oo .. .E.   |
|  ..+Bo+S o .    |
|   +===..o =   o |
|  . =o. ..+ o.o.+|
|   oo oo .. ..oo+|
|   ..+  o  .  o==|
+----[SHA256]-----+
[root@zabbix-proxy ~]# ssh-copy-id git@192.168.220.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.220.128's password:         #输入git密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@192.168.220.128'"
and check to make sure that only the key(s) you wanted were added.

[root@zabbix-proxy ~]# ssh git@192.168.220.128
Last login: Fri Apr 21 04:20:12 2023 from 192.168.220.108
[git@zabbix-server ~]$ 

ssh服务端
[root@zabbix-server ~]# ls -l /home/git/.ssh/authorized_keys        ##可以看到公钥被拷贝到authorized_keys文件内
-rw------- 1 git git 399 Apr 21 04:19 /home/git/.ssh/authorized_keys

处理git用户的shell为git-shell

# rpm -qf `which git-shell`   #git-shell属于git软件包
git-1.8.3.1-25.el7_9.x86_64

在Git用作仓库的时候我们可以将默认ssh登录shell改为git-shell,这个git提供的shell程序,一旦登录会自动秒退  #为了安全考虑我们需要禁用ssh登录到我们的shell,防止别人登录到shell之后对我们的电脑做增删改
ssh服务器
[root@zabbix-server git]# chsh -s `which git-shell` git
Changing shell for git.
chsh: Warning: "/usr/bin/git-shell" is not listed in /etc/shells.
Shell changed.
ssh客户端
[root@zabbix-proxy ~]# ssh git@192.168.220.128
git@192.168.220.128's password: 
Last login: Fri Apr 21 04:23:57 2023 from 192.168.220.108
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.220.128 closed.
[root@zabbix-proxy ~]# 

出现的问题

问题1:由于用户git在服务器端的 shell 为 git-shell,在使用 ssh-copy-id 的时候报错:
[root@zabbix-proxy test]# ssh-copy-id git@192.168.220.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.220.128's password: 
fatal: unrecognized command 'exec sh -c 'cd ; umask 077 ; mkdir -p .ssh && cat >> .ssh/authorized_keys || exit 1 ; if type restorecon >/dev/null 2>&1 ; then restorecon -F .ssh .ssh/authorized_keys ; fi''
解决办法
[root@zabbix-proxy .ssh]# scp /root/.ssh/id_rsa.pub root@192.168.220.128:/home/git/.ssh/authorized_keys   #确保ssh服务器/home/git/.ssh/目录存在,scp是ssh的文件复制命令   
id_rsa.pub                                                     100%  399   373.5KB/s   00:00   
[root@zabbix-proxy .ssh]# ssh root@192.168.220.128
Last login: Fri Apr 21 04:12:25 2023 from 192.168.220.108
[root@zabbix-server ~]# cd /home/git/
[root@zabbix-server git]# chown -R git. .ssh
[root@zabbix-server git]# chmod -R 700 .ssh/
[root@zabbix-server git]# ll /home/git/.ssh/
total 4
-rwx------ 1 git git 399 Apr 21 05:35 authorized_keys
 

git详细教程

Git 分支管理 | 菜鸟教程
关于Git这一篇就够了_17岁boy想当攻城狮的博客-CSDN博客
Git简明指南git 简明指南

搭建git本地服务器

环境
[root@zabbix-server ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
zabbix-server    192.168.220.128
zabbix-proxy     192.168.220.108
分别已安装软件  yum install git ssh -y;service ssh start 

配置ssh远程访问

zabbix-server服务端
[root@zabbix-server ~]# useradd -m git
[root@zabbix-server ~]# passwd git       #给git用户设置密码
Changing password for user git.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
zabbix-proxy客户端
[root@zabbix-proxy ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fmDBtjmOjnHxy/HOhtT4+PcvyZ91533TbelGyo8n5Sw root@zabbix-proxy
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|       .         |
|        +        |
|       . +       |
|      . So       |
|       Boo.    o |
|    . o.=+. ..*.B|
|     = .o*o .E=O@|
|    . . o+=. oOOB|
+----[SHA256]-----+
[root@zabbix-proxy ~]# ssh-copy-id git@192.168.220.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.220.128's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@192.168.220.128'"
and check to make sure that only the key(s) you wanted were added.

[root@zabbix-proxy ~]# ssh 'git@192.168.220.128'                  #免密访问
Last failed login: Fri Apr 21 05:05:04 EDT 2023 from 192.168.220.108 on ssh:notty
There was 1 failed login attempt since the last successful login.
[git@zabbix-server ~]$ 
zabbix-server服务端
[root@zabbix-server .ssh]# chown -R git. /home/git/.ssh/
[root@zabbix-server .ssh]# chmod -R 700 /home/git/.ssh/
[root@zabbix-server .ssh]# chsh -s `which git-shell` git               #修改git用户的shell,防止客户机ssh通过git用户登录服务器修改文件
Changing shell for git.
chsh: Warning: "/usr/bin/git-shell" is not listed in /etc/shells.
Shell changed.

创建git本地仓库

zabbix-server服务端
创建仓库存放路径并设置所属用户为git,路径可任意起名,这里叫gitrepo。
[root@zabbix-server .ssh]# mkdir -p /home/git/gitrepo
初始化仓库并设置所属用户为git,这里建了一个叫test的仓库。
root@zabbix-server .ssh]# cd /home/git/gitrepo/
[root@zabbix-server gitrepo]# git init --bare test.git
Initialized empty Git repository in /home/git/gitrepo/test.git/
[root@zabbix-server gitrepo]# chown -R git. /home/git/gitrepo/          ##因为这里的gitrepo路径下有多个文件,所以加-R参数递归
到此,git服务器就算搭建完成,URL地址如下:
git@192.168.220.128:/home/git/gitrepo/test.git

测试1 --git clone
以下简单测试Clone和Push,如果你之前用过git,那么得到上边的仓库地址后应该就非常熟悉了。
clone--zabbix-proxy客户端
[root@zabbix-proxy ~]# mkdir -p $HOME/A
[root@zabbix-proxy ~]# cd A/
[root@zabbix-proxy A]# git clone git@192.168.220.128:/home/git/gitrepo/test.git   #这里并不需要输入上边创建git用户的密码,如果需要输入密码,说明公钥添加有误,请重新检查。
Cloning into 'test'...
warning: You appear to have cloned an empty repository.
[root@zabbix-proxy A]# ls
test
[root@zabbix-proxy A]# cd test
[root@zabbix-proxy test]# ls
[root@zabbix-proxy test]# touch 1
[root@zabbix-proxy test]# git add .
[root@zabbix-proxy test]# git commit -m "ceshi"
[master (root-commit) e3b8d5b] ceshi
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1
[root@zabbix-proxy test]# git push origin master      #通过克隆来的远程仓库,默认有个别名叫origin,所以推送可以直接这样:
Counting objects: 3, done.
Writing objects: 100% (3/3), 193 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.220.128:/home/git/gitrepo/test.git
 * [new branch]      master -> master
[root@zabbix-proxy test]# mkdir -p $HOME/B             #再次克隆
[root@zabbix-proxy test]# cd $HOME/B
[root@zabbix-proxy B]# git clone git@192.168.220.128:/home/git/gitrepo/test.git
Cloning into 'test'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@zabbix-proxy B]# ls
test
[root@zabbix-proxy B]# cd test
[root@zabbix-proxy test]# ls               #可以看到这个就是刚才提交文件
1

测试2 --git remote   #需要有本地仓库
[root@zabbix-proxy ~]# mkdir -p $HOME/C
[root@zabbix-proxy ~]# cd $HOME/C        
[root@zabbix-proxy C]# git init        #初始化创建本地仓库
Initialized empty Git repository in /root/C/.git/
[root@zabbix-proxy C]# git remote add origin git@192.168.220.128:/home/git/gitrepo/test.git       #git远程添加源
[root@zabbix-proxy C]# git push -u origin master    #将本地的master分支推送到origin主机,同时指定origin为默认主机
error: src refspec master does not match any.
error: failed to push some refs to 'git@192.168.220.128:/home/git/gitrepo/test.git'
[root@zabbix-proxy C]#  git pull git@192.168.220.128:/home/git/gitrepo/test.git    ##在上传你的代码之前因为产生冲突会报错,提交不上,通常要进行拉取代码。将远程仓库主分支文件下载到本地仓库并创建master分支,等同于git pull origin master
remote: Counting objects: 26, done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 26 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (26/26), done.
From 192.168.220.128:/home/git/gitrepo/test
 * branch            HEAD       -> FETCH_HEAD
[root@zabbix-proxy C]# ls
1  README  runoob.php  test.txt
[root@zabbix-proxy C]# git branch
* master
[root@zabbix-proxy C]# echo 1111111 >ceshi
[root@zabbix-proxy C]# git add .
[root@zabbix-proxy C]# git commit -m "add ceshi"
[master 6e0e239] add ceshi
 1 file changed, 1 insertion(+)
 create mode 100644 ceshi
[root@zabbix-proxy C]# git push -u origin master    ##将本地的master分支推送到origin主机,同时指定origin为默认主机
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 257 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@192.168.220.128:/home/git/gitrepo/test.git
   0e88423..6e0e239  master -> master
Branch master set up to track remote branch master from origin.
[root@zabbix-proxy C]# 

git clone与git pull的区别 

点击下面链接:
git上传_提交代码前先git pull吗_年少不知富婆好的博客-CSDN博客

git如何拉取指定分支的代码

问题背景:

  新项目还在开发阶段,没有正式对外发布,所以开发同事合并代码到develop上(或者其他名称分支上),而不是到master分支上

  通过git拉取代码的时候,默认拉取的是master分支,如下图:

拉取指定分支代码解决方案:

以拉取develop分支的代码为例, 要拉取其余分支代码类似操作

1.使用git命令拉取

命令:git clone -b develop XXX 其中develop就是分支的名称

2.使用TortoiseGit工具拉取 

git fetch与git merge

参考文章:git fetch - git merge - git pull 指令_git fetch merge-CSDN博客

参考内容:Git 远程仓库(Github) | 菜鸟教程

  • 执行 git fetch origin master 时,它的意思是从名为 origin 的远程上获取名为 master 的分支更新,这将更新远程仓库origin中master分支的最新commit-id, 将其记录到.git/FETCH_HEAD文件中。
    FETCH_HEAD: 是一个版本链接,记录在本地的一个文件中,指向目前已经从远程仓库取下来的分支的末端版本。
  • 执行 git merge origin/master 时,它的意思是将远程仓库origin的master分支的更新(假设有人这时候推送到服务器了)合并到你的当前分支
  • 执行 git push origin master 时,它的意思是推送本地的 master 分支到远程 origin仓库的master分支
  • 还可以一次性获取多个分支的更新信息:git fetch origin master stable oldstable
  • 也还可以一次性合并多个分支的代码:git merge origin/master hotfix-2275 hotfix-2276 hotfix-2290
客户端A
[root@dongm xuexi]# ls
11111  1.txt  ceshi.c  ceshi.ccccccc  ceshi.sh  test.c  test.txt  xinjian.txt
[root@dongm xuexi]# echo  555555 >11111111111.txt
[root@dongm xuexi]# git add .
[root@dongm xuexi]# git commit -m "add 1111111111.txt"
[master c3d767b] add 1111111111.txt
 1 file changed, 1 insertion(+)
 create mode 100644 11111111111.txt
[root@dongm xuexi]# git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:dongmin6/test.git
   3f7513d..c3d767b  master -> master        ##增加文件11111111111.txt
[root@dongm xuexi]# ls
11111  11111111111.txt  1.txt  ceshi.c  ceshi.ccccccc  ceshi.sh  test.c  test.txt  xinjian.txt



客户端B
[root@dongm test]# git fetch origin
From github.com:dongmin6/test
   3f7513d..c3d767b  master     -> origin/master     ##与仓库新增文件11111111111.txt对应
[root@dongm test]# ls
11111  1.txt  ceshi.c  ceshi.ccccccc  ceshi.sh  README.md  test.c  test.txt  xinjian.txt
[root@dongm test]# git merge origin/master
Merge made by the 'recursive' strategy.
 11111111111.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 11111111111.txt
[root@dongm test]# ls
11111  11111111111.txt  1.txt  ceshi.c  ceshi.ccccccc  ceshi.sh  README.md  test.c  test.txt  xinjian.txt

实例

客户端A
# git branch -av      注释:列出所有现有分支
* master                d922f89 Mon Apr 29 16:12:08 CST 2024
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master d922f89 Mon Apr 29 16:12:08 CST 2024
  remotes/origin/test   ebfb711 Merge branch 'test' of 192.168.220.125:/home/git/gitrepo/test into test
# git fetch origin     注释:从origin远程仓库上获取分支更新信息
# git branch -av
* master                d922f89 Mon Apr 29 16:12:08 CST 2024
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master d922f89 Mon Apr 29 16:12:08 CST 2024
  remotes/origin/test   ebfb711 Merge branch 'test' of 192.168.220.125:/home/git/gitrepo/test into test
说明:remotes/origin/master指向本地仓库的.git/refs/remotes/origin/master文件,cat .git/refs/remotes/origin/master保存了远程仓库origin中master分支的最新commit-id

客户端B推送代码
# git push origin master 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@192.168.220.125:/home/git/gitrepo/test.git
   d922f89..3cb380d  master -> master    注释:远程origin/master分支仓库哈希id由d922f89更新至3cb380d

客户端A再次获取远端最新改动
# git fetch origin   
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.220.125:/home/git/gitrepo/test
   d922f89..3cb380d  master     -> origin/master   注释:d922f89..3cb380d  master  -> origin/master说明 master 分支已被更新,我们可以使用merge命令将更新同步到本地
# git branch -av
* master                d922f89 [behind 1] Mon Apr 29 16:12:08 CST 2024
  remotes/origin/HEAD   -> origin/master 注释:remotes/origin/HEAD指向origin/master
  remotes/origin/master 3cb380d Adding 3.txt    注释:本地remotes/origin/master哈希id已由d922f89更新至3cb380d
  remotes/origin/test   ebfb711 Merge branch 'test' of 192.168.220.125:/home/git/gitrepo/test into test
# git branch -r   注释:查看远程仓库分支信息
  origin/HEAD -> origin/master  注释:远程仓库origin/HEAD指向origin/master
  origin/master
  origin/test

-----------------------------------------------------------------------------------------
HEAD文件指向本地仓库的说明
# git branch
  cccccc
  master
* test      注释:当前位于test分支仓库
# ls
branches  COMMIT_EDITMSG  config  description  FETCH_HEAD  HEAD  hooks  index  info  logs  objects  ORIG_HEAD  refs
# cat HEAD  注释:HEAD为相对位置,文件位于本地仓库.git目录下,当前指向test分支仓库
ref: refs/heads/test
# git checkout master 
fatal: This operation must be run in a work tree
# cd ..
# ls
1.txt  ceshi  注释:仓库代码文件
# git checkout master 
Switched to branch 'master'
# cd .git/
# ls
branches  COMMIT_EDITMSG  config  description  FETCH_HEAD  HEAD  hooks  index  info  logs  objects  ORIG_HEAD  refs
# cat HEAD  注释:当前指向master主仓库
ref: refs/heads/master

切换HEAD分支
git checkout <branch>

-----------------------------------------------------------------------------------------
error: failed to push some refs to 'github.com:dongmin6/like.git'与
fatal: Need to specify how to reconcile divergent branches. 的分析与处理

直接获取master分支
# mkdir ceshi
# cd ceshi/
# ls -a
.  ..
# git init   注释:初始化本地仓库
Initialized empty Git repository in /root/git/ceshi/.git/
# ls -a
.  ..  .git
# git remote add origin git@github.com:dongmin6/like.git    注释:添加远程仓库
# git remote -v
origin	git@github.com:dongmin6/like.git (fetch)
origin	git@github.com:dongmin6/like.git (push)
# git branch -r             注释:查看指向远程仓库的分支信息
# git branch -av            注释:查看本地仓库的分支
# git fetch origin          注释:获取指向远程仓库信息的分支
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 30 (delta 5), reused 26 (delta 4), pack-reused 0
Unpacking objects: 100% (30/30), 3.10 KiB | 793.00 KiB/s, done.
From github.com:dongmin6/like
 * [new branch]      ceshi      -> origin/ceshi
 * [new branch]      like       -> origin/like
 * [new branch]      main       -> origin/main
 * [new branch]      master     -> origin/master
 * [new branch]      test       -> origin/test
# git branch -r
  origin/ceshi
  origin/like
  origin/main
  origin/master
  origin/test
# git branch -av
  remotes/origin/ceshi  10eabbd Adding ceshi
  remotes/origin/like   f03fae0 Adding like.txt
  remotes/origin/main   0b32f2c Delete test.txt branchceshi.txt
  remotes/origin/master b30ef8a Adding 3.txt
  remotes/origin/test   9df25f6 Adding main.txt
# git merge origin/master       注释:合并已获取的指向分支origin/master
# ls
1.txt  3.txt
# git branch -av
* master                b30ef8a Adding 3.txt      注释:新增了分支master
  remotes/origin/ceshi  10eabbd Adding ceshi
  remotes/origin/like   f03fae0 Adding like.txt
  remotes/origin/main   0b32f2c Delete test.txt branchceshi.txt
  remotes/origin/master b30ef8a Adding 3.txt
  remotes/origin/test   9df25f6 Adding main.txt
# git merge origin/ceshi        注释:再次合并已获取的指向分支origin/ceshi
Merge made by the 'ort' strategy.
 ceshi | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 ceshi
# git branch
* master
# ls
1.txt  3.txt  ceshi           注释:新增了文件ceshi


先创建master分支
# mkdir test
# cd test
# ls -a
.  ..
# git init      注释:初始化仓库
Initialized empty Git repository in /root/git/C/.git/
# ls -a
.  ..  .git
# echo "test.txt" >test.txt
# git add .    注释:提交到暂存区,自动在./git目录下创建index文件
# git branch -av
# git commit -m "Submit test.txt"    注释:提交到本地仓库,在./git目录下创建COMMIT_EDITMSG文件
[master (root-commit) e6096c5] Submit test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
# git branch -av        注释:git commit提交后自动创建默认主分支master
* master e6096c5 Submit test.txt   
# git remote -v
# git remote add origin git@github.com:dongmin6/like.git
# git remote -v
origin	git@github.com:dongmin6/like.git (fetch)
origin	git@github.com:dongmin6/like.git (push)
# git push origin master
To github.com:dongmin6/like.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'github.com:dongmin6/like.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#########################################################
error: failed to push some refs to 'github.com:dongmin6/like.git'

报错解释:
这个错误通常发生在使用Git推送(push)到远程仓库时,意味着Git无法推送一些引用(如分支或标签)。这可能是由于本地分支的状态与远程仓库不同步,或者你没有足够的权限推送到远程仓库。

解决方法:
确认你有权限推送到该远程仓库。如果仓库是私有的,你需要确保你的GitHub账户有权限。
尝试先拉取(pull)远程仓库的最新内容,并合并到你的本地分支:
git pull origin master
替换master为你想要拉取的分支名。
如果存在冲突,解决它们后继续推送。
如果你不想拉取远程改动,可以使用强制推送,但这可能会覆盖远程仓库的改动:
git push -f origin master
再次替换master为你的分支名,但请小心使用,因为这可能会导致别人的工作丢失。
如果你正处于一个非常新的分支上,可以尝试先将分支推送到远程仓库,然后再尝试拉取:
git push -u origin your-branch
替换your-branch为你的分支名。
确保在推送之前你的分支是最新的,并且你有正确的权限。如果问题依然存在,可能需要查看具体的错误信息或者联系远程仓库的管理员。
###########################################################

# git fetch origin master
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 436 bytes | 436.00 KiB/s, done.
From github.com:dongmin6/like
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
# git merge  origin/master
fatal: refusing to merge unrelated histories       解决方案:在你操作命令后面加 --allow-unrelated-histories  (git pull origin master --allow-unrelated-histories / git pull --allow-unrelated-histories)
# git pull origin master --allow-unrelated-histories
From github.com:dongmin6/like
 * branch            master     -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
###############################翻译与解决办法###################################
# git pull origin master --allow-unrelated-histories
提示:您有不同的分支,需要指定如何协调它们。
提示:您可以通过在之前某个时间运行以下命令之一来做到这一点
提示:你的下一招:
提示:
提示:git config pull.rebase false 	# 合并(默认策略)
提示:git config pull.rebase true  	# Rebase
提示:git config pull.ff only	 	# 仅快进
提示:
提示:可以将"git config"替换为"git config --global"来设置默认值
提示:首选所有存储库。你也可以传递--rebase,--no-rebase,
提示:或命令行上的--ff-only,以覆盖配置的默认per
提示:调用。
fatal:需要指定如何协调不同的分支。

解决方法:参考https://blog.csdn.net/qq_45677671/article/details/122574671
分析:这是由于你拉取pull分支前,进行过merge合并更新分支操作,而其他人在你之前已经push过一个版本,导致版本不一致(本地和远程2个版本不相关)
第一种解决方法:比较简单
执行git config pull.rebase false
默认将pull下来的代码与现有改动的代码进行合并
但是可能会造成代码冲突,需要处理下这个问题,代码冲突如果2个人都改了同一个文件,需要联系之前push的同学,看看这块代码怎么保存
第二种解决方法:回退到合并之前的代码,在进行pull拉取最新代码
#################################################################################

# git branch -av
* master                e6096c5 Submit test.txt
  remotes/origin/master b30ef8a Adding 3.txt
# git branch -r
  origin/master
# git log origin/master
commit b30ef8a10080bf8b95d72467f561b8214b5a1d23 (origin/master)
Author: Your Name <you@example.com>
Date:   Tue May 7 09:52:24 2024 -0400

    Adding 3.txt

commit d79b263652f2fa3ab77edba2b531bee2526f983d
Author: Your Name <you@example.com>
Date:   Tue May 7 09:17:13 2024 -0400

    Adding 1.txt
# git log -p master..origin/master            注释:比较本地的 master 分支和 origin/master 分支的差别
commit b30ef8a10080bf8b95d72467f561b8214b5a1d23 (origin/master)
Author: Your Name <you@example.com>
Date:   Tue May 7 09:52:24 2024 -0400

    Adding 3.txt

diff --git a/3.txt b/3.txt
new file mode 100644
index 0000000..cdc0d58
--- /dev/null
+++ b/3.txt
@@ -0,0 +1 @@
+33333333

commit d79b263652f2fa3ab77edba2b531bee2526f983d
Author: Your Name <you@example.com>
Date:   Tue May 7 09:17:13 2024 -0400

    Adding 1.txt

diff --git a/1.txt b/1.txt
new file mode 100644
index 0000000..0527e6b
--- /dev/null
+++ b/1.txt
@@ -0,0 +1 @@
+This is a test
# ls
test.txt
# git config pull.rebase false
# git pull origin master --allow-unrelated-histories
From github.com:dongmin6/like
 * branch            master     -> FETCH_HEAD
Merge made by the 'ort' strategy.
 1.txt | 1 +
 3.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 1.txt
 create mode 100644 3.txt
[root@iZbp10igf852350ib2ny5fZ test]# ls
1.txt  3.txt  test.txt
# git branch -av
* master                b2ec5b6 Merge branch 'master' of github.com:dongmin6/like
  remotes/origin/master b30ef8a Adding 3.txt

使用--allow-unrelated-histories标志允许合并不相关的历史。这是一种解决问题的快速方法,但请谨慎使用,因为它将两个不同的历史合并到一个新的共同历史中。这可能会导致冲突,我们需要解决这些冲突后再次提交。                 
原文链接:https://blog.csdn.net/Tester_muller/article/details/132815155

Logo

更多推荐