GIT入門介紹

來源:互聯網
上載者:User

標籤:git

現在git已經成為當下最流行的版本管理系統,不管是從事的是開發還是營運或者是與之相關的工作崗位,瞭解一下git的強大之處是非常有必要的。本文為自己的學習筆記,借鑒與廖雪峰老師的git教程,(http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ )有不足之處大家可以互相交流。


GIT介紹

“Talk is cheap, Show me the code.” 說到git不得不提一下它的作者Linus-Linux神話的締造者,雖然才過不惑之年,但已經兩次改變了世界。咳咳...簡單膜拜之後,開始進入我們的正題吧 ~ 

git是分布式版本控制系統,這種優於集中式系統的架構是它流行原因之一。與SVN的集中式版本管理不同的是,git可以讓每個用戶端保留一個完整的版本,所以當開發人員在進行自己的工作時,不必連網,當自己的工作完成之後可以先提交到本地,等待有網路的情況下再提交到伺服器。當然這隻是git的一個優點,在分支管理方面,git的表現也是其它系統無法望其項背的。


安裝部署GIT

GIT可以在我們常見的系統中安裝,但是作為一名忠實的Linux愛好者,我更傾向於直接在Linux上進行安裝。下面就簡單的瞭解一下Linux安裝方式.

這裡使用的是CentOS7的系統作為樣本:

[[email protected] ~]# uname  -aLinux work 3.10.0-327.28.2.el7.x86_64 #1 SMP Wed Aug 3 11:11:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

查看系統是否安裝git:

[[email protected] ~]# git-bash: git: command not found

沒有,那就裝上吧:

[[email protected] ~]# yum install  -y git

由於是多人使用的版本控制系統,所以我們要標示上自己的個人賬戶:

[[email protected] ~]# git config --global user.name "trying"  # Your name[[email protected] ~]# git config --global user.email "[email protected]" # Your Mail

建立本地git 倉庫:

[[email protected] ~]# mkdir gitrepo   [[email protected] ~]# cd gitrepo[[email protected] gitrepo]# git init      # 初始化git倉庫,會在此目錄產生一個.git的目錄Initialized empty Git repository in /root/gitrepo/.git/

建立了本地的git 倉庫會產生一個.git的目錄,此目錄中記錄以後所有對代碼的改動資訊。值得注意的是,所有的版本控制系統只能追蹤文本的改動資訊,如代碼,網頁,txt檔案等,對於視頻,圖片這種二進位的檔案是無法追蹤其具體的變化的。


提交一個檔案到git 倉庫

在建立了一個git 倉庫之後,我們就可以嘗試添加一個檔案到我們的git倉庫中了。

建立一個檔案:

[[email protected] gitrepo]# echo "Welcome to gitrepo! " > readme.txt

添加一個檔案到倉庫:

[[email protected] gitrepo]# git add readme.txt # 可以一次添加多個檔案,也分多次添加

將檔案提交到倉庫:

[[email protected] gitrepo]# git commit -m "my first git file"[master (root-commit) 031cca7] my first git file 1 file changed, 1 insertion(+) create mode 100644 readme.txt

執行了git commit之後,檔案就提交到倉庫了,-m 參數是對本此提交的一個說明,在實際的開發過程中,需要對每次提交的內容作說明,這樣更加方便識別和管理。

當對檔案再次改動:

[[email protected] gitrepo]# echo  "change it +1" >> readme.txt [[email protected] gitrepo]# cat readme.txt Welcome to gitrepo! change it +1

使用git status和git diff 命令可以時刻掌握檔案修改的狀態和內容:

[[email protected] gitrepo]# git status# On branch master# Changes not staged for commit:  # 顯示檔案已經被更改,但是還沒有提交。#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##       modified:   readme.txt#no changes added to commit (use "git add" and/or "git commit -a") # 提示需要add[[email protected] gitrepo]# git diff   # 查看修改前後的內容,類似於linux的diff命令diff --git a/readme.txt b/readme.txtindex 42c334d..238398e 100644--- a/readme.txt+++ b/readme.txt@@ -1 +1,2 @@ Welcome to gitrepo! +change it +1                   # 新添加的內容

然後在對修改的檔案進行提交,同時查看git status 在這個過程中的變化:

[[email protected] gitrepo]# git add readme.txt [[email protected] gitrepo]# git status  # 執行add命令後,顯示需要commit這個檔案# On branch master# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##       modified:   readme.txt#[[email protected] gitrepo]# git commit -m "change +1"[master 58a7726] change +1 1 file changed, 1 insertion(+)[[email protected] gitrepo]# git status # 提交完成後,顯示工作目錄為空白,沒有需要提交的內容。# On branch masternothing to commit, working directory clean


版本控制與檔案修改

當我們使用git 提交了多個版本,需要查看之前提交的版本資訊,可以使用git log命令查看:

[[email protected] gitrepo]# git log commit c78d925cd2ce9836cd607089b958a48c8959b1d6Author: trying <[email protected]>Date:   Mon Dec 12 15:34:19 2016 +0800    change +3    # 從近到遠,依次顯示提交的commit資訊commit 8d255bedddd881e4fa70b2c2e6e9b6b14e54f41bAuthor: trying <[email protected]>Date:   Tue Aug 9 01:02:08 2016 +0800    change +2commit 58a7726b7e0f7a05b6b8077578e4085b423956d1Author: trying <[email protected]>Date:   Tue Aug 9 00:41:55 2016 +0800    change +1commit 031cca7e6021aee8c8a4fba49790c59c53990a4fAuthor: trying <[email protected]>Date:   Tue Aug 9 00:08:34 2016 +0800    my first git file

git log命令顯示從最近到最遠的提交日誌,如果想簡化輸出可以使用 --pretty=oneline參數:

[[email protected] gitrepo]# git log  --pretty=onelinec78d925cd2ce9836cd607089b958a48c8959b1d6 change +38d255bedddd881e4fa70b2c2e6e9b6b14e54f41b change +258a7726b7e0f7a05b6b8077578e4085b423956d1 change +1031cca7e6021aee8c8a4fba49790c59c53990a4f my first git file

查看這些有什麼用呢,哈哈,相信聰明的你已經發行了,這裡輸出的日誌中都包含了一個ID,通過這個ID 我們就可以回退到我們想要的版本,如我們要回退到上一個版本,git reset:

[[email protected] gitrepo]# git reset --hard 8d255beHEAD is now at 8d255be change +2[[email protected] gitrepo]# cat readme.txt # 查看檔案發現已經變回之前的內容Welcome to gitrepo! change it +1change +2

這裡使用的是指定ID來回退到指點的版本,其實我們還有一個更加便捷的方法,可以使用HEAD^來回退:

[[email protected]k gitrepo]# git reset --hard HEAD^ # HEAD^前一個版本 HEAD^^前兩個版本HEAD is now at 58a7726 change +1[[email protected] gitrepo]# cat readme.txt Welcome to gitrepo! change it +1

這裡用 “^” 來指定版本,幾個“^”就表示回退幾次,當然,如果回退的版本有點多可以使用HEAD~100,回退到100次版本前。


查看日誌:

[[email protected] gitrepo]# git log --pretty=oneline58a7726b7e0f7a05b6b8077578e4085b423956d1 change +1031cca7e6021aee8c8a4fba49790c59c53990a4f my first git file

通過查看日誌,我們發現我新編輯的更新沒有了,如果ID也沒記住,那豈不是回不到最新的內容了/(ㄒoㄒ)/~~ ,別擔心git都幫我們想到了,使用git reflog:

[[email protected] gitrepo]# git reflog58a7726 [email protected]{0}: reset: moving to HEAD^8d255be [email protected]{1}: reset: moving to 8d255bec78d925 [email protected]{2}: commit: change +38d255be [email protected]{3}: commit: change +258a7726 [email protected]{4}: commit: change +1031cca7 [email protected]{5}: commit (initial): my first git file

發現了吧,我們所有的操作都被記錄了,這裡面就包含了每次操作的ID,這樣,我們又可以找到我們最新的版本,“快進” 到未來:

[[email protected] gitrepo]# git reset --hard c78d925HEAD is now at c78d925 change +3[[email protected] gitrepo]# cat readme.txt Welcome to gitrepo! change it +1change +2change it 3

這樣通過git reset --hard  和 git log,git reflog 命令我們可以讓檔案回到過去的任意狀態。


GIT緩衝區和工作區

在使用git add 命令的時候,實際上是將我們的編輯的檔案提交到GIT的緩衝區,然後再使用git commit 命令將檔案從緩衝區提交到本地倉庫,如果不執行git add 添加檔案,那麼緩衝區就不會記錄此檔案,後面再次變更檔內容時就不會在執行git commit時提交。 如果對檔案進行了git add 之後,再次對檔案修改,需要重新執行git add 添加此檔案,最後執行git commit 的時候,會記錄最後一次git add 的檔案更改。


撤銷工作區修改內容:

有時會出現這樣一種情況,在我們的工作區(git倉庫目錄)修改了大量檔案,但是最後你發現檔案全部都改錯了! 是的,遇到這種尷尬的情況應該怎麼辦呢,回退到上一版本?顯然這樣不科學,尤其是本地沒有上一版本的時候,其實不必擔心,此時執行git status就能獲得提示:

[[email protected] gitrepo]# git status# On branch master# Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##       modified:   readme.txt#no changes added to commit (use "git add" and/or "git commit -a")

提示告訴我們要撤銷工作目錄改變的檔案可以使用git checkout -- <file>:

[[email protected] gitrepo]# git checkout -- readme.txt  #一條命令即可


撤銷暫存區修改內容:

比上面更加尷尬的一種情況是,不僅修改了工作區的內容,而且還將錯誤的內容使用git add 提交到了暫存區,這時候怎麼辦呢,讓我們再看看git status 說什麼吧:

[[email protected] gitrepo]# git status# On branch master# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##       modified:   readme.txt#

提示我們使用git  reset HEAD <file>來從暫存區撤銷,試一下效果:

[[email protected] gitrepo]# git reset HEAD readme.txt Unstaged changes after reset:M       readme.txt[[email protected] gitrepo]# git status # 查看撤銷後的狀態,回到了git add之前的情況# On branch master# Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##       modified:   readme.txt#no changes added to commit (use "git add" and/or "git commit -a")

然後在再使用上面的命令,撤銷工作區的更改:

[[email protected] gitrepo]# git  checkout -- readme.txt [[email protected] gitrepo]# git status# On branch masternothing to commit, working directory clean

這樣,所有的更改都已經撤銷了。

如果不幸執行了commit命令,可以通過版本復原版本來回到之前的狀態,但是如果已經推送到遠程倉庫,那就呵呵了。如果改動直接上線,可以通過持續整合伺服器,可以快速恢複,不過此時或多或少會影響到線上的業務。


刪除檔案

如果我們想刪除本地版本庫中的檔案,可以直接執行下面兩條命令:

[[email protected] gitrepo]# rm -f aa.txt        # 刪除本地檔案[[email protected] gitrepo]# git rm aa.txt       # 刪除版本庫檔案,並且commit提交rm ‘aa.txt‘[[email protected] gitrepo]# git commit -m "del aa.txt"[master 59f04b4] del aa.txt 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 aa.txt

使用git rm <file> 和 git commit 來刪除版本庫中的檔案。


但是如果本地檔案不小心刪除錯了,只要我們版本庫中提交過就可以快速恢複:

[[email protected] gitrepo]# git checkout -- readme.txt
[[email protected] gitrepo]# rm -f readme.txt [[email protected] gitrepo]# lltotal 0[[email protected] gitrepo]# git status# On branch master# Changes not staged for commit:#   (use "git add/rm <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##       deleted:    readme.txt#no changes added to commit (use "git add" and/or "git commit -a")[roo[email protected] gitrepo]# git checkout -- readme.txt[[email protected] gitrepo]# lltotal 4-rw-r--r--. 1 root root 69 Dec 12 18:36 readme.txt  # 檔案又回來了!




本文出自 “Trying” 部落格,請務必保留此出處http://tryingstuff.blog.51cto.com/4603492/1882034

GIT入門介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.