git和github的上傳項目

一:github入門

https://github.com/

1.1 登錄github官網

使用谷歌瀏覽器可以幫助翻譯成中文
在這裏插入圖片描述

1.2 註冊

在這裏插入圖片描述

1.3 點擊創建

在這裏插入圖片描述

選擇自己偏向方向

完成設定

在這裏插入圖片描述

驗證郵箱
在這裏插入圖片描述
在這裏插入圖片描述

點擊跳過
在這裏插入圖片描述

1.4 測試創建倉庫

在這裏插入圖片描述

此時便在新建倉庫內

在這裏插入圖片描述

二:瞭解一下git

hit說白了就是用來管理項目的

Git(讀音爲/gɪt/)是一個開源的分佈式版本控制系統,可以有效、高速地處理從很小到非常大的項目版本管理。

Git 是 Linus Torvalds爲了幫助管理 Linux 內核開發而開發的一個開放源碼的版本控制軟件。

Torvalds 開始着手開發 Git 是爲了作爲一種過渡方案來替代 BitKeeper

2.1 git特點

分佈式相比於集中式的最大區別在於開發者可以提交到本地,每個開發者通過克隆(git clone),在本地機器上拷貝一個完整的Git倉庫。

下圖是經典的git開發過程。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-mHby6fxD-1591197082841)(G:\GSY\Documents\typora圖片)]

2.2 Git的功能特性:

2.2.1 從一般開發者的角度來看,git有以下功能:

1、從服務器上克隆完整的Git倉庫(包括代碼和版本信息)到單機上。

2、在自己的機器上根據不同的開發目的,創建分支,修改代碼。

3、在單機上自己創建的分支上提交代碼。

4、在單機上合併分支。

5、把服務器上最新版的代碼fetch下來,然後跟自己的主分支合併。

6、生成補丁(patch),把補丁發送給主開發者。

7、看主開發者的反饋,如果主開發者發現兩個一般開發者之間有衝突(他們之間可以合作解決的衝突),就會要求他們先解決衝突,然後再由其中一個人提交。如果主開發者可以自己解決,或者沒有衝突,就通過。

8、一般開發者之間解決衝突的方法,開發者之間可以使用pull 命令解決衝突,解決完衝突之後再向主開發者提交補丁。

2.2.2 從主開發者的角度(假設主開發者不用開發代碼)看,git有以下功能:

1、查看郵件或者通過其它方式查看一般開發者的提交狀態。

2、打上補丁,解決衝突(可以自己解決,也可以要求開發者之間解決以後再重新提交,如果是開源項目,還要決定哪些補丁有用,哪些不用)。

3、向公共服務器提交結果,然後通知所有開發人員。

2.3 git優點:

  • 適合分佈式開發,強調個體。

  • 公共服務器壓力和數據量都不會太大。

  • 速度快、靈活。

  • 任意兩個開發者之間可以很容易的解決衝突。

  • 離線工作。

2.4 git缺點:

  • 資料少(起碼中文資料很少)。

  • 學習週期相對而言比較長。

  • 不符合常規思維。

  • 代碼保密性差,一旦開發者把整個庫克隆下來就可以完全公開所有代碼和版本信息。;p

2.5 小結

git是有一個項目管理服務,很多開發人員喜歡使用這個來實現代碼託管、融合,實現協同統一性的開發

可以實現環境統一,git可以實現權限管理,可以把代碼提交到git上

在使用git之前,都是使用ftp,架構師去提取出來,然後進行處理

git的這種分佈式相比於集中式的最大區別在於開發者可以提交到本地放到緩存區通過克隆在本地機器上拷貝一個完整的git倉庫

有點像掛載

使用push回去拉取本地提交的

git使用的是ssh協議方式

master是一個默認的分支,可以創建多個分支,代表多個項目

每個人都是server和client,本地從目標服務端拉取一個git,克隆到本地

將代碼提交到本地上,git會自動提交到github上

三:在自己的服務器上部署git

部署git服務器項目倉庫,先完成git部署

git服務器是搭建在內網的倉庫
source是開發者編寫代碼的服務器

3.1 修改主機名爲git,清空防火牆,關閉核心防護

[root@lamp ~]# hostnamectl set-hostname git
[root@lamp ~]# su
[root@git ~]# iptables -F
[root@git ~]# setenforce 0
[root@git ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

順便在簡單修改另一臺主機環境

[root@nginx ~]# hostnamectl set-hostname source
[root@nginx ~]# su
[root@source ~]# iptables -F
[root@source ~]# setenforce 0
[root@source ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
[root@source ~]# 

3.2 git服務器安裝git服務

[root@git ~]# yum install git -y

3.3 git服務器創建程序服務用戶git

[root@git ~]# useradd git
[root@git ~]# passwd git
Changing password for user git.
New password: 123123
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 123123
passwd: all authentication tokens updated successfully.

3.4 因爲服務默認提交以git身份去提交,使用git身份去創建倉庫/home/git/repos/

[root@git ~]# su git
[git@git root]$ cd ~
[git@git ~]$ pwd
/home/git
[git@git ~]$ mkdir repos
[git@git ~]$ ls
repos

3.5 倉庫爲repos,裏面可以再創建

[git@git repos]$ mkdir demo.git
[git@git repos]$ ls
demo.git
[git@git repos]$ cd demo.git/
[git@git demo.git]$ 

備註:後綴必須要git

3.6 倉庫初始化git init --bare

初始化生成文件

[git@git demo.git]$ git init --bare
Initialized empty Git repository in /home/git/repos/demo.git/
[git@git demo.git]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

3.7 git常用命令

最常用的 git 命令有:
   add        添加文件內容至索引
   bisect     通過二分查找定位引入 bug 的變更
   branch     列出、創建或刪除分支
   checkout   檢出一個分支或路徑到工作區
   clone      克隆一個版本庫到一個新目錄
   commit     記錄變更到版本庫
   diff       顯示提交之間、提交和工作區之間等的差異
   fetch      從另外一個版本庫下載對象和引用
   grep       輸出和模式匹配的行
   init       創建一個空的 Git 版本庫或重新初始化一個已存在的版本庫
   log        顯示提交日誌
   merge      合併兩個或更多開發歷史
   mv         移動或重命名一個文件、目錄或符號鏈接
   pull       獲取併合並另外的版本庫或一個本地分支
   push       更新遠程引用和相關的對象
   rebase     本地提交轉移至更新後的上游分支中
   reset      重置當前HEAD到指定狀態
   rm         從工作區和索引中刪除文件
   show       顯示各種類型的對象
   status     顯示工作區狀態
   tag        創建、列出、刪除或校驗一個GPG簽名的 tag 對象

3.8 此時demo.git就不再是文件夾了,變成了代碼倉庫

[git@git demo.git]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

對方(source)若是使用倉庫的話,克隆倉庫,需要配置ssh

3.9 配置主機之間的免密登錄

souce免密登錄git服務器

[root@source ~]# ssh-keygen -t rsa		//-t rsa 非堆成密鑰
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RADvFbl3IeI6MzfDraYwBuzeiozLGFhMTCpTPFcHwH0 root@source
The key's randomart image is:
+---[RSA 2048]----+
[root@source ~]# ls -a
.                .bashrc    .ICEauthority         .viminfo     桌面
..               .cache     initial-setup-ks.cfg  .Xauthority  模板
anaconda-ks.cfg  .config    .local                下載         視頻
.bash_history    .cshrc     .mozilla              公共         音樂
.bash_logout     .dbus      .ssh                  圖片
.bash_profile    .esd_auth  .tcshrc               文檔
[root@source ~]# cd ssh
bash: cd: ssh: No such file or directory
[root@source ~]# cd .ssh
[root@source .ssh]# ll
total 12
-rw-------. 1 root root 1675 May 31 13:29 id_rsa
-rw-r--r--. 1 root root  393 May 31 13:29 id_rsa.pub
-rw-r--r--. 1 root root  175 Dec 26 15:44 known_hosts

  • 然後把本地密鑰推送進去
[root@source .ssh]# ssh-copy-id [email protected]		
//也可以ssh-copy-id -i id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.206 (192.168.247.206)' can't be established.
ECDSA key fingerprint is SHA256:dXWxtS2ShXQgfb7R672V7+l3i7rGqHBbIB5MTcFnAws.
ECDSA key fingerprint is MD5:59:fb:20:f0:28:96:5e:14:90:82:63:c9:ae:67:d6:e9.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"and check to make sure that only the key(s) you wanted were added.
#ssh-agent bash  #免密登錄,這條指令也可以刷一下
#ssh-add 
[root@source .ssh]# ssh '[email protected]'	#連接測試
Last login: Sun May 31 13:20:30 2020
[git@git ~]$ exit
logout
Connection to 192.168.247.206 closed.
[root@source .ssh]# ssh 192.168.247.206		#若是不指定用戶git就是默認使用root登錄,需要輸入密碼
[email protected]'s password: 
Last login: Sun May 31 13:20:16 2020
[root@git ~]# exit
logout
Connection to 192.168.247.206 closed.
[root@source .ssh]# ssh [email protected]
Last login: Sun May 31 13:32:09 2020 from 192.168.247.160
[git@git ~]$ exit
logout
Connection to 192.168.247.206 closed.

3.10 開始克隆倉庫

[git@git demo.git]$ pwd
/home/git/repos/demo.git
  • 本地日常使用編寫代碼端也需要安裝git
[root@source .ssh]# yum install git -y
[root@source .ssh]# cd ~
[root@source ~]# git clone [email protected]:/home/git/repos/demo.git
Cloning into 'demo'...
warning: You appear to have cloned an empty repository.
警告:您似乎克隆了一個空版本庫
[root@source ~]# ls
anaconda-ks.cfg  demo
[root@source ~]# cd demo/
[root@source demo]# ls -a
.  ..  .git
[root@source demo]# cd .git/	#這個是隱藏目錄
[root@source .git]# ls	
branches  config  description  HEAD  hooks  info  objects  refs

3.11 拷貝一個項目進source來做演示

[root@source .git]# cd ..
[root@source demo]# pwd
/root/demo
[root@source demo]# cp -rp /abc/Apple/* /root/demo/	
[root@source demo]# ls
pom.xml  src  target

3.12 把項目提交到本地庫

add添加,commit是提交到本地庫

-m 指定名稱

[root@source demo]# git add *	#add,添加文件內容至緩存層
[root@source demo]# git commit -m "gsygsy"	#commit是提交到本地庫

*** Please tell me who you are.	
#請告訴我你是誰
Run
#運行一下命令驗證身份
  git config --global user.email "[email protected]"
  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 'root@source.(none)')

請告訴我你是誰

運行下面中的指令

3. 13 設置用戶郵箱

[root@source demo]# git config --global user.email "[email protected]"
[root@source demo]# git commit -m "gsygsy"
[master (root-commit) f8063bc] gsygsy
 14 files changed, 122 insertions(+)

此時收到了郵件,不過是英文,看不懂
在這裏插入圖片描述

3.14 這個是本地提交,接下來遠程提交remote到git服務端中

[root@source demo]# git remote -v	#檢查是否與遠程倉庫關聯成功
origin  [email protected]:/home/git/repos/demo.git (fetch)
origin  [email protected]:/home/git/repos/demo.git (push)
[root@source demo]# git remote add origin [email protected]:/home/git/repos/demo.git		#遠程提交
fatal: remote origin already exists.
[root@source demo]# git remote rm origin
[root@source demo]# git remote add origin [email protected]:/home/git/repos/demo.git
[root@source demo]# ls
pom.xml  src  target

或者換個名字

3.15 將源碼推送至遠程倉庫

[root@source demo]# git push origin master
Counting objects: 47, done.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (47/47), 5.46 KiB | 0 bytes/s, done.
Total 47 (delta 1), reused 0 (delta 0)
To [email protected]:/home/git/repos/demo.git
 * [new branch]      master -> master

3.16 切換到git上面查看

[git@git demo.git]$ ll
total 16
drwxrwxr-x.  2 git git    6 May 31 13:24 branches
-rw-rw-r--.  1 git git   66 May 31 13:24 config
-rw-rw-r--.  1 git git   73 May 31 13:24 description
-rw-rw-r--.  1 git git   23 May 31 13:24 HEAD
drwxrwxr-x.  2 git git  242 May 31 13:24 hooks
drwxrwxr-x.  2 git git   21 May 31 13:24 info
drwxrwxr-x. 48 git git 4096 May 31 16:22 objects		#被修改過
drwxrwxr-x.  4 git git   31 May 31 13:24 refs
[git@git demo.git]$ cd objects/	
02/   16/   1c/   2b/   37/   43/   56/   69/   81/   9b/   b0/   c4/   e4/   f3/   f9/   pack/ 
07/   18/   1d/   32/   40/   46/   63/   76/   82/   9c/   b6/   d2/   e6/   f5/   ff/   
09/   19/   28/   34/   42/   51/   66/   77/   9a/   af/   c2/   e3/   f1/   f8/   info/ 

四:在github上創建一個新的倉庫用以提交

#或者使用最開始創建的倉庫2020531

創建一個新的倉庫

github會基於兩種方式提交

https或者ssh

在這裏插入圖片描述

4.1 把項目推到github上去

4.1.1 需要修改一下本地的參數

[root@source demo]# ls -a
.  ..  .git  pom.xml  src  target
[root@source demo]# cd .git/
[root@source .git]# ls
branches  COMMIT_EDITMSG  config  description  HEAD  hooks  index  info  logs  objects  refs
[root@source .git]# vim config 
修改url,去指向github
        url = [email protected]:gsy-0519/2020531.git

或者使用https

4.1.2 然後先把項目進行本地提交add

[root@source .git]# git add .
fatal: This operation must be run in a work tree
[root@source .git]# cd /root/demo/
[root@source demo]# git add .

4.1.3 commit -m起名字

[root@source demo]# git commit -m "2020532"
# On branch master
nothing to commit, working directory clean
[root@source demo]# git push origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

4.1.4 ssh 不行,說沒有權限,使用https

[root@source demo]# vim .git/config 
        url = https://github.com/gsy-0519/2020531.git
[root@source demo]# git push origin master		#上傳
Username for 'https://github.com': gsy-0519
Password for 'https://[email protected]': 
Counting objects: 47, done.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (47/47), 5.46 KiB | 0 bytes/s, done.
Total 47 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/gsy-0519/2020531.git
 * [new branch]      master -> master

4.1.5 可以了,到網站上查看

回到web

在這裏插入圖片描述
成功

4.1.6 下載拉取項目直接用pull

[root@source demo]# git pull https://github.com/gsy-0519/2020531.git
Username for 'https://github.com': gsy-0519
Password for 'https://[email protected]': 
From https://github.com/gsy-0519/2020531
 * branch            HEAD       -> FETCH_HEAD
Already up-to-date.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章