參考:
https://www.cnblogs.com/dee0912/p/5815267.html#_label0
https://blog.csdn.net/carfge/article/details/79691360
https://www.cnblogs.com/fly_dragon/p/8718614.html
1、環境准備
服務器:CentOS 7.3 + git (1.8.3.1)
客戶端:win10 + git (2.17.0.windows.1)
2、服務器安裝git
yum install -y git
3、創建git用戶,管理 git服務
[root@localhost home]# useradd git [root@localhost home]# passwd git
4、服務器創建git 倉庫
設置/home/git/repository-git 為git 服務器倉庫,然后把 git 倉庫的 owner 修改為 git 用戶。
[root@localhost git]# mkdir repository-git [root@localhost git]# git init --bare repository-git/ Initialized empty Git repository in /home/git/repository-gt/ [root@localhost git]# chown -R git:git repository-git/
5、客戶端安裝git
下載 Git for Windows,地址:https://git-for-windows.github.io/
安裝完之后,可以使用 Git Bash 作為命令行客戶端。
5.1、選擇一個目錄 F:\project\sell 作為本地倉庫,右鍵進入Git Bash 命令行模式
初始化本地倉庫:git init
5.2、嘗試克隆一個服務器的空倉庫到本地倉庫
git clone git@192.168.116.129:/home/git/repository-gt
第一次連接到目標 Git 服務器時會得到一個提示:
The authenticity of host '192.168.116.129(192.168.116.129)' can't be established. RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ. Are you sure you want to continue connecting (yes/no)?
選擇 yes:
Warning: Permanently added '192.168.116.129' (RSA) to the list of known hosts.
此時 C:\Users\用戶名\.ssh 下會多出一個文件 known_hosts,以后在這台電腦上再次連接目標 Git 服務器時不會再提示上面的語句。
6、客戶端創建公鑰和私鑰
客戶端git 第一次連接 服務器端的git倉庫時,需要創建公鑰和私鑰,並將公鑰發給服務器保存起來,才能正常訪問。
$ ssh-keygen -t rsa -C "xxxx100@163.com",郵箱隨便輸入即可。
此時 C:\Users\用戶名\.ssh 下會多出兩個文件 id_rsa 和 id_rsa.pub
id_rsa 是私鑰
id_rsa.pub 是公鑰
7、服務器 git 打開RSA 認證
進入 /etc/ssh 目錄,編輯sshd_config,查看以下三個配置是否被注釋,如被注釋,取消掉注釋:
RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
保存並重啟sshd服務:
servcie sshd restart
由 AuthorizedKeysFile 得知公鑰的存放路徑是 .ssh/authorized_keys,實際上是 $Home/.ssh/authorized_keys,由於管理 git 服務的用戶是 git,所以實際存放公鑰的路徑是 /home/git/.ssh/authorized_keys
在 /home/git/ 下創建目錄 .ssh
[root@localhost git]# pwd /home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla .ssh
然后把 .ssh 文件夾的 owner 修改為 git
[root@localhost git]# chown -R git:git .ssh [root@localhost git]# ll -a 總用量 32 drwx------. 5 git git 4096 8月 28 20:04 . drwxr-xr-x. 8 root root 4096 8月 28 19:32 .. -rw-r--r--. 1 git git 18 10月 16 2014 .bash_logout -rw-r--r--. 1 git git 176 10月 16 2014 .bash_profile -rw-r--r--. 1 git git 124 10月 16 2014 .bashrc drwxr-xr-x. 2 git git 4096 11月 12 2010 .gnome2 drwxr-xr-x. 4 git git 4096 5月 8 12:22 .mozilla drwxr-xr-x. 2 git git 4096 8月 28 20:08 .ssh
設置權限,此步驟不能省略,而且權限值也不要改,不然會報錯。
將客戶端公鑰導入服務器端 /home/git/.ssh/authorized_keys 文件
打開 C:\Users\user\.ssh 下 id_rsa.pub,將里面所有的內容拷貝到/home/git/.ssh/authorized_keys文件里。
8、客戶端與服務器倉庫關聯起來
git remote add origin git@192.168.116.129:/home/git/repository-git
將本地項目推送到遠程倉庫:
git add -A
git commit -m 'First push.'
git push -u origin master
git push -u origin master 如果報錯,則表明本地倉庫與遠程倉庫沖突,需要先拉取遠程倉庫到本地:
git pull --rebase origin master
現在我們有這樣的兩個分支,test和master,提交如下:
D---E test / A---B---C---F--- master
在master執行git merge test,然后會得到如下結果:
D--------E
/ \
A---B---C---F----G--- test, master
在master執行git rebase test,然后得到如下結果:
A---B---D---E---C‘---F‘--- test, master
merge 操作會生成一個新的節點,之前的提交分開顯示。
而rebase操作不會生成新的節點,是將兩個分支融合成一個線性的提交。
9、另起一個客戶端倉庫拉取遠程倉庫測試
首先初始化本地倉庫,其次將本地倉庫與遠程倉庫進行關聯,之后再拉取遠程倉庫的項目。
jinwe@DESKTOP-V4BF121 MINGW64 /e/project/sell2 $ git init Initialized empty Git repository in E:/project/sell2/.git/ jinwe@DESKTOP-V4BF121 MINGW64 /e/project/sell2 (master) $ git remote add origin git@192.168.116.129:/home/git/repository-git jinwe@DESKTOP-V4BF121 MINGW64 /e/project/sell2 (master) $ git fetch origin master git@192.168.116.129's password: remote: Counting objects: 26, done. remote: Compressing objects: 100% (17/17), done. remote: Total 26 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (26/26), done. From 192.168.116.129:/home/git/repository-git * branch master -> FETCH_HEAD * [new branch] master -> origin/master
在本地建立分支並切換到該分支,將遠程分支的內容拉取到本地。
jinwe@DESKTOP-V4BF121 MINGW64 /e/project/sell2 (master) $ git checkout -b master origin/master Already on 'master' Branch 'master' set up to track remote branch 'master' from 'origin'. jinwe@DESKTOP-V4BF121 MINGW64 /e/project/sell2 (master) $ git pull origin master git@192.168.116.129's password: remote: Counting objects: 17, done. remote: Compressing objects: 100% (5/5), done. remote: Total 9 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (9/9), done. From 192.168.116.129:/home/git/repository-git * branch master -> FETCH_HEAD 9a87e03..83b12cd master -> origin/master Updating 9a87e03..83b12cd Fast-forward src/test/java/com/latiny/sell/LoggerTest.java | 1 - 1 file changed, 1 deletion(-)
最后,回到本地文件夾E:\project\sell2 查看,已拉取遠程分支到本地啦!
10、額外知識
客戶端在使用ssh方式連接時,報錯。查看了下ssh使用的注意事項,記錄下來。
查看狀態:
service sshd status
service sshd restart
service sshd stop
ssh服務的網絡連接情況:
netstat -ntlp
netstat -ntlp|grep sshd
查看本地倉庫管理的遠程倉庫
git remote -v
修改遠程倉庫的關聯
比如,之前你關聯的遠程倉庫使用的協議是 http ,你想將關聯的遠程倉庫的 url 改為 ssh 協議的。
修改關聯的遠程倉庫的方法,主要有三種。
第一種:使用 git remote set-url 命令,更新遠程倉庫的 url
git remote set-url origin <newurl>
第二種:先刪除之前關聯的遠程倉庫,再來添加新的遠程倉庫關聯
# 刪除關聯的遠程倉庫
git remote rm <name>(遠程分支)
# 添加新的遠程倉庫關聯
git remote add <name> <url>
遠程倉庫的名稱推薦使用默認的名稱 origin 。
第三種:直接修改項目目錄下的 .git 目錄中的 config 配置文件。
11、常見的git 命令
git init //初始化本地git倉庫
git add<file> //添加文件
git add *.html //添加一類文件
git add . //添加所有文件
git status //查看狀態
git commit //提交
git commit -m ''提交並注釋 (這樣就可以不用進入到編輯頁面了)
git push //從遠程倉庫考取數據
git clone //從遠程倉庫拷貝數據
git config --global user.name'張宜成' 配置用戶名
git config -- global user.email 'chengchengzhang123@qq.com' 配置郵箱
git rm --cached 文件名 //從add工作區中刪除
git branch 分支名 (創建分支)
git checkout 分支名 (切換到你的分支中)
git checkout master(切換到主分支)
git merge 分支名(在master主分支下使用 將分支合並到主分支)
git remote 查看連接
git remote add origin 鏈接 (創建鏈接)
git remote remove 名稱(刪除remote)
git clone 將服務器端的項目克隆島本地倉庫
touch .gitignore 忽略需要上傳的文件(將要忽略的文件放到該文件夾)
touch.gitignore 忽略文件(/文件名)
README.md文檔 git對項目進行描述
$ git push origin master //推送到遠程倉庫
abc

