1. 程式人生 > >git的簡單玩法

git的簡單玩法

簡單 www 依然 說明 新版 %d 提取 comm diff

本篇筆記參考廖雪峰的git教程,為方便查看將命令部分提取並記錄下來。

無意對原作的版權侵犯,如需要學習請到廖雪峰網站學習git

創建git倉庫

# mkdir learngit && cd learngit
# git init

添加一個文件 readme.txt 內容如下:

Git is a version control system.
Git is free software.

將修改的文件加入git緩存

# git add readme.txt

然後將緩存提交到本地倉庫

git commit -m "wrote a readme file"
[master (root
-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt

修改readme.txt文件

Git is a distributed version control system.
Git is free software distributed under the GPL.

然後嘗試提交:

$ git add readme.txt
$ git commit -m "append GPL"
[master 3628164] append GPL
 
1 file changed, 1 insertion(+), 1 deletion(-)

修改readme.txt文件,改成如下內容:

Git is a distributed version control system.
Git is free software.

運行 git status 查看git狀態

# 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")

可以看到readme.txt被修改,查看修改的具體內容

# git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.

提交修改到緩存中

# git add readme.txt

我們再運行git status看看當前倉庫的狀態

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

依然是readme.txt被修改,此時可以放心提交到倉庫

# git commit -m "add distributed"
[master ea34578] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)

提交後,我們再用git status命令看看倉庫的當前狀態:

# git status
# On branch master
nothing to commit (working directory clean)

現在有三個版本:

版本1:wrote a readme file

Git is a version control system.
Git is free software.

版本2:add distributed

Git is a distributed version control system.
Git is free software.

版本3:append GPL

Git is a distributed version control system.
Git is free software distributed under the GPL.

使用 git log 查看歷史版本

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <[email protected]>
Date:   Tue Aug 20 15:11:49 2013 +0800

    append GPL

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <[email protected]>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <[email protected]>
Date:   Mon Aug 19 17:51:55 2013 +0800

    wrote a readme file

加上--pretty=oneline參數可以只輸出摘要信息

$ git log --pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file

像 3628164fb26d48395383f8f31179f24e0882e1e0 稱為 commit ID ,用於代表某個版本,通過他可以回退版本

假如現在想要回退到 add distributed 時的狀態:

$ git reset --hard ea34578d5496d7dd233c827ed32a8cd576c5ee85

如果你保證commit ID前5位唯一,也可以簡寫:

$ git reset --hard ea3457

或者使用 HEAD^ 代表HEAD指針指向上一個版本:

git reset --hard HEAD^

說明:

技術分享

版本回退後,即HEAD指針指向上個版本HEAD^

技術分享

如果想指向上上個版本用:HEAD^^ ,以此類推

如果想指向上100個版本用: HEAD~100

現在已經成功回退到 add distributed 狀態了,此時查看log

$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <[email protected]>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <[email protected]>
Date:   Mon Aug 19 17:51:55 2013 +0800

    wrote a readme file

最新的那個版本append GPL已經看不到了!,而現在又想恢復到那個最新的版本方法如下:

方法一:如果終端沒關,向上翻記錄找到最新版本的coommit ID號

$ git reset --hard 3628164
HEAD is now at 3628164 append GPL

方法二:如果終端關了,查看git歷史操作尋找新版本的commit ID號

$ git reflog
ea34578 [email protected]{0}: reset: moving to HEAD^
3628164 [email protected]{1}: commit: append GPL
ea34578 [email protected]{2}: commit: add distributed
cb926e7 [email protected]{3}: commit (initial): wrote a readme file

方法三:如果歷史操作也被其他操作覆蓋記錄了,查看歷史所有版本號

$ git log --graph --pretty=format:%h -%d %s (%cr) --abbrev-commit --reflog

現在既可以向後回退,也可以向前恢復,只要有COMMIT ID號即可,查看當前狀態號

$ git rev-parse HEAD

假如當前提交了多個文件a b c,現在只想恢復文件c的上次狀態,其他文件a 和 b保持當前狀態

$ git checkout HEAD^ -- c

或者

$ git checkout HEAD^ c

本篇筆記參考廖雪峰的git教程,為方便查看將命令部分提取並記錄下來。

無意對原作的版權侵犯,如需要學習請到廖雪峰網站學習git

git的簡單玩法