基于Ubuntu环境Git服务器搭建及使用

基于Ubuntu环境Git服务器搭建及使用


Chapter1 搭建本地git服务器及详细操作步骤

原文链接:https://blog.csdn.net/weixin_45805339/article/details/133318082

1.搭建本地git服务器

1.1 环境

服务器:Ubuntu18.04

客户端:windows10_x64、Ubuntu20.04

无论是服务端还是客户端都需要先安装git

Ubuntu安装方法:

bash 复制代码
sudo apt update
sudo apt install git

1.2 服务端配置

本地git仓库依赖OpenSSH进行数据传输,先检查时候已经安装OpenSSH:

bash 复制代码
kyland@kyland-u-wuhao:~$ dpkg --list | grep ssh
ii  openssh-client                                                     1:7.6p1-4ubuntu0.7                              amd64        secure shell (SSH) client, for secure access to remote machines
ii  openssh-server                                                     1:7.6p1-4ubuntu0.7                              amd64        secure shell (SSH) server, for secure access from remote machines

如上已经安装了OpenSSH,如果没有安装需要执行一下命令:

bash 复制代码
sudo apt install -y openssh-server openssh-client

查看ssh状态:

bash 复制代码
sudo systemctl status ssh 
#如果没有启动,手动启动ssh服务
sudo systemctl enable ssh

1.3 创建git专属用户

在服务器终端输入如下命令,输入两次密码后(比如密码也是:git),一路回车就创建好了

bash 复制代码
kyland@kyland-u-wuhao:~$ sudo adduser git
[sudo] kyland 的密码: 
正在添加用户"git"...
正在添加新组"git" (1001)...
正在添加新用户"git" (1001) 到组"git"...
创建主目录"/home/git"...
正在从"/etc/skel"复制文件...
输入新的 UNIX 密码: 
重新输入新的 UNIX 密码: 
passwd:已成功更新密码
正在改变 git 的用户信息
请输入新值,或直接敲回车键以使用默认值
	全名 []: 
	房间号码 []: 
	工作电话 []: 
	家庭电话 []: 
	其它 []: 
这些信息是否正确? [Y/n] 

1.4 创建git仓库

bash 复制代码
cd /home/git #进入git用户家目录
mkdir private_code_repository #创建本地私有git仓库目录
git@kyland-u-wuhao:~$ git init --bare RosProject.git #创建本地私有git仓库RosProject.git
已初始化空的 Git 仓库于 /home/git/RosProject.git/

1.5 配置免密登录基础

bash 复制代码
mkdir -p /home/git/.ssh/ #在git主目录下先创建一个.ssh目录以便后面使用

2.客户端拉取推送代码

2.1客户端创建ssh公钥

bash 复制代码
ssh-keygen -t rsa #终端输入一路回车
Generating public/private rsa key pair.
Enter file in which to save the key (/home/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/Administrator/.ssh/id_rsa
Your public key has been saved in /home/Administrator/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:DyX41KiKahfSewidYUcINWBEDXfMuLRnuICwNMm16fQ Administrator@MS-TOVQOVTJPKXR
The key's randomart image is:
+---[RSA 3072]----+
|+BO==.           |
|o=.==+ . o       |
|+.o+= . + o      |
|o.o*.+ + o       |
|  =.BE. S        |
| o B .   o       |
|  + =     .      |
| o + .           |
|o . .            |
+----[SHA256]-----+
#在家目录.ssh下生成:id_rsa(私钥)、id_rsa.pub(公钥)两个密钥

原文链接:https://blog.csdn.net/weixin_56546908/article/details/124600622
设置用户名和邮箱

使用命令git config --global user.name "名称"设置用户名

使用命令git config --global user.email "邮箱"设置邮箱

查看设置的用户名和邮箱

使用命令git config --global --list

生成密钥信息

输入命令ssh-keygen -t rsa -C"youremail"

之后一直回车即可

youremail:你自己的邮箱账号 。

补充:-t:指定要创建的密钥的类型。rsa:一种加密算法。-C:添加注释

红色框框内是生成的公钥和私钥的位置

查看创建好的公钥

可使用gedit或其他工具查看生成的公钥,

以下用gedit: gedit /home/ubuntutest/.ssh/id_rsa.pub

此公钥将用gitee中SSH公钥的配置
打开gitee--->设置--->SSH公钥

bash 复制代码
sudo chown git.git rsa.pub 
bash 复制代码
git@ubuntu:~$ sudo cat >> .ssh/authorized_keys < rsa.pub 

git@ubuntu:~$ vim .ssh/authorized_keys
bash 复制代码
Administrator@PC-xxx MINGW64 /d/123/testgit (master)
$ git clone git@192.168.1.101:/home/git/mytest/mytest.git
Cloning into 'mytest'...
git@192.168.1.101's password:
warning: You appear to have cloned an empty repository.

2.2 免密配置

将客户端公钥id_rsa.pub内容复制到服务端.ssh/下新建文件authorized_keys里,这样我们在拉取推送代码时,就不需要输入密码了。

3.仓库使用(拉取及推送代码分支)

3.1拉取仓库分支

第一次我们需要拉取在服务端创建的仓库:

bash 复制代码
git clone git@192.168.0.83:/home/git/private_code_repository/RosProject.git
cd RosProject #进入工程
Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ ls -lah #查看工程内容
total 4.0K
drwxr-xr-x 1 Administrator 197121 0 Sep 26 15:48 ./
drwxr-xr-x 1 Administrator 197121 0 Sep 26 15:45 ../
drwxr-xr-x 1 Administrator 197121 0 Sep 26 17:17 .git/
-rw-r--r-- 1 Administrator 197121 0 Sep 26 15:48 .gitignore

3.2推送代码分支

每次我们更改一个功能的时候都需要创建一个分支,如:

bash 复制代码
$ git checkout -b ros2_230926 #创建本地分支
Switched to a new branch 'ros2_230926'

更改或者添加代码文件之后要提交代码分支到远程仓库

bash 复制代码
Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ touch .gitignore #添加.gitignore文件

Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git status #查看当前分支状态
On branch ros2_230926

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore

nothing added to commit but untracked files present (use "git add" to track)

Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git add . #将文件添加到git仓库缓存区(暂存区)

Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git status
On branch ros2_230926

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore


Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git commit -m'添加.gitignore'  #将某些已被跟踪的文件提交到版本库(包含工作区和版本库) 
#因为我们没设置git自身配置文件所以需要进行一下操作
Author identity unknown

*** 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 'Administrator@MS-TOVQOVTJPKXR.(
none)')

Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git config --global user.email "15840235191@163.com"

Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git config --global user.name "wuhao"

Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git commit -m'添加.gitignore' #提交到本地仓库成功
[ros2_230926 (root-commit) 0748e6c] 添加.gitignore
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
Administrator@MS-TOVQOVTJPKXR MINGW64 /d/git_pro/RosProject (ros2_230926)
$ git push -u origin ros2_230926 #在远程仓库创建该分支并且同步本地分支及远程分支
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 221 bytes | 221.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 192.168.0.83:/home/git/private_code_repository/RosProject.git
 * [new branch]      ros2_230926 -> ros2_230926
branch 'ros2_230926' set up to track 'origin/ros2_230926'.

以上就完成了代码分支的推送

4.常用git指令

bash 复制代码
git checkout -b [分支名称] #创建本地分支
git status               #查看本地分支状态
git add .                #将文件添加到git仓库缓存区(暂存区)
git commit -m'[说明]'		#将某些已被跟踪的文件提交到版本库(包含工作区和版本库) 
git diff [修改文件路径]    #查看修改文件详细内容
git reset [分支号]			#本地仓库切换到该分支上,修改的代码保留
git reset --hard [分支号]			#本地仓库切换到该分支上,修改的代码不保留
git rebase [分支名]        #合并该分支到当前分支
git log                  #查看本地操作信息
相关推荐
XY.散人8 分钟前
初识Linux · 文件(1)
linux·运维·服务器
不惑_2 小时前
在 Ubuntu 安装 Python3.7(没有弯路)
linux·运维·ubuntu
枫叶丹45 小时前
【在Linux世界中追寻伟大的One Piece】进程信号
linux·运维·服务器
刻词梨木5 小时前
ubuntu中挂载点内存不足,分配不合理后使用软链接的注意事项
linux·运维·ubuntu
灯火不休ᝰ6 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
数云界9 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
powerfulzyh10 小时前
Ubuntu24.04远程开机
linux·ubuntu·远程工作
ulimpid10 小时前
Command | Ubuntu 个别实用命令记录(新建用户、查看网速等)
linux·ubuntu·command
HHoao10 小时前
Ubuntu启动后第一次需要很久才能启动GTK应用问题
linux·运维·ubuntu
小灰兔的小白兔10 小时前
【Ubuntu】Ubuntu常用命令
linux·运维·ubuntu