GIT服务器搭建(转载)

简介: 一、GIT服务器的搭建1. 安装Gityum -y install git 2. 创建git用户adduser git3. 创建证书登陆收集所有客户端需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

一、GIT服务器的搭建

1. 安装Git

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">yum -y install git </pre>

2. 创建git用户

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">adduser git</pre>

3. 创建证书登陆

收集所有客户端需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

保证ssh不输入密码能连接到git用户

4. 初始化

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@app-01 opt]# git init --bare demo.git
Initialized empty Git repository in /opt/demo.git/</pre>

Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。

5. 把owner改为git

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">chown -R git:git demo.git/</pre>

6. 禁用shell登陆

 将/bin/bash改成

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@app-01 opt]# tail -1f /etc/passwd
git:x:1002:1006::/home/git:/usr/bin/git-shell</pre>

至此git服务器就搭建完成了。

二、GIT的基本使用

1. 克隆远程仓库

在客户端操作

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c DEV]$ git clone ssh://git@120.77.85.77:2121/opt/demo.git
Initialized empty Git repository in /root/DEV/demo/.git/ warning: You appear to have cloned an empty repository.
[root@wls12c DEV]$ ls
demo</pre>

2. 初始化客户端的工作环境

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ git config --global user.name "Scott Cho" [root@wls12c demo]$ git config --global user.email "root@wls12c.com" [root@wls12c demo]$ git config --global core.editor vim</pre>

3. 向Git本地仓库中提交一个新文件

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ echo "I successfully cloned the Git repository" > readme.txt
[root@wls12c demo]$ git add readme.txt #增加数据到暂存区
[root@wls12c demo]$ git status        #查看当前工作目录的状态

On branch master

Initial commit

Changes to be committed:

(use "git rm --cached <file>..." to unstage)

new file: readme.txt

[root@wls12c demo]$ git log
fatal: bad default revision 'HEAD' [root@wls12c demo]$ git commit -m "Clone the Git repository" [master (root-commit) b548d3b] Clone the Git repository 1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
[root@wls12c demo]$ git log
commit b548d3bd469cf5f183e9be9a3b2949f6361b5385
Author: Scott Cho root@wls12c.com Date: Mon Feb 27 17:42:29 2017 +0800 Clone the Git repository</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

** 常见技巧:**

**  查看当前文件内容与Git版本数据库中的差别:        **git diff readme.txt

**将当前工作目录内的所有文件都一起添加到暂存区域:   **git add .

创建忽略文件列表:                   .gitignore

文件被直接提交到Git数据库:              git commit -a -m "Modified again”

4 定义远程的Git服务器

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ git remote add server ssh://git@120.77.85.77:2121/opt/demo.git</pre>

5. 将文件提交到远程Git服务器

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ git push -u server master
Counting objects: 3, done.
Writing objects: 100% (3/3), 262 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@120.77.85.77:2121/opt/demo.git

  • [new branch] master -> master
    Branch master set up to track remote branch master from server.</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

6. 移除数据

** 保留工作区的文件,删除暂存区的文件: **git rm --cache test.java

从Git暂存区和工作目录中一起删除:   git rm -f test.java

删除Git版本仓库内的文件快照:    git rm test.java

7. 重命名文件

git mv 1.txt 2.txt

git commit -m "changed name"

8. 还原数据

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ echo "Git is a version control system" >> readme.txt
[root@wls12c demo]$ git add .
[root@wls12c demo]$ git commit -m "test rollback" [master e34168a] test rollback 1 files changed, 1 insertions(+), 0 deletions(-)
[root@wls12c demo]$ git log --pretty=oneline
e34168afeff0e1b1462eb6a52db4bd207d384332 test rollback
2947448a1d50b8a2613f93c54db31db797e65a5c changed name
458eacab758f72ecb4f63524caa301b4c440d702 ADD 1.txt agin
7c1464fa93af1eed88eaa76b15a663bae84cb78a ADD 1.txt agin
361b54468b1d9e4cbd271ce2be196d556ee84556 ADD 1.txt
b548d3bd469cf5f183e9be9a3b2949f6361b5385 Clone the Git repository
[root@wls12c demo]$ git reset --hard 29474 HEAD is now at 2947448 changed name [root@wls12c demo]$ cat readme.txt
I successfully cloned the Git repository</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

用git reflog命令来查看所有的历史记录,这样就可以看见还原以前的记录了。

我们突然发现不应该写一句话的,可以手工删除(当内容比较多的时候会很麻烦),还可以将文件内容从暂存区中恢复或者版本库中恢复:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ echo "xxxxx" >>readme.txt
[root@wls12c demo]$ git checkout -- readme.txt
[root@wls12c demo]$ cat readme.txt
I successfully cloned the Git repository
Git is a version control system</pre>

checkou规则是如果暂存区中有该文件,则直接从暂存区恢复,如果暂存区没有该文件,则将还原成最近一次文件提交时的快照。

** 9. 管理标签**

打标签 tag v1.0

git tag v1.1 -m "version 1.1 released" d316fb

查看所有的已有标签:

git tag

查看此标签的详细信息:

git show v1.0

10. 创建分支

  创建分支:   git branch dev1.0

** 查看分支:   git branch**

切换分支:   git checkout dev1.0

  删除分支:   **git branch -d ** dev1.0

  合并分支:   git merge dev1.0

目录
相关文章
|
存储 Java 开发工具
WinServer服务器上搭建Git代码库
本文介绍如何在WinServer服务器上搭建Git代码库。
609 0
|
6月前
|
Shell Linux 网络安全
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
1109 3
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
|
存储 开发工具 git
[Git] 深入理解 Git 的客户端与服务器角色
Git 的核心设计理念是分布式,每个仓库既可以是客户端也可以是服务器。通过 GitHub 远程仓库和本地仓库的协作,Git 实现了高效的版本管理和代码协作。GitHub 作为远程裸仓库,存储项目的完整版本历史并支持多客户端协作;本地仓库则通过 `.git` 文件夹独立管理版本历史,可在离线状态下进行提交、回滚等操作,并通过 `git pull` 和 `git push` 与远程仓库同步。这种分布式特性使得 Git 在代码协作中具备强大的灵活性和可靠性。
[Git] 深入理解 Git 的客户端与服务器角色
|
网络安全 Apache 开发工具
图解Git——服务器上的Git《Pro Git》
Git 远程仓库及通信协议简介:远程仓库为团队协作提供平台,支持共享代码。常见形式为裸仓库,仅保存 Git 元数据。Git 支持多种协议,包括本地协议(适合局域网)、HTTP/HTTPS(推荐智能 HTTP,安全易用)、SSH(企业内部协作首选)和 Git 协议(高效只读访问)。选择协议需根据协作需求、安全性和配置难度权衡。此外,搭建 Git 服务器涉及创建裸仓库、上传至服务器、初始化共享仓库等步骤。生成 SSH 公钥、配置服务器及使用 GitWeb 或 GitLab 等工具可进一步增强功能。第三方托管服务如 GitHub 提供便捷的托管选项,适合快速启动和开源项目。总结而言,自行运行服务器提
714 11
|
Linux 开发工具 数据安全/隐私保护
搭建 Git 私人服务器完整指南
本文详细介绍了如何从零开始搭建一个私人的 `Git` 服务器。首先,文章解释了 `Git` 的概念及其优势,并阐述了搭建私人 `Git` 服务器的重要性,包括数据隐私、定制化需求及成本效益。接着,文章分步骤指导读者完成服务器的准备工作,包括操作系统、硬件和网络要求。随后,详细介绍了在不同操作系统上安装 `Git` 的方法,并演示了如何创建 `git` 用户、部署仓库以及设置免密登录。此外,还提供了客户端连接远程仓库的具体步骤,包括 Linux 和 Windows 的操作方式。最后,文章探讨了迁移仓库的方法以及搭建 `Git` 服务器的一些进阶选项。
6433 0
搭建 Git 私人服务器完整指南
|
开发工具 git
服务器定时自动拉取Git仓库代码自动部署
服务器定时自动拉取Git仓库代码自动部署
680 0
|
前端开发 开发工具 git
如何清理 docker 磁盘空间+修改 Gitea 服务器的 Webhook 设置+前端一些好学好用的代码规范-git hook+husky + commitlint
如何清理 docker 磁盘空间+修改 Gitea 服务器的 Webhook 设置+前端一些好学好用的代码规范-git hook+husky + commitlint
314 5
|
开发工具 git iOS开发
服务器配置Huggingface并git clone模型和文件
该博客提供了在服务器上配置Huggingface、安装必要的工具(如git-lfs和huggingface_hub库)、登录Huggingface以及使用git clone命令克隆模型和文件的详细步骤。
2642 1
|
Shell 网络安全 开发工具
git实现服务器自动push拉取代码--webhooks
git实现服务器自动push拉取代码--webhooks
1507 1
|
开发工具 git
git怎么设置http代理服务器
git怎么设置http代理服务器
976 12