git的基本使用

本文详细介绍了如何在服务端和客户端上安装Git,包括通过yum源和编译方式安装,创建git用户,实现免密登录,创建并初始化空仓库,以及进行远程克隆和推送测试。同时,还涉及了修改git用户权限和配置个人身份信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主机名IP角色
git1192.168.100.130服务端
git2192.168.100.131客户端

一、安装git
(1)使用yum源安装

# yum -y install git

(2)使用编译暗转git
安装依赖包

[root@git1 ~]# yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel wget gcc perl-ExtUtils-MakeMaker

下载安装包

[root@git1 ~]# cd /usr/local/src/
[root@git1 src]# wget https://www.kernel.org/pub/software/scm/git/git-2.10.0.tar.gz

编译安装

[root@git1 src]# tar zxf git-2.10.0.tar.gz
[root@git1 src]# cd /usr/local/src/git-2.10.0
[root@git1 git-2.10.0]# ./configure --prefix=/usr/local/git/ && make install

配置环境变量

[root@git1 git-2.10.0]# echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc
[root@git1 git-2.10.0]# source /etc/bashrc
[root@git1 git-2.10.0]# git --version
git version 2.10.0

二、在服务端创建git用户,密码为“git”

[root@git1 ~]# useradd git
[root@git1 ~]# passwd git
Changing password for user git.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

三、配置服务端git用户与客户端的root用户免密登录
服务端

[git@git1 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa): 
Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qil6K2goAsKQYY2DshdtjaXsRUQFBOLNkh1mJFSndNc git@git1
The key's randomart image is:
+---[RSA 2048]----+
|..*+OOXoo.       |
|+=.&+B..  E      |
|o+=oB .          |
|+ .o .           |
|o.  .   S        |
|o.     .         |
|=     .          |
|*.o  o           |
|=+.oo            |
+----[SHA256]-----+
[git@git1 ~]$ ssh-copy-id root@192.168.100.131
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/git/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.131 (192.168.100.131)' can't be established.
ECDSA key fingerprint is SHA256:TF6Ncozs5ykItu5PdJsfvCopUH+mh2L+Bxxszboj5m8.
ECDSA key fingerprint is MD5:51:94:f9:7e:16:ee:5b:b9:e6:f0:5b:57:bd:91:b2:e9.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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.100.131's password: 

Number of key(s) added: 1

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


客户端

[root@git2 ~]# ssh-keygen
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:C5lpAAeGtw3lQ6TiP8JCnj3xq/S7Bw/BHKoVQFSwEH8 root@git2
The key's randomart image is:
+---[RSA 2048]----+
|=BB==            |
|oo+B .           |
|.oo+E .          |
|...+.* +         |
| oo.  B S        |
|+.+ o+ . .       |
|.= * .+ .        |
|. o + .o         |
|   ..=+          |
+----[SHA256]-----+
[root@git2 ~]# ssh-copy-id git@192.168.100.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.130 (192.168.100.130)' can't be established.
ECDSA key fingerprint is SHA256:TF6Ncozs5ykItu5PdJsfvCopUH+mh2L+Bxxszboj5m8.
ECDSA key fingerprint is MD5:51:94:f9:7e:16:ee:5b:b9:e6:f0:5b:57:bd:91:b2:e9.
Are you sure you want to continue connecting (yes/no)? yes
/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.100.130's password: 

Number of key(s) added: 1

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


四、在服务端git用户上创建一个空仓库,并授权

[root@git1 ~]# su - git
[git@git1 ~]$ mkdir project.git
[git@git1 ~]$ cd project.git/
[git@git1 project.git]$ git init --bare //初始化git仓库
Initialized empty Git repository in /home/git/project.git/
[git@git1 project.git]$ chown -R git:git .
[git@git1 project.git]$ ll ..
total 0
drwxrwxr-x 7 git git 119 Mar 18 00:29 project.git
[git@git1 project.git]$ ll .
total 12
drwxrwxr-x 2 git git   6 Mar 18 00:29 branches
-rw-rw-r-- 1 git git  66 Mar 18 00:29 config
-rw-rw-r-- 1 git git  73 Mar 18 00:29 description
-rw-rw-r-- 1 git git  23 Mar 18 00:29 HEAD
drwxrwxr-x 2 git git 242 Mar 18 00:29 hooks
drwxrwxr-x 2 git git  21 Mar 18 00:29 info
drwxrwxr-x 4 git git  30 Mar 18 00:29 objects
drwxrwxr-x 4 git git  31 Mar 18 00:29 refs

五、克隆
本地克隆

[root@git1 ~]# git clone file:///home/git/project.git/
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
[root@git1 ~]# ll
total 3104
-rw-------. 1 root root    1502 Nov  6 19:53 anaconda-ks.cfg
drwxr-xr-x  2 root root      94 Nov 19 15:21 elk
-rw-r--r--. 1 root root 3173245 Nov 13 15:11 lnmp-install.log
drwxr-xr-x  3 root root      18 Mar 18 00:35 project

远程克隆

[root@git2 ~]# git clone git@192.168.100.130:/home/git/project.git
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
[root@git2 ~]# ll
total 3104
-rw-------. 1 root root    1502 Nov  6 19:53 anaconda-ks.cfg
drwxr-xr-x  2 root root      94 Nov 19 15:21 elk
-rw-r--r--. 1 root root 3173245 Nov 13 15:11 lnmp-install.log
drwxr-xr-x  3 root root      18 Mar 18 00:38 project

六、修改git用户的shell为git-shell(使其无法使用shell登录)
方法一、

[root@git1 ~]# usermod git -s /usr/bin/git-shell

方法二、

[root@git1 ~]# vim /etc/passwd 	//修改最后一个字段

git:x:1002:1002::/home/git:/usr/bin/git-shell

七、测试发送到远程服务器

[root@git2 project]# echo 'test' > test.txt
[root@git2 project]# git add test.txt
[root@git2 project]# git commit -m  "test"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@git2.(none)')

此处会提示不知道我们的名字和邮箱,所以我们先设置然后再进行测试
八、修改git配置

[root@git2 project]# git config --global user.name "sqq" //设置名字
[root@git2 project]# git config --global user.email "1084373816@qq.com"  //设置邮箱

九、重新测试

[root@git2 project]# git remote add origin git@192.168.100.130:/home/git/project.git //查看是否本地与远程状态正常
fatal: remote origin already exists.
[root@git2 project]# git push origin master //发送到远程服务器
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.130:/home/git/project.git
 * [new branch]      master -> master
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值