1.搭建本地git服務(wù)器
1.1環(huán)境
服務(wù)器:Ubuntu18.04
客戶端:windows10_x64、Ubuntu20.04
無論是服務(wù)端還是客戶端都需要先安裝git
Ubuntu安裝方法:
sudo apt update
sudo apt install git
windows安裝方法:
https://blog.csdn.net/WANGLI123956/article/details/131074567
1.2服務(wù)端配置
本地git倉庫依賴OpenSSH進行數(shù)據(jù)傳輸,先檢查時候已經(jīng)安裝OpenSSH:
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
如上已經(jīng)安裝了OpenSSH,如果沒有安裝需要執(zhí)行一下命令:
sudo apt install -y openssh-server openssh-client
查看ssh狀態(tài):
sudo systemctl status ssh
#如果沒有啟動,手動啟動ssh服務(wù)
sudo systemctl enable ssh
1.3創(chuàng)建git專屬用戶
在服務(wù)器終端輸入如下命令,輸入兩次密碼后,一路回車就創(chuàng)建好了
kyland@kyland-u-wuhao:~$ sudo adduser git
[sudo] kyland 的密碼:
正在添加用戶"git"...
正在添加新組"git" (1001)...
正在添加新用戶"git" (1001) 到組"git"...
創(chuàng)建主目錄"/home/git"...
正在從"/etc/skel"復(fù)制文件...
輸入新的 UNIX 密碼:
重新輸入新的 UNIX 密碼:
passwd:已成功更新密碼
正在改變 git 的用戶信息
請輸入新值,或直接敲回車鍵以使用默認值
全名 []:
房間號碼 []:
工作電話 []:
家庭電話 []:
其它 []:
這些信息是否正確? [Y/n]
1.4創(chuàng)建git倉庫
cd /home/git #進入git用戶家目錄
mkdir private_code_repository #創(chuàng)建本地私有g(shù)it倉庫目錄
git@kyland-u-wuhao:~$ git init --bare RosProject.git #創(chuàng)建本地私有g(shù)it倉庫RosProject.git
已初始化空的 Git 倉庫于 /home/git/RosProject.git/
1.5配置免密登錄基礎(chǔ)
mkdir -p /home/git/.ssh/ #在git主目錄下先創(chuàng)建一個.ssh目錄以便后面使用
2.客戶端拉取推送代碼
2.1客戶端創(chuàng)建ssh公鑰
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(公鑰)兩個密鑰
2.2免密配置
將客戶端公鑰id_rsa.pub內(nèi)容復(fù)制到服務(wù)端.ssh/下新建文件authorized_keys里,這樣我們在拉取推送代碼時,就不需要輸入密碼了。
3.倉庫使用(拉取及推送代碼分支)
3.1拉取倉庫分支
第一次我們需要拉取在服務(wù)端創(chuàng)建的倉庫:
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 #查看工程內(nèi)容
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推送代碼分支
每次我們更改一個功能的時候都需要創(chuàng)建一個分支,如:
$ git checkout -b ros2_230926 #創(chuàng)建本地分支
Switched to a new branch 'ros2_230926'
更改或者添加代碼文件之后要提交代碼分支到遠程倉庫文章來源:http://www.zghlxwxcb.cn/news/detail-788332.html
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 #查看當(dāng)前分支狀態(tài)
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倉庫緩存區(qū)(暫存區(qū))
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' #將某些已被跟蹤的文件提交到版本庫(包含工作區(qū)和版本庫)
#因為我們沒設(shè)置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 #在遠程倉庫創(chuàng)建該分支并且同步本地分支及遠程分支
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'.
以上就完成了代碼分支的推送文章來源地址http://www.zghlxwxcb.cn/news/detail-788332.html
4.常用git指令
git checkout -b [分支名稱] #創(chuàng)建本地分支
git status #查看本地分支狀態(tài)
git add . #將文件添加到git倉庫緩存區(qū)(暫存區(qū))
git commit -m'[說明]' #將某些已被跟蹤的文件提交到版本庫(包含工作區(qū)和版本庫)
git diff [修改文件路徑] #查看修改文件詳細內(nèi)容
git reset [分支號] #本地倉庫切換到該分支上,修改的代碼保留
git reset --hard [分支號] #本地倉庫切換到該分支上,修改的代碼不保留
git rebase [分支名] #合并該分支到當(dāng)前分支
git log #查看本地操作信息
到了這里,關(guān)于搭建本地git服務(wù)器及詳細操作步驟的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!