git倉庫的簡單使用

筆記內容:git倉庫的簡單使用
筆記日期:2018-01-12

  • 22.5/22.6 單機上使用git
  • 22.7 建立遠程倉庫
  • 22.8 克隆遠程倉庫

22.5/22.6 單機上使用git

git是分佈式的倉庫,我們不需要把代碼上傳或更新到某個特定的服務器上,所以它不需要依賴網絡,我們可以在本地創建一個git倉庫。

安裝命令:

yum install -y git

創建git倉庫:

[root@localhost ~]# mkdir /data/gitroot
[root@localhost ~]# cd /data/gitroot
[root@localhost /data/gitroot]# git init
初始化空的 Git 版本庫於 /data/gitroot/.git/
[root@localhost /data/gitroot]# ll -a
總用量 0
drwxr-xr-x  3 root root  17 1月  12 18:38 .
drwxr-xr-x 11 root root 144 1月  12 18:38 ..
drwxr-xr-x  7 root root 111 1月  12 18:38 .git  # 會生成一個.git目錄
[root@localhost /data/gitroot]#

創建一個新的文件,然後隨便寫些東西:

[root@localhost /data/gitroot]# vim Hello.java
class Hello{

        public static void main(String[] s){

                System.out.println("Hello World!");

        }

}

把剛剛創建的文件添加到git倉庫裏,然後進行上傳:

[root@localhost /data/gitroot]# git add Hello.java
[root@localhost /data/gitroot]# git commit -m "add new file Hello.java"
[master(根提交) 1387815] add new file Hello.java
 Committer: root <[email protected]>
您的姓名和郵件地址基於登錄名和主機名進行了自動設置。請檢查它們正確  # 如果你沒有設置姓名和郵件地址就會有這段提示
與否。您可以通過下面的命令對其進行明確地設置以免再出現本提示信息:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

設置完畢後,您可以用下面的命令來修正本次提交所使用的用戶身份:

    git commit --amend --reset-author

 1 file changed, 9 insertions(+)
 create mode 100644 Hello.java

# 爲了避免老是打印提示信息,可以隨便設置一下這兩項信息
[root@localhost /data/gitroot]# git config --global user.name "zero"  
[root@localhost /data/gitroot]# git config --global user.email [email protected]

接着再次修改文件中的內容,然後進行提交:

[root@localhost /data/gitroot]# echo "class hello{}" >> Hello.java
[root@localhost /data/gitroot]# git add Hello.java
[root@localhost /data/gitroot]# git commit -m "add Hello.java agin"
[master 8d77f14] add Hello.java agin
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@localhost /data/gitroot]#

git status命令可以查看當前倉庫中的狀態,比如是否有改動的文件等:

[root@localhost /data/gitroot]# git status
# 位於分支 master
無文件要提交,乾淨的工作區
[root@localhost /data/gitroot]#

git diff命令可以對比某個文件本次修改了什麼內容,相比較倉庫裏面的版本:

[root@localhost /data/gitroot]# echo "class emm{}" >> Hello.java 
[root@localhost /data/gitroot]# git diff Hello.java
diff --git a/Hello.java b/Hello.java
index 2935899..2bac836 100644
--- a/Hello.java
+++ b/Hello.java
@@ -8,3 +8,4 @@ class Hello{

 }
 class hello{} 
+class emm{}  # 該文件相比較倉庫裏面的版本多了這行代碼
[root@localhost /data/gitroot]#

我們都知道,代碼管理倉庫最重要的一個功能就是版本控制,通過版本控制,可以進行版本的回退操作:

# 多更改幾次Hello.java,然後add,commit
[root@localhost /data/gitroot]# git add Hello.java
[root@localhost /data/gitroot]# git commit -m "ch Hello.java agin"
[master d1cf481] ch Hello.java agin
 1 file changed, 1 insertion(+)
[root@localhost /data/gitroot]# echo "class Hi{}" >> Hello.java 
[root@localhost /data/gitroot]# git add Hello.java; git commit -m "ch Hello.java agin"
[master 5341f93] ch Hello.java agin
 1 file changed, 1 insertion(+)
[root@localhost /data/gitroot]# git log   # 查看所有的提交記錄
commit 5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c  # 這個是該版本的id,進行回退操作時需要使用
Author: zero <[email protected]>
Date:   Fri Jan 12 18:59:51 2018 +0800

    ch Hello.java agin

commit d1cf48198534e3bd1a7764ce27667f756f4974b5
Author: zero <[email protected]>
Date:   Fri Jan 12 18:59:12 2018 +0800

    ch Hello.java agin

commit 8d77f141ba84dae557ab42cd9a110c2542e06643
Author: zero <[email protected]>
Date:   Fri Jan 12 18:50:07 2018 +0800

    add Hello.java agin

commit b576e395c1197a5dc0aa72e584bb54ef9ab66458
Author: root <[email protected]>
Date:   Fri Jan 12 18:47:52 2018 +0800

    add Hello.java agin

commit 1387815eb4f0eeb58966d89d7756a0ac45c3dde8
Author: root <[email protected]>
Date:   Fri Jan 12 18:44:06 2018 +0800

:
[root@localhost /data/gitroot]# git log --pretty=oneline  # 一行顯示
5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c ch Hello.java agin
d1cf48198534e3bd1a7764ce27667f756f4974b5 ch Hello.java agin
8d77f141ba84dae557ab42cd9a110c2542e06643 add Hello.java agin
b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
[root@localhost /data/gitroot]# git reset --hard b576e395c1197a5dc0aa72e584bb54ef9ab66458 # 回退版本
HEAD 現在位於 b576e39 add Hello.java agin
[root@localhost /data/gitroot]# git log --pretty=oneline  # 可以看到已經回退到第二個版本了
b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
[root@localhost /data/gitroot]# git reset --hard 1387815eb  # 這個版本ID是可以簡寫的,取前面的幾個字符即可
HEAD 現在位於 1387815 add new file Hello.java
[root@localhost /data/gitroot]# git log --pretty=oneline
1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
[root@localhost /data/gitroot]#

如果回退版本後,發現不合適,想要回退到新版本或者其他歷史版本上,可以使用git reflog命令查看所有歷史版本:

[root@localhost /data/gitroot]# git reflog  # 查看所有歷史版本
1387815 HEAD@{0}: reset: moving to 1387815eb
b576e39 HEAD@{1}: reset: moving to b576e395c1197a5dc0aa72e584bb54ef9ab66458
5341f93 HEAD@{2}: commit: ch Hello.java agin
d1cf481 HEAD@{3}: commit: ch Hello.java agin
8d77f14 HEAD@{4}: commit: add Hello.java agin
b576e39 HEAD@{5}: commit: add Hello.java agin
1387815 HEAD@{6}: commit (initial): add new file Hello.java
[root@localhost /data/gitroot]# git reset --hard 5341f93  # 通過id進行回退
HEAD 現在位於 5341f93 ch Hello.java agin
[root@localhost /data/gitroot]# git log --pretty=oneline  # 回退到最新版本了
5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c ch Hello.java agin
d1cf48198534e3bd1a7764ce27667f756f4974b5 ch Hello.java agin
8d77f141ba84dae557ab42cd9a110c2542e06643 add Hello.java agin
b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
[root@localhost /data/gitroot]#

通過git可以恢復刪除的文件,前提是你已經將文件提交到了倉庫中。如果不小心把某個文件刪除了,而這個文件已經存儲在倉庫中的話,就可以從倉庫恢復這個文件:

[root@localhost /data/gitroot]# rm -f Hello.java 
[root@localhost /data/gitroot]# ls
[root@localhost /data/gitroot]# git checkout -- Hello.java  # 因爲文件已經存儲到倉庫裏了,所以可以從倉庫恢復
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]#

如果某個文件進行了修改,add後但沒有commit,再想回退到上一次提交的狀態,可以使用git reset HEAD filename,再執行git checkout -- filename:

[root@localhost /data/gitroot]# echo "class Car{}" >> Hello.java 
[root@localhost /data/gitroot]# git add Hello.java
[root@localhost /data/gitroot]# git reset HEAD Hello.java  # 這個命令可以把add標記去掉
重置後撤出暫存區的變更:
M   Hello.java
[root@localhost /data/gitroot]# git checkout -- Hello.java
[root@localhost /data/gitroot]# cat !$
cat Hello.java
class Hello{

    public static void main(String[] s){

        System.out.println("Hello World!"); 

    }

}
class hello{ } 
class emm{}
class Hi{}
[root@localhost /data/gitroot]#

刪除倉庫中的文件:

[root@localhost /data/gitroot]# git rm Hello.java   # 刪除倉庫中的文件
rm 'Hello.java'
[root@localhost /data/gitroot]# ls
[root@localhost /data/gitroot]# git commit -m "delete Hello.java"  # 提交刪除操作
[master 86da43d] delete Hello.java
 1 file changed, 12 deletions(-)
 delete mode 100644 Hello.java
[root@localhost /data/gitroot]# git checkout -- Hello.java  # 這時候就無法從倉庫中檢出該文件了
error: pathspec 'Hello.java' did not match any file(s) known to git.
[root@localhost /data/gitroot]#

即便刪除了倉庫中的文件,也是可以通過版本id來恢復的:

[root@localhost /data/gitroot]# git log --pretty=oneline
86da43d5b2f68985d376f297fc670d16fd473884 delete Hello.java
5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c ch Hello.java agin
d1cf48198534e3bd1a7764ce27667f756f4974b5 ch Hello.java agin
8d77f141ba84dae557ab42cd9a110c2542e06643 add Hello.java agin
b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
[root@localhost /data/gitroot]# git reset --hard 5341f93
HEAD 現在位於 5341f93 ch Hello.java agin
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]# cat Hello.java 
class Hello{

    public static void main(String[] s){

        System.out.println("Hello World!"); 

    }

}
class hello{ } 
class emm{}
class Hi{}
[root@localhost /data/gitroot]#

22.7 建立遠程倉庫

以上的示例都是在本地使用git倉庫,沒有涉及到遠程倉庫的使用。下面演示一下如何連接遠程的GitHub倉庫:

1.首先到 https://github.com 註冊一個賬號,我這裏已經有賬戶了,所以直接登錄:
git倉庫的簡單使用

2.登錄之後,點擊右上角,頭像旁邊的 + 圖標,創建一個自己的repository(倉庫):
git倉庫的簡單使用

3.填寫倉庫的相關信息:
git倉庫的簡單使用

4.創建完成,如下,遠程倉庫就創建好了:
git倉庫的簡單使用

可以把GitHub上創建的倉庫,作爲我們的遠程服務端。

5.在本地機器上創建密鑰對:

[root@localhost ~]# 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.
The key fingerprint is:
0e:17:93:c8:8c:9a:d9:b4:21:6e:72:68:41:fa:79:0a [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .               |
|o    + . .       |
|... + + +        |
| +.O o   o       |
|E.X + . S        |
|.= o   +         |
|  .     .        |
|                 |
|                 |
+-----------------+
[root@localhost ~]# cat .ssh/id_rsa.pub  # 複製你的公鑰

6.然後給遠程的倉庫添加密鑰認證,保證訪問的安全性:
git倉庫的簡單使用
git倉庫的簡單使用
git倉庫的簡單使用

添加完成:
git倉庫的簡單使用


連接遠程倉庫

以上已經在GitHub上創建好了一個遠程倉庫,並且也添加了密鑰認證,現在我們就可以在本地上連接這個倉庫了。

1.創建一個目錄,用於存放和上傳倉庫文件,也相當於是一個本地倉庫:

[root@localhost ~]# mkdir /tmp/example
[root@localhost ~]# cd !$
cd /tmp/example
[root@localhost /tmp/example]#

2.根據GitHub的操作示例進行倉庫的初始化:

[root@localhost /tmp/example]# echo "# example" >> README.md  # 生成README.md文件
[root@localhost /tmp/example]# git init  # 初始化
初始化空的 Git 版本庫於 /tmp/example/.git/
[root@localhost /tmp/example]# ll -a
總用量 8
drwxr-xr-x  3 root root   33 1月  12 23:17 .
drwxrwxrwt. 9 root root 4096 1月  12 23:12 ..
drwxr-xr-x  7 root root  111 1月  12 23:17 .git
-rw-r--r--  1 root root   10 1月  12 23:16 README.md
[root@localhost /tmp/example]# git add README.md
[root@localhost /tmp/example]# git commit -m "first commit"
[master(根提交) 4b710bc] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

## 將本地文件推送到遠程倉庫上
[root@localhost /tmp/example]# git remote add origin https://github.com/Binary-ZeroOne/example.git
[root@localhost /tmp/example]# git push -u origin master
Username for 'https://github.com': Binary-ZeroOne  # 你的github用戶名
Password for 'https://[email protected]':   # 以及密碼
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
 * [new branch]      master -> master
分支 master 設置爲跟蹤來自 origin 的遠程分支 master。
[root@localhost /tmp/example]# 

然後再創建一個文件,再次進行推送:

[root@localhost /tmp/example]# vim example.txt
This is a example
[root@localhost /tmp/example]# git add example.txt
[root@localhost /tmp/example]# git commit -m "example commit"
[master aacb77a] example commit
 1 file changed, 1 insertion(+)
 create mode 100644 example.txt
[root@localhost /tmp/example]# git push
warning: push.default 未設置,它的默認值將會在 Git 2.0 由 'matching'  # 以下是新版本的一些提示信息
修改爲 'simple'。若要不再顯示本信息並在其默認值改變後維持當前使用習慣,
進行如下設置:

  git config --global push.default matching

若要不再顯示本信息並從現在開始採用新的使用習慣,設置:

  git config --global push.default simple

參見 'git help config' 並查找 'push.default' 以獲取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有時要使用老版本的 Git,
爲保持兼容,請用 'current' 代替 'simple' 模式)

Username for 'https://github.com': Binary-ZeroOne
Password for 'https://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
   4b710bc..aacb77a  master -> master
[root@localhost /tmp/example]#

接着到GitHub的倉庫上,可以發現多了兩個文件,README.md 和 example.txt:
git倉庫的簡單使用
git倉庫的簡單使用
git倉庫的簡單使用


22.8 克隆遠程倉庫

以上演示了將本地文件推送到遠程倉庫,我們也可以將遠程倉庫給克隆到本地機器上。

1.複製遠程倉庫的URL鏈接:
git倉庫的簡單使用

2.然後到本地機器上執行命令進行克隆:

[root@localhost ~]# cd /home/
[root@localhost /home]# git clone https://github.com/Binary-ZeroOne/example.git  # 克隆的命令
正克隆到 'example'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
[root@localhost /home]# ls
example
[root@localhost /home]# ll -a example/
總用量 8
drwxr-xr-x  3 root root  51 1月  12 23:37 .
drwxr-xr-x. 8 root root  88 1月  12 23:37 ..
-rw-r--r--  1 root root  18 1月  12 23:37 example.txt
drwxr-xr-x  8 root root 152 1月  12 23:37 .git
-rw-r--r--  1 root root  10 1月  12 23:37 README.md
[root@localhost /home]# 

注:公開的倉庫是任何人都可以進行克隆的,但是隻能克隆不可以對倉庫進行寫操作。

3.對克隆的文件進行更改,然後再推送到遠程的倉庫,因爲我們是該倉庫的所有者,可以進行寫操作:

[root@localhost /home]# cd example/
[root@localhost /home/example]# echo "This is a change operation" >> example.txt 
[root@localhost /home/example]# git add example.txt
[root@localhost /home/example]# git commit -m "change example.txt"
[master 09b7380] change example.txt
 1 file changed, 1 insertion(+)
[root@localhost /home/example]# git config --global push.default simple
[root@localhost /home/example]# git push
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://[email protected]': 
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
   aacb77a..09b7380  master -> master
[root@localhost /home/example]#

3.然後到GitHub上看看是否有更改的內容:
git倉庫的簡單使用

4.我現在在GitHub上更改這個文件的內容,更改之後同樣可以在本地把新內容拉下來:
git倉庫的簡單使用
git倉庫的簡單使用

拉到頁面下方,點擊Commit changes提交更改:
git倉庫的簡單使用

接着到本地機器上,執行git pull命令,把遠程倉庫的更改內容拉下來:

[root@localhost /home/example]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
來自 https://github.com/Binary-ZeroOne/example
   09b7380..b71be6b  master     -> origin/master
更新 09b7380..b71be6b
Fast-forward
 example.txt | 1 +
 1 file changed, 1 insertion(+)
[root@localhost /home/example]# cat example.txt 
This is a example
This is a change operation
This is another change operation  # 可以看到文件內容成功更新了
[root@localhost /home/example]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章