本文介绍了如何在Ubuntu服务器搭建简易的Git仓库,适用于小型团队的代码寄存,管理以及版本控制,介绍了在vscode中使用集成Git工具以及Git Graph插件。
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:~$ 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' ... 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. home目录新增git即为成功jetson@jetson-desktop:~$ ls /home git jetson git,openssh-server,openssh-clientsudo apt update sudo apt install git openssh-server openssh-client git --version sudo systemctl status ssh sudo systemctl enable ssh ssh git@192.168.5.5 cd ~ ssh-keygen -t rsa # 之后一直回车 
cd ~ ssh-keygen -t rsa cd .ssh cat id_rsa.pub # 复制这个公钥,之后要用到 ~/.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 将#PubkeyAuthentication yes#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 的 #号都去掉,Esc,:+wq保存
cd ~ mkdir ADAS.git cd ADAS.git --bare代表仓库是裸的,方便之后上传文件git init --bare git@jetson-desktop:~$ ls ~/ADAS.git/ branches config description HEAD hooks info objects refs 这个目录是一个 Git 仓库的裸版本(bare repository),通常用于作为远程仓库或共享仓库,不包含工作目录,只包含 Git 版本库的核心内容。
以下是这些目录和文件的简要说明:
branches/: 存放分支的目录,每个分支对应一个文件。
config: Git 仓库的配置文件,包含仓库的配置信息。
description: 仓库的描述文件,通常为空文件。
HEAD: 指示当前所在分支或提交的文件。
hooks/: 存放 Git 钩子脚本的目录,可以在特定事件触发时执行自定义操作。
info/: 存放仓库的一些额外信息和配置文件。
objects/: 存放 Git 版本库中所有的对象(commits、trees、blobs)。
refs/: 存放分支和标签的引用(references)。
这个目录结构是一个典型的 Git 裸仓库的组成部分。裸仓库通常用于远程仓库,供多个开发者协作使用,或者用作备份和共享中心。
sudo apt update sudo apt install git git --version user.name和user.email,可以不填真实姓名和邮箱git config --global user.name "Your Name" git config --global user.email "your.email@example.com" 比如:
git config --global user.name "xiaolan" git config --global user.email "xiaolan@qq.com" git clone git@192.168.5.5:~/ADAS.git cd ADAS touch README.md echo "hello git!" > README.md 写好代码后需要提交,下面介绍git提交代码的流程:
xiaolan@ubuntu:~/Documents/ADAS$ git status On branch master Initial commit Untracked files: (use "git add ..." to include in what will be committed) README.md nothing added to commit but untracked files present (use "git add" to track) 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 ..." to unstage) new file: README.md 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 这里有一个报错(你们不一定有):提示 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. xiaolan@ubuntu:~/Documents/ADAS$ git log commit ce4ec0785102d1dd9dbb48c2678d67e8b3dd41c2 Author: xiaolan Date: Sat May 11 16:37:51 2024 +0800 新增了README.md,走出了坚实的一大步 xiaolan@ubuntu:~/Documents/ADAS$ git push origin master git@192.168.5.5's password: Everything up-to-date 这里出现了
branch,即分支,观察 git 官方logo,也有个分支的形状,branch是 git 能够多人协作的基本机制。就像多元宇宙的世界线一样,在你新建分支的时候,你便拥有了自己的世界线。
你可以选择自己的工作内容编写,然后暂存,提交到版本库,等到需要整合大家的工作代码时,便发生合并,统一为一个版本。当然这里面会出现许多冲突,比如多个人修改了同一段代码,但是合并的分支只需要一个版本,这时候就需要审核员判断并手动解决。所以在工作之前先把当前最新的工作代码拉取到本地对照,保持本地与远程仓库的同步,将是个好习惯。
xiaolan@ubuntu:~/Documents/ADAS$ git branch * master xiaolan@ubuntu:~/Documents/ADAS$ git checkout -b xiaolan Switched to a new branch 'xiaolan' xiaolan@ubuntu:~/Documents/ADAS$ git branch #你会看到带有 * 符号的分支名称是新创建的 xiaolan master * 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 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 xiaolan@ubuntu:~/Documents/ADAS$ git log commit 827730004dfd675af9391fa191272ad6c08309b4 Author: xiaolan Date: Sat May 11 17:16:29 2024 +0800 README.md modified by xiaolan commit ce4ec0785102d1dd9dbb48c2678d67e8b3dd41c2 Author: xiaolan Date: Sat May 11 16:37:51 2024 +0800 新增了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! 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$ 合并分支的步骤
xiaolan@ubuntu:~/Documents/ADAS$ git branch * master xiaolan 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. 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 ..." to mark resolution) both modified: README.md no changes added to commit (use "git add" and/or "git commit -a") xiaolan@ubuntu:~/Documents/ADAS$ cat README.md <<<<<<< HEAD hello git! ======= hello, i am xiaolan! >>>>>>> xiaolan <<<<<<< HEAD 表示当前分支(通常是目标分支,如 master)的内容。
======= 是分隔符,用于分隔两个不同分支的内容。
>>>>>>> branch_name 表示要合并的分支(如 xiaolan)的内容。
xiaolan@ubuntu:~/Documents/ADAS$ vim README.md xiaolan@ubuntu:~/Documents/ADAS$ cat README.md hello git! hello, i am xiaolan! 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$ 想必了解了这么多关于git的用法,大家还是会觉得使用命令行过于繁琐,尤其是涉及到的文件很多时,解决冲突会变成一场灾难,所以这里介绍使用vscode自带的gui来轻松完成上面复杂的工作。
进入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’

出现合并冲突
手动修改解决冲突
* 我这里保留双方修改
然后又是,暂存,提交,同步更改
到此,合并完成
点击vscode左侧插件工具栏,搜索Git Graph安装
回到git管理,多出一个Git Graph图标,点击
哇哦,多么美妙的图形,将分支非常直观地展现出来了,可以发现:

不只如此,点击各个节点还能看到详细信息
右键点击右侧的文件
View Diff:查看本次commit提交的文件和提交前的文件的对比
View File at this Revision:查看本次修改的文件
View Diff with Working File:查看本次commit提交的文件和工作区的文件的对比

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