当前位置:   article > 正文

在Ubuntu服务器搭建Git仓库,以及Git使用基本流程_ubuntu git

ubuntu git

本文介绍了如何在Ubuntu服务器搭建简易的Git仓库,适用于小型团队的代码寄存,管理以及版本控制,介绍了在vscode中使用集成Git工具以及Git Graph插件。
在这里插入图片描述

一、在Ubuntu服务器中创建新用户,专用于搭建Git仓库

  • 登录任何能连接上局域网的Ubuntu服务器,最好是24小时开机运行,比如我的服务器ip为192.168.5.5
(base) PS C:\Users\29116\Desktop> ssh jetson@192.168.5.5
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 4.9.337-tegra aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.

Expanded Security Maintenance for Infrastructure is not enabled.

1 update can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

176 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

Last login: Sat May 11 15:08:30 2024 from 192.168.5.20
jetson@jetson-desktop:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 添加新的用户,sudo adduser git,输入用户密码,再次输入,然后一直回车
jetson@jetson-desktop:~$ sudo adduser git
Adding user `git' ...
Adding new group `git' (1001) ...
Adding new user `git' (1001) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]
Adding new user `git' to extra groups ...
Adding user `git' to group `audio' ...
Adding user `git' to group `gdm' ...
Adding user `git' to group `gpio' ...
Adding user `git' to group `i2c' ...
Adding user `git' to group `lightdm' ...
Adding user `git' to group `video' ...
Adding user `git' to group `weston-launch' ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 添加用户到sudo组sudo adduser git sudo,否则使用不了sudo命令
jetson@jetson-desktop:~$ sudo adduser git sudo
Adding user `git' to group `sudo' ...
Adding user git to group sudo
Done.
  • 1
  • 2
  • 3
  • 4
  • 检查是否添加成功,home目录新增git即为成功
jetson@jetson-desktop:~$ ls /home
git  jetson
  • 1
  • 2
  • 安装gitopenssh-serveropenssh-client
sudo apt update
sudo apt install git openssh-server openssh-client
  • 1
  • 2
  • 验证
git --version
sudo systemctl status ssh 
sudo systemctl enable ssh
  • 1
  • 2
  • 3
  • 以git账户远程登陆服务器
ssh git@192.168.5.5
  • 1
  • 创建ssh密钥对
cd ~
ssh-keygen -t rsa 		# 之后一直回车
  • 1
  • 2

在这里插入图片描述

  • 在客户端也按同样的指令生成密钥对
cd ~
ssh-keygen -t rsa
cd .ssh
cat id_rsa.pub		# 复制这个公钥,之后要用到
  • 1
  • 2
  • 3
  • 4
  • 将客户端公钥复制到服务器~/.ssh/authorized_keys里面
git@jetson-desktop:~$ cd .ssh
git@jetson-desktop:~/.ssh$ ls
id_rsa  id_rsa.pub
git@jetson-desktop:~/.ssh$ touch authorized_keys
git@jetson-desktop:~/.ssh$ chmod 600 authorized_keys
git@jetson-desktop:~/.ssh$ echo > "替换为客户端的公钥" authorized_keys 
git@jetson-desktop:~/.ssh$ sudo vim /etc/ssh/sshd_config
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 的 #号都去掉,Esc,:+wq保存

  • 在合适的位置创建仓库,只要有个目录就行,记住路径,之后客户端克隆要用
cd ~
mkdir ADAS.git
cd ADAS.git
  • 1
  • 2
  • 3
  • 初始化仓库,--bare代表仓库是裸的,方便之后上传文件
git init --bare
  • 1
  • 查看目录下的文件
git@jetson-desktop:~$ ls ~/ADAS.git/
branches  config  description  HEAD  hooks  info  objects  refs
  • 1
  • 2

这个目录是一个 Git 仓库的裸版本(bare repository),通常用于作为远程仓库或共享仓库,不包含工作目录,只包含 Git 版本库的核心内容。
以下是这些目录和文件的简要说明:
branches/: 存放分支的目录,每个分支对应一个文件。
config: Git 仓库的配置文件,包含仓库的配置信息。
description: 仓库的描述文件,通常为空文件。
HEAD: 指示当前所在分支或提交的文件。
hooks/: 存放 Git 钩子脚本的目录,可以在特定事件触发时执行自定义操作。
info/: 存放仓库的一些额外信息和配置文件。
objects/: 存放 Git 版本库中所有的对象(commits、trees、blobs)。
refs/: 存放分支和标签的引用(references)。
这个目录结构是一个典型的 Git 裸仓库的组成部分。裸仓库通常用于远程仓库,供多个开发者协作使用,或者用作备份和共享中心。

二、Ubuntu客户端Git安装、配置

  • 安装git
sudo apt update
sudo apt install git
  • 1
  • 2
  • 验证是否安装成功
git --version
  • 1
  • 配置user.nameuser.email,可以不填真实姓名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
  • 1
  • 2

比如:

git config --global user.name "xiaolan"
git config --global user.email "xiaolan@qq.com"
  • 1
  • 2
  • 克隆远程仓库,第一次会提示克隆了空的仓库
git clone git@192.168.5.5:~/ADAS.git
  • 1
  • 添加代码库第一个文件,新建README.md
cd ADAS
touch README.md
echo "hello git!" > README.md
  • 1
  • 2
  • 3

写好代码后需要提交,下面介绍git提交代码的流程:
在这里插入图片描述

  • 查看工作目录状态,这里显示未跟踪,因为还没有暂存
xiaolan@ubuntu:~/Documents/ADAS$ git status
On branch master

Initial commit

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

        README.md

nothing added to commit but untracked files present (use "git add" to track)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 添加到暂存区,这时候显示new file了
xiaolan@ubuntu:~/Documents/ADAS$ git add README.md 
xiaolan@ubuntu:~/Documents/ADAS$ git status
On branch master

Initial commit

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

        new file:   README.md
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 提交到版本库
xiaolan@ubuntu:~/Documents/ADAS$ git commit -m "新增了README.md,走出了坚实的一大步"
[master (root-commit) ce4ec07] 新增了README.md,走出了坚实的一大步
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
xiaolan@ubuntu:~/Documents/ADAS$ git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
nothing to commit, working directory clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里有一个报错(你们不一定有):提示 Your branch is based on ‘origin/master’, but the upstream is gone.,意思是你当前的分支与远程仓库的 origin/master 分支关联已经丢失。
使用以下命令来清除分支的上游关联:
git branch --unset-upstream
将本地的 master 分支与远程仓库重新关联起来,可以使用:
git push --set-upstream origin master

  • 解决报错
xiaolan@ubuntu:~/Documents/ADAS$ git branch --unset-upstream
xiaolan@ubuntu:~/Documents/ADAS$ git push --set-upstream origin master
git@192.168.5.5's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.5.5:~/ADAS.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 查看提交历史记录
xiaolan@ubuntu:~/Documents/ADAS$ git log
commit ce4ec0785102d1dd9dbb48c2678d67e8b3dd41c2
Author: xiaolan <xiaolan@qq.com>
Date:   Sat May 11 16:37:51 2024 +0800

    新增了README.md,走出了坚实的一大步
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 推送到远程仓库(可选)
xiaolan@ubuntu:~/Documents/ADAS$ git push origin master
git@192.168.5.5's password: 
Everything up-to-date
  • 1
  • 2
  • 3

这里出现了branch,即分支,观察 git 官方logo,也有个分支的形状,branch是 git 能够多人协作的基本机制。
git官网logo

就像多元宇宙的世界线一样,在你新建分支的时候,你便拥有了自己的世界线。
你可以选择自己的工作内容编写,然后暂存,提交到版本库,等到需要整合大家的工作代码时,便发生合并,统一为一个版本。

当然这里面会出现许多冲突,比如多个人修改了同一段代码,但是合并的分支只需要一个版本,这时候就需要审核员判断并手动解决。所以在工作之前先把当前最新的工作代码拉取到本地对照,保持本地与远程仓库的同步,将是个好习惯。

  • 查看当前分支
xiaolan@ubuntu:~/Documents/ADAS$ git branch
* master
  • 1
  • 2
  • 新建分支并切换到该分支
xiaolan@ubuntu:~/Documents/ADAS$ git checkout -b xiaolan
Switched to a new branch 'xiaolan'
xiaolan@ubuntu:~/Documents/ADAS$ git branch #你会看到带有 * 符号的分支名称是新创建的 xiaolan
  master
* xiaolan
  • 1
  • 2
  • 3
  • 4
  • 5
  • 现在你可以在新分支 xiaolan 上进行代码修改、提交等操作。
  • 切换回主分支(可选)
xiaolan@ubuntu:~/Documents/ADAS$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
xiaolan@ubuntu:~/Documents/ADAS$ git branch
* master
  xiaolan
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 使用xiaolan分支,修改,暂存,提交,推送代码
xiaolan@ubuntu:~/Documents/ADAS$ git checkout xiaolan
Switched to branch 'xiaolan'
xiaolan@ubuntu:~/Documents/ADAS$ echo "hello, i am xiaolan!" > README.md 
xiaolan@ubuntu:~/Documents/ADAS$ git add README.md 
xiaolan@ubuntu:~/Documents/ADAS$ git commit -m "README.md modified by xiaolan"
[xiaolan 8277300] README.md modified by xiaolan
 1 file changed, 1 insertion(+), 1 deletion(-)
xiaolan@ubuntu:~/Documents/ADAS$ git push origin xiaolan 
git@192.168.5.5's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.5.5:~/ADAS.git
 * [new branch]      xiaolan -> xiaolan
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 查看提交历史记录
xiaolan@ubuntu:~/Documents/ADAS$ git log
commit 827730004dfd675af9391fa191272ad6c08309b4
Author: xiaolan <xiaolan@qq.com>
Date:   Sat May 11 17:16:29 2024 +0800

    README.md modified by xiaolan

commit ce4ec0785102d1dd9dbb48c2678d67e8b3dd41c2
Author: xiaolan <xiaolan@qq.com>
Date:   Sat May 11 16:37:51 2024 +0800

    新增了README.md,走出了坚实的一大步
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 合并分支,为了效果更明显,我们先进入master分支,改变README.md的第一行和第三行内容,来人为制造冲突
xiaolan@ubuntu:~/Documents/ADAS$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
xiaolan@ubuntu:~/Documents/ADAS$ vim README.md 
xiaolan@ubuntu:~/Documents/ADAS$ cat README.md 
hello git! <conflict1>

<conflict2>
xiaolan@ubuntu:~/Documents/ADAS$ git add README.md 
xiaolan@ubuntu:~/Documents/ADAS$ git commit -m "新增了conflict"
[master dfd6f91] 新增了conflict
 1 file changed, 3 insertions(+), 1 deletion(-)
xiaolan@ubuntu:~/Documents/ADAS$ git push origin master 
git@192.168.5.5's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.5.5:~/ADAS.git
   ce4ec07..dfd6f91  master -> master
xiaolan@ubuntu:~/Documents/ADAS$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

合并分支的步骤
在这里插入图片描述

  • 合并xiaolan分支到master分支需要切换到master分支
xiaolan@ubuntu:~/Documents/ADAS$ git branch
* master
  xiaolan
  • 1
  • 2
  • 3
  • 使用git merge命令合并
xiaolan@ubuntu:~/Documents/ADAS$ git merge xiaolan
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
  • 1
  • 2
  • 3
  • 4
  • 出现冲突
xiaolan@ubuntu:~/Documents/ADAS$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 查看冲突
xiaolan@ubuntu:~/Documents/ADAS$ cat README.md 
<<<<<<< HEAD
hello git! <conflict1>

<conflict2>
=======
hello, i am xiaolan!
>>>>>>> xiaolan
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

<<<<<<< HEAD 表示当前分支(通常是目标分支,如 master)的内容。
======= 是分隔符,用于分隔两个不同分支的内容。
>>>>>>> branch_name 表示要合并的分支(如 xiaolan)的内容。

  • 手动编辑冲突文件,选择保留或修改其中的内容,以解决冲突。删除冲突标记 <<<<<<<、=======、>>>>>>> 并保留正确的代码。
xiaolan@ubuntu:~/Documents/ADAS$ vim README.md 
xiaolan@ubuntu:~/Documents/ADAS$ cat README.md 
hello git! <conflict1>
hello, i am xiaolan!
<conflict2>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 暂存,提交,推送解决冲突后的代码
xiaolan@ubuntu:~/Documents/ADAS$ git add README.md 
xiaolan@ubuntu:~/Documents/ADAS$ git commit -m "解决了conflict"
[master f06d9bb] 解决了conflict
xiaolan@ubuntu:~/Documents/ADAS$ git push origin master 
git@192.168.5.5's password: 
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 325 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.5.5:~/ADAS.git
   dfd6f91..f06d9bb  master -> master
xiaolan@ubuntu:~/Documents/ADAS$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 到此便合并成功

三、vscode使用git协作

想必了解了这么多关于git的用法,大家还是会觉得使用命令行过于繁琐,尤其是涉及到的文件很多时,解决冲突会变成一场灾难,所以这里介绍使用vscode自带的gui来轻松完成上面复杂的工作。

  • vscode集成git

进入vscode,左边工具栏自带git工具,里面有很多选项,见下图:
在这里插入图片描述
选项栏内容
在这里插入图片描述
工程目录下文件改动的标记
在这里插入图片描述在这里插入图片描述

  • 使用vscode进行协作

  • 创建并更改分支,创建时输入分支名,如xiaohong,等同于git checkout -b xiaohong
    在这里插入图片描述
    在这里插入图片描述

  • 暂存改动后的文件,等同于git add README.md
    在这里插入图片描述

    在这里插入图片描述

  • 输入提交信息,等同于git commit -m "修改了 README.md"
    在这里插入图片描述

  • 点击同步更新,等同于git push origin xiaohong
    在这里插入图片描述

  • 拉取master版本,同步更改(推送代码前最好先拉取主分支)
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 合并分支,等同于’git checkout master’加’git merge xiaohong’
    在这里插入图片描述
    在这里插入图片描述

  • 出现合并冲突
    在这里插入图片描述

  • 手动修改解决冲突
    在这里插入图片描述在这里插入图片描述* 我这里保留双方修改
    在这里插入图片描述

  • 然后又是,暂存,提交,同步更改

  • 到此,合并完成

四、Git Graph插件

  • 点击vscode左侧插件工具栏,搜索Git Graph安装
    在这里插入图片描述

  • 回到git管理,多出一个Git Graph图标,点击
    在这里插入图片描述

  • 哇哦,多么美妙的图形,将分支非常直观地展现出来了,可以发现:

  1. 每个节点都是commit提交的记录
  2. 当发布新的分支时,主分支便会分出一条支线,合并分支时,便会回到主分支
    在这里插入图片描述
  • 不只如此,点击各个节点还能看到详细信息
    在这里插入图片描述

  • 右键点击右侧的文件
    在这里插入图片描述
    View Diff:查看本次commit提交的文件和提交前的文件的对比
    在这里插入图片描述
    View File at this Revision:查看本次修改的文件
    在这里插入图片描述
    View Diff with Working File:查看本次commit提交的文件和工作区的文件的对比
    在这里插入图片描述

五、总结

  • 介绍了如何在Ubuntu服务器搭建Git仓库
  • 介绍了如何使用Git命令
  • 介绍了如何使用VsCode集成的Git工具
  • 介绍了实用的Git Graph插件
  • 使用Git协作来提高效率的例子
    在这里插入图片描述

参考资料:
基于Ubuntu环境Git服务器搭建及使用
Git 基本命令汇总
GIT常用命令大全——赶紧收藏
手把手教你在VSCode中使用Git
Git版本控制:提升开发效率的利器

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/689856
推荐阅读
相关标签
  

闽ICP备14008679号