86.git使用 創建和克隆遠程倉庫

22.5/22.6 單機上使用gitlinux

22.7 創建遠程倉庫git

22.8 克隆遠程倉庫github

 

 

 

 

 

22.5/22.6 單機上使用gitweb

 

 

git是分佈式的。所謂分佈式是咱們不依賴網絡。你再你電腦上使用git我在我電腦上使用git們均可以,不須要把個人代碼更新到服務端上去 。在本地作操做就能夠了vim

1.yum install -y git網絡

#yum安裝便可dom

2.mkdir /data/gitrootssh

#建立git 目錄分佈式

3.cd /data/gitrootide

4.git init //初始化倉庫

5.echo -e  「123\naaa\n456\nbbb」 > 1.txt //建立一個新文件

#隨便寫點東西

6.git add 1.txt //把1.txt添加到倉庫,作標記

7.git commit -m 「add new file 1.txt」 //add完了必需要commit纔算真正把文件提交到git倉庫裏

#-m""雙引號裏指定解釋說明這個文件,可儘可能寫的清楚

8.再次更改1.txt

9.git status  //查看當前倉庫中的狀態,好比是否有改動的文件(未提交的)

10.git diff 1.txt  //能夠對比1.txt本次修改了什麼內容,相比較倉庫裏面的版本

#也就是查看變動

11.git log//查看全部提交記錄。前面的字符是ID號

#版本回退

#多更改幾回1.txt,而後add,commit,就可查看

12.git log --pretty=oneline//一行顯示。前面的字符是ID號

#基於git log可顯示的更加好看

13.git reset --hard f7c8e9//回退版本,其中後面跟的字符串是ID號簡寫

#撤銷修改

#也能夠利用ID號再回到以前的已經回退過的版本,但前提你要知道要回退的那個ID號

14.git reflog //查看全部歷史版本

#這個命令能夠查看全部的歷史版本及ID號,包括已經刪掉的

15.rm -f 1.txt//不當心刪除了1.txt

git checkout -- 1.txt//恢復1.txt #注意空格

16.若是1.txt文件修改,add後但沒有commit,再想回退到上一次提交的狀態,可使用git reset HEAD 1.txt來取消標記。#也就是能夠將你add命令的標記去掉

再執行git checkout -- 1.txt(就恢復1.txt到修改以前的狀態)

17.刪除文件

echo -e "11111111111\n2222222222" > 2.txt #測試

git rm 2.txt #刪除2.txt

git commit -m "rm 2.txt" #提交也就是將庫裏的文件刪除

18. git commit -a #能夠查看提示信息

 

 

 

實例:

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

[root@axinlinux-01 ~]# mkdir /data/gitroot

[root@axinlinux-01 ~]# cd /data/gitroot/

[root@axinlinux-01 gitroot]# git init

初始化空的 Git 版本庫於 /data/gitroot/.git/

[root@axinlinux-01 gitroot]# ls -la #當前目錄下會生成.git的文件。跟svn很像

總用量 0

drwxr-xr-x 3 root root 18 11月 22 16:58 .

drwxr-xr-x 12 root root 167 11月 22 16:57 ..

drwxr-xr-x 7 root root 119 11月 22 16:58 .git

[root@axinlinux-01 gitroot]# vim 1.txt #隨便寫點東西

[root@axinlinux-01 gitroot]# git add 1.txt #添加到倉庫,作標記

[root@axinlinux-01 gitroot]# git commit -m "add 1.txt" #真正提交到倉庫。可是報錯了。意思是讓咱們告訴他咱們的郵箱以及username。有提示咱們怎麼操做

*** 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 'root@axinlinux-01.(none)')

[root@axinlinux-01 gitroot]# git config --global user.email "519321158@qq.com" #按操做輸入郵箱

[root@axinlinux-01 gitroot]# git config --global user.name "axin" #按操做輸入username

[root@axinlinux-01 gitroot]# git commit -m "add 1.txt" #再次提交

[master(根提交) 3369881] add 1.txt

1 file changed, 4 insertions(+)

create mode 100644 1.txt

[root@axinlinux-01 gitroot]# git status

# 位於分支 master

無文件要提交,乾淨的工做區

[root@axinlinux-01 gitroot]# vim 1.txt #變動一下,可是不提交

[root@axinlinux-01 gitroot]# git status #咱們查看狀態,就會出現會提交的。以及怎麼提交

# 位於分支 master

# 還沒有暫存以備提交的變動:

# (使用 "git add <file>..." 更新要提交的內容)

# (使用 "git checkout -- <file>..." 丟棄工做區的改動)

#

# 修改: 1.txt

#

修改還沒有加入提交(使用 "git add" 和/或 "git commit -a")

[root@axinlinux-01 gitroot]# git diff 1.txt #查看變動

diff --git a/1.txt b/1.txt

index 06b4546..4b2d89e 100644

--- a/1.txt

+++ b/1.txt

@@ -3,4 +3,4 @@ dfnjdshfjhdkjfhsdk

njkndkjfnhkdjsnfkjdsnf

dfdfdf

735678465874368564376587435

-

+43679839885679875698859677

[root@axinlinux-01 gitroot]# git log

commit 9bd505e01e4713b386b01b55faa109465a609762

Author: axin <519321158@qq.com> #咱們首次登錄設置的,能夠在配置中查看到

Date: Thu Nov 22 17:20:54 2018 +0800

 

add 1.txt again3

下面還有其餘版本,不作演示了

[root@axinlinux-01 gitroot]# cat /root/.gitconfig #是在這定義了你的全局配置

[root@axinlinux-01 gitroot]# git log --pretty=oneline #比起git log更加規範

9bd505e01e4713b386b01b55faa109465a609762 add 1.txt agent3

3655c5f4cb7f018bb36fe0d75301a223a334fe48 add 1.txt agent2

4e654b08101e8acb95ee09f84f24a6206842835f add 1.txt agent

3369881e206ece9abeab7234bef9c1192789f9b5 add 1.txt

[root@axinlinux-01 gitroot]# git reset --hard 3369881e206ece9abeab72 #後面的數字可簡寫幾個

HEAD 如今位於 3369881 add 1.txt

[root@axinlinux-01 gitroot]# git log --pretty=oneline

3369881e206ece9abeab7234bef9c1192789f9b5 add 1.txt

[root@axinlinux-01 gitroot]# git reflog #查看全部的歷史版本。可讓咱們知道並來回回退各個歷史版本。前面的是ID號

9bd505e HEAD@{0}: reset: moving to 9bd505e01e4713b3

3369881 HEAD@{1}: reset: moving to 3369881e206ece9abeab72

9bd505e HEAD@{2}: commit: add 1.txt agent3

3655c5f HEAD@{3}: commit: add 1.txt agent2

4e654b0 HEAD@{4}: commit: add 1.txt agent

3369881 HEAD@{5}: commit (initial): add 1.txt

[root@axinlinux-01 gitroot]# rm -rf 1.txt #假設咱們不當心刪掉了這個文件

[root@axinlinux-01 gitroot]# ls

[root@axinlinux-01 gitroot]# git checkout -- 1.txt #能夠用git checkout -- 1.txt來恢復。注意空格

[root@axinlinux-01 gitroot]# ls #而後就有了。由於他還存在於你的版本管理庫裏,雖然目錄裏已經沒有了

1.txt

[root@axinlinux-01 gitroot]# vim 1.txt

[root@axinlinux-01 gitroot]# git add 1.txt

[root@axinlinux-01 gitroot]# git reset HEAD 1.txt

重置後撤出暫存區的變動:

M 1.txt

[root@axinlinux-01 gitroot]# git checkout -- 1.txt

17.

[root@axinlinux-01 gitroot]# git rm 1.txt #使用git rm將1.txt刪除

rm '1.txt'

[root@axinlinux-01 gitroot]# git commit -m "delete 1.txt" #再次提交就是講庫裏的也刪掉了

[master e0ad56c] delete 1.txt

1 file changed, 6 deletions(-)

delete mode 100644 1.txt

[root@axinlinux-01 gitroot]# ls #就沒有了

[root@axinlinux-01 gitroot]# git checkout -- 1.txt #並且也恢復不來

error: pathspec '1.txt' did not match any file(s) known to git.

[root@axinlinux-01 gitroot]# git reflog #恢復的話使用git erflog來查看歷史版本

e0ad56c HEAD@{0}: commit: delete 1.txt

1a3d7c8 HEAD@{1}: commit: add 1.txt agint

9bd505e HEAD@{2}: reset: moving to 9bd505e01e4713b3

3369881 HEAD@{3}: reset: moving to 3369881e206ece9abeab72

9bd505e HEAD@{4}: commit: add 1.txt agent3

3655c5f HEAD@{5}: commit: add 1.txt agent2

4e654b0 HEAD@{6}: commit: add 1.txt agent

3369881 HEAD@{7}: commit (initial): add 1.txt

[root@axinlinux-01 gitroot]# git reset --hard 1a3d7c8 #而後在使用git reset --hard ID號來恢復

HEAD 如今位於 1a3d7c8 add 1.txt agint

[root@axinlinux-01 gitroot]# ls

1.txt 2.txt

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

22.7 創建遠程倉庫

 

 

 

1.首先到 https://github.com 註冊一個帳號,建立本身的git,點repositories 再點new

2.名字自定義,好比叫studygit 選擇public 點 create repository

3.添加key:右上角點本身頭像,選擇settings,左側選擇SSH and GPG keys

4.左側點New SSH key,把linux機器上的~/.ssh/id_rsa.pub內容粘貼到這裏

5.把本地倉庫推送到遠程倉庫 git remote add origin git@github.com:aminglinux/studygit.git //這一步是在遠程建立一個新的倉庫studygit,名字儘可能和本地的一致

6.git push -u origin master //而後把本地的studygit倉庫推送到遠程的studygit

下一次再推送,就能夠直接 git push

 

 

 

 

實例:

進入github網站,建立帳號

輸入username爲axin-linux,郵箱爲519321158@qq.com,密碼爲wx15098751520

再作三次傻逼的遊戲,就能夠點擊sign in登陸了

 

 

以上,就會看到這個界面。想有一些簡單的用法或說明啥的,一會在linux上還會用到這些提示的用法

此時還須要加一個祕鑰。目的是爲了認證,你得告訴他我是一個合法的用戶

 

 

以上,公鑰的方法爲,linux上:

[root@axinlinux-01 gitroot]# ssh-keygen #ssh-keygen命令生成密鑰對

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. #帶pub的爲公鑰,在這個路徑裏

The key fingerprint is:

SHA256:upAdKxtWB6rI5MaSnlrXbuKz8WQfXohi+NwqQyjAZqQ root@axinlinux-01

The key's randomart image is:

+---[RSA 2048]----+

| |

| . |

|+ . |

|E+ . . |

|+o . o S |

|Bo.o = * . |

|=*+ @ O o . |

|+.+*o&.+ o |

|oo +B*= o |

+----[SHA256]-----+

[root@axinlinux-01 gitroot]# cd #由於公鑰在root目錄下,直接cd出來

[root@axinlinux-01 ~]# cat .ssh/id_rsa.pub #cat 這個公鑰文件就能夠了

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXXjoeyXRbpHSwxJa8kvnGev9HV7xqHfQxxVL711ypsMMaKGlpcRVGhRWNkfzS37W0jAfnVZJX3/nSD2BEcrmqmPAxRG48+OZMBhEqh6g6KeCe0JgOA0azm/gpujrrcRNLxbVsT6dTGiRzxfvAG2OPqwOCWN/a3QMPXpdd2IDVTSAw0GMDBYUpr+tHu1DzeVogt5wvdkcBAtb9+caDAAWM0uZLLlG/mZ+Zm2FqX7j8J9LPpy2LIeJF0OBbzeFHMHT1zdUkpLBL5FQSmQ2hrwweT3iqcBXB/A7MNQNan7SFAW4vj7LiUWuxA301RBHuY8e9sS74nLb9lJZds1yle5Hz root@axinlinux-01

 

以上,會讓你在輸入一次密碼。就會出現這個畫面。

接下來還要在客戶端上(linux)上,新建一個倉庫,而後在這個倉庫寫一些東西,再把這些東西推送到遠程上去

[root@axinlinux-01 ~]# cd /tmp/ #就在tmp下建立這個倉庫吧

[root@axinlinux-01 tmp]# mkdir apelearn #爲了與遠程端保持一致,最好也寫成同樣的名稱

[root@axinlinux-01 tmp]# cd apelearn/ #cd進去

[root@axinlinux-01 apelearn]#

到這,有點小問題。我又從新搞了一次

[root@axinlinux-01 tmp]# rm -rf apelearn/

[root@axinlinux-01 tmp]# mkdir aming_linux

[root@axinlinux-01 tmp]# cd aming_linux/

[root@axinlinux-01 aming_linux]# echo "# aming_linux" >> README.md #從這一步開始都是複製的建遠程庫(web上)以後提示的操做

[root@axinlinux-01 aming_linux]# git init

初始化空的 Git 版本庫於 /tmp/aming_linux/.git/

[root@axinlinux-01 aming_linux]# git add README.md

[root@axinlinux-01 aming_linux]# git commit -m "first commit"

[master(根提交) 25ca739] first commit

1 file changed, 1 insertion(+)

create mode 100644 README.md

[root@axinlinux-01 aming_linux]# git remote add origin https://github.com/axin-linux/aming_linux.git

[root@axinlinux-01 aming_linux]# git push -u origin master

Username for 'https://github.com': axin-linux #初次登陸會讓輸入用戶名和密碼,就是一開始建立的用戶名和密碼

Password for 'https://axin-linux@github.com':

Counting objects: 3, done.

Writing objects: 100% (3/3), 216 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

remote:

remote: Create a pull request for 'master' on GitHub by visiting:

remote: https://github.com/axin-linux/aming_linux/pull/new/master

remote:

To https://github.com/axin-linux/aming_linux.git

* [new branch] master -> master

分支 master 設置爲跟蹤來自 origin 的遠程分支 master。

以上,就把本地的文件推送到了遠程。那怎麼看遠程有沒有呢?回到web上,刷新一下界面就能看到咱們按照提示建的README了

[root@axinlinux-01 aming_linux]# vim 2.txt #咱們再來測試一下。建立個2.txt

[root@axinlinux-01 aming_linux]# git add 2.txt

[root@axinlinux-01 aming_linux]# git commit -m "add 2.txt"

[master 27e1249] add 2.txt

1 file changed, 1 insertion(+)

create mode 100644 2.txt

[root@axinlinux-01 aming_linux]# git push #記得要推一下。才能到遠程

再回到web上刷新一下就有了

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

22.8 克隆遠程倉庫

 

 

 

github上有不少開源的項目。實際工做,使用最多的就是咱們怎麼把遠程上這些項目搞到本地上來

1.cd /home

2.git clone git@github.com:aminglinux/lanmp.git

#使用git clone後面跟克隆的地址

它提示,會在當前目錄下初始化一個倉庫,並建立一個.git的目錄,以下

Initialized empty Git repository in /home/lanmp/.

3.git/完成後,ls能夠看到一個lanmp的目錄

4.cd lanmp

5.vi lanmp.sh 編輯一下文件,而後提交

6.git add lanmp.sh

7.git commit -m "sdlfasdf"

8.而後再推送到遠程服務端

git push

9.若是咱們在遠程裏(web上)足了一些文件的更改,要拉倒本地:

git pull

 

 

實例:

[root@axinlinux-01 aming_linux]# git clone git@github.com:aminglinux/lanmp.git

#git clone克隆的地址就是上圖中標出的地址

正克隆到 'lanmp'...

Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.

remote: Enumerating objects: 32, done.

remote: Total 32 (delta 0), reused 0 (delta 0), pack-reused 32

接收對象中: 100% (32/32), 5.99 KiB | 0 bytes/s, done.

處理 delta 中: 100% (6/6), done.

[root@axinlinux-01 aming_linux]# cd lanmp/

[root@axinlinux-01 lanmp]# ls

lanmp.sh README.md

相關文章
相關標籤/搜索