GET基本知識

基礎在前:

安裝 
apt-get install Git 
默認安裝目錄:/usr/share 
可執行文件路徑:/usr/bin/git

上傳本地項目到github 
- cd到目標項目的根目錄下 
- git init(創建git本地倉庫) 
- git add . (項目的所有文件添加到倉庫中,如果想添加某個特定的文件,只需把.換成特定的文件名即可) 
- git commit -m “example”(將add的文件commit到倉庫) 
- 去github上創建自己的Repository(登錄github->new Respository,拷貝生成的https clone URL) 
- git remote add origin https_clone_URL(將本地的倉庫關聯到github上)(備註:如果顯示origin已存在,則:git remote rm origin 刪除後再關聯) 
- git pull origin master(上傳github之前,要先pull一下) 
- git push -u origin master(上傳代碼到遠程倉庫) 
-ok,上傳成功!

http://blog.csdn.net/whu_zhangmin/article/details/12111663


覆蓋了Git中很基礎的一部分。命令行中輸入的代碼用深藍色表示,輸出的代碼用棕色表示

首先安裝git

$ sudo apt-get install git

$ sudo apt-get install git-doc git-svn git-email git-gui gitk

首先要設置好用戶的姓名和郵箱(爲保證提交時提交者和作者信息的正確性):

$ git config --global user.name “xx”

$ git config --global user.email “[email protected]

然後進入工作目錄gitTest

$ cd gitTest

$ git init

創建版本庫,這時會發現工作目錄文件夾下多了一個.git的目錄

創建一個文件test.txt,內容爲hello world

$ git add test.txt 提交到版本庫之前需要進行的行爲

$ git commit -m “test1” 提交到版本庫,提交說明爲test1

[master(root-commit) 1e96add] test1

1files changed, 1 insertions(+), 0 deletions(-)

create mode 100644 test.txt

修改文件test.txt,添加一行hello

$ git diff可以看到修改後的文件與版本庫中的文件的差異

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

index 3b18e51..48d1c4e 100644

--- a/test.txt

+++ b/test.txt

@@ -1 +1,2 @@

 hello world

+hello

$ git status -s 查看文件狀態,-s表示查看精簡狀態

 M test.txt 注意:M前面有個空格,M處在第二個位置上

如果要提交修改的話:

git commit -m "add new line"
# 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:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

說明修改後不能直接提交,要先對文件執行git add命令,將修改的文件添加到暫存區,然後才能提交

$ git add test.txt

這時$ git diff看不到差異

$ git diff HEAD 或 $ git diff master 會發現有差異,HEAD爲當前版本庫的頭指針,master分支爲當前的工作分支

diff --git a/test.txt b/test.txt
index 3b18e51..48d1c4e 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 hello world
+hello

$ git status -s

M  test.txt這時M前面沒有空格,M處於第一個位置上

第一個位置代表版本庫中的文件與處於中間狀態(提交暫存區)中的文件相比有改動;第二個位置代表工作區當前的文件與處於中間狀態(提交暫存區)中的文件相比有改動。

若在test.txt中再添加一行world

$ git status -s

MM test.txt

$ git diff 工作區與暫存區的差異

diff --git a/test.txt b/test.txt
index 48d1c4e..ecd874d 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 hello world
 hello
+world

$ git diff master、$ git diff HEAD 工作區和版本庫的差異

diff --git a/test.txt b/test.txt
index 3b18e51..ecd874d 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,3 @@
 hello world
+hello
+world

$ git diff --cached 暫存區和版本庫的差異

diff --git a/test.txt b/test.txt
index 3b18e51..48d1c4e 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 hello world
+hello

這時如果直接提交的話,提交上去的test.txt只有兩行,因爲後面添加的world一行還未添加到暫存區

$ git commit -m "2 lines" 

[master 584d55d] 2 lines
 1 files changed, 1 insertions(+), 0 deletions(-)

$ git add test.txt

$ git commit -m "3 lines"

成功提交三行的test.txt文件

[master 2b6b6e3] 3 lines
 1 files changed, 1 insertions(+), 0 deletions(-)

修改test.txt,添加一行abc

$ git checkout -- test.txt

會發現新添加的行abc不見了,checkout會撤銷工作區中test.txt文件尚未提交的修改,若添加到暫存區,則不能撤銷。相當於取消自上次執行add以來的本地修改

$ git branch 可以查看分支

* master *號表示當前分支

$ git log 查看日誌

commit ec615c08858af2e90de2fe7ba1911a00011360d0
Author: xx <[email protected]>
Date:   Fri Aug 10 15:56:11 2012 +0800


    3 lines


commit 584d55d6eee909a33130a8528326e2998dc82450
Author: xx <[email protected]>
Date:   Fri Aug 10 15:53:46 2012 +0800


    2 lines


commit 1e96add73eef18106747d7127f3279ff950f262d
Author: xx <[email protected]>
Date:   Fri Aug 10 13:23:36 2012 +0800


    test1

一長串字母和數字的組合是提交ID,由40個十六進制的數字組成,是SHA1哈希值。這種方法使得發生衝突的機率變得很小。

$ git rev-parse HEAD 顯示引用對應的提交ID

ec615c08858af2e90de2fe7ba1911a00011360d0

$ git rev-parse HEAD^HEAD的父提交

584d55d6eee909a33130a8528326e2998dc82450

語法:master代表分支master中最新的提交;HEAD代表版本庫中最近的一次提交;^指代父提交;^2代表第二個父提交;~<n>可以指代祖先提交(父親的父親的...);ID^{tree}訪問提交對應的樹對象;ID:path/to/file訪問某一次提交對應的文件對象;:path/to/file訪問暫存區中的文件對象

$ git log --pretty=raw --graph通過提交對象之間的相互關聯,可以識別出一條跟蹤鏈,可以通過--graph參數看到

* commit ec615c08858af2e90de2fe7ba1911a00011360d0
| tree abb0df6cfbbab361ce0002af3fd29c0d7237fa9c
| parent 584d55d6eee909a33130a8528326e2998dc82450
| author xx <[email protected]> 1344585371 +0800
| committer xx <[email protected]> 1344585607 +0800

|     3 lines
|  
* commit 584d55d6eee909a33130a8528326e2998dc82450
| tree c1566beea63b0cc08e940916b5ae835d4ef86b92
| parent 1e96add73eef18106747d7127f3279ff950f262d
| author xx <[email protected]> 1344585226 +0800
| committer xx <[email protected]> 1344585226 +0800

|     2 lines
|  
* commit 1e96add73eef18106747d7127f3279ff950f262d
  tree c3b8bb102afeca86037d5b5dd89ceeb0090eae9d
  author xx <[email protected]> 1344576216 +0800
  committer xx <[email protected]> 1344576216 +0800
  
      test1

$ git cat-file ec615c查看此提交ID(在不衝突的前提下取至少4位)對應的信息,需要加參數 -t類型 -s大小 -p詳細信息

在原來基礎上再添加abc一行,add並commit,提交說明設爲"4 lines"

由日誌可以看到提交ID爲12e76d61f5c66af0a6c9e6302b2362bb36203bb1

$ git reset --hard HEAD^ 人爲修改遊標,將master重置到上一個老的提交上(--hard參數會替換引用指向、暫存區、工作區;--soft替換引用指向;--mixed即默認替換引用的指向、暫存區)

HEAD is now at ec615c0 3 lines

$ cat test.txt

hello world
hello
world

重置讓提交歷史也改變了

$ git log

commit ec615c08858af2e90de2fe7ba1911a00011360d0
Author: xx <[email protected]>
Date:   Fri Aug 10 15:56:11 2012 +0800


    3 lines


commit 584d55d6eee909a33130a8528326e2998dc82450
Author: xx <[email protected]>
Date:   Fri Aug 10 15:53:46 2012 +0800


    2 lines


commit 1e96add73eef18106747d7127f3279ff950f262d
Author: xx <[email protected]>
Date:   Fri Aug 10 13:23:36 2012 +0800


    test1

挽救錯誤的重置需要用reflog。

$ tail -5 .git/logs/refs/heads/master這個文件記錄了master分支指向的變遷,最新的改變追加到文件的末尾。

1e96add73eef18106747d7127f3279ff950f262d 584d55d6eee909a33130a8528326e2998dc82450 xx <[email protected]> 1344585226 +0800commit: 3 lines
584d55d6eee909a33130a8528326e2998dc82450 2b6b6e3554ddf8b79759294403b9c31c88bcf986 xx <[email protected]> 1344585371 +0800commit: 2 lines
2b6b6e3554ddf8b79759294403b9c31c88bcf986 ec615c08858af2e90de2fe7ba1911a00011360d0 xx <[email protected]> 1344585607 +0800commit (amend): 3 lines
ec615c08858af2e90de2fe7ba1911a00011360d0 12e76d61f5c66af0a6c9e6302b2362bb36203bb1 xx <[email protected]> 1344594757 +0800commit: 4 lines
12e76d61f5c66af0a6c9e6302b2362bb36203bb1 ec615c08858af2e90de2fe7ba1911a00011360d0 xx <[email protected]> 1344595145 +0800HEAD^: updating HEAD

$ git reflog show master | head -6 

ec615c0 master@{0}: HEAD^: updating HEAD
12e76d6 master@{1}: commit: 4 lines
ec615c0 master@{2}: commit (amend): 3 lines 
這個amend是當時不小心用了一個commit的--amend參數,現在暫時忽略這個amend
2b6b6e3 master@{3}: commit: 3 lines
584d55d master@{4}: commit: 2 lines
1e96add master@{5}: commit (initial): test1

從這裏可以看出消失的提交說明爲"4 lines"的提交又出現了

$ git reset --hard master@{1}

HEAD is now at 12e76d6 4 lines

$ cat test.txt

hello world
hello
world
abc

發現abc一行又回來了,提交歷史也回來了,恢復master的操作頁記錄在了日誌中。

$ git reflog show master | head -7

12e76d6 master@{0}: master@{1}: updating HEAD
ec615c0 master@{1}: HEAD^: updating HEAD
12e76d6 master@{2}: commit: 4 lines
ec615c0 master@{3}: commit (amend): 3 lines
2b6b6e3 master@{4}: commit: 3 lines
584d55d master@{5}: commit: 2 lines
1e96add master@{6}: commit (initial): test1

如果再向test.txt中添加一行abcd

$ git add test.txt但不commit

$ git reset HEAD test.txt相當於取消之前執行的git add命令,是用指定提交狀態(HEAD)下的文件test.txt替換掉暫存區中的文件

$ git diff會發現工作區和暫存區內容不一樣了,暫存區的內容和版本庫統一了

diff --git a/test.txt b/test.txt
index e0593e8..d76f881 100644
--- a/test.txt
+++ b/test.txt
@@ -2,3 +2,4 @@ hello world
 hello
 world
 abc
+abcd

$ git reset --hard HEAD 統一工作區、暫存區、版本庫的內容,爲下一步做準備

$ cat .git/HEAD查看當前HEAD的指向,指向master

ref: refs/heads/master

$ git branch -v

* master 12e76d6 4 lines

$ git checkout 12e76d6^檢出該ID的父提交

Note: checking out '12e76d6^'.

分離頭指針狀態,指的是頭指針指向了一個具體的提交ID,而不是一個引用(分支)
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.


If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:


  git checkout -b new_branch_name


HEAD is now at ec615c0... 3 lines

$ git reflog可以看到HEAD的指針被更改了

ec615c0 HEAD@{0}: checkout: moving from master to 12e76d6^
12e76d6 HEAD@{1}: master@{1}: updating HEAD
ec615c0 HEAD@{2}: HEAD^: updating HEAD
12e76d6 HEAD@{3}: commit: 4 lines
ec615c0 HEAD@{4}: commit (amend): 3 lines
2b6b6e3 HEAD@{5}: commit: 3 lines
584d55d HEAD@{6}: commit: 2 lines
1e96add HEAD@{7}: commit (initial): test1

$ git rev-parse HEAD master發現HEAD和master的指向不一樣了

ec615c08858af2e90de2fe7ba1911a00011360d0
12e76d61f5c66af0a6c9e6302b2362bb36203bb1

$ git branch現在不處於任何分支上

* (no branch)
  master

這時若修改文件並提交,切換到master分支上以後,改動會消失,此提交雖然暫時在版本庫中,但沒被任何分支跟蹤到,因此不能保證這個提交會永久存在。在文件中添加一行no branch

$ git add test.txt

$ git commit -m "no branch"

[detached HEAD 6014965] no branch
 1 files changed, 1 insertions(+), 0 deletions(-)

$ git checkout master切換到master分支上,發現改動消失了。若爲git checkout branch -- filename 則維持HEAD指向不變,用branch所指向的提交中的filename替換暫存區和工作區中相應的文件;git checkout -- .或git checkout .會取消所有本地相對於暫存區的修改。

$ git merge 6014965合併到當前分支

Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

$ cat test.txt

hello world
hello
world
<<<<<<< HEAD
abc
=======
no branch
>>>>>>> 6014965

需要修改文件以解決衝突並add、commit,才能合併

$ git log --graph --pretty=oneline 會發現出現了不一樣的分支

*   29744ad4a7688770729de3c7c7cf00895025a89e to merge
|\  
| * 60149653b9d2488e423782a7ae5cdf710e1cd5cb no branch
* | 12e76d61f5c66af0a6c9e6302b2362bb36203bb1 4 lines
|/  
* ec615c08858af2e90de2fe7ba1911a00011360d0 3 lines
* 584d55d6eee909a33130a8528326e2998dc82450 2 lines
* 1e96add73eef18106747d7127f3279ff950f262d test1

最新提交會有兩個父提交

新建test2.txt文件並add、commit,用於下文要介紹的刪除命令。

若直接在工作區中刪除文件,則對暫存區和版本庫均不起作用。

$ git rm test2.txt 則此文件在工作區和暫存區被刪除,更改名字也類似,git mv命令

$ git commit -m "use git rm to delete file"

[master 756e9d5] use git rm to delete file
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test2.txt

新建test3.txt文件並add、commit,用於另一個刪除方法。

首先在本地刪除test3.txt

$ git status -s

 D test3.txt

$ git add -u將本地有改動(包括修改和刪除)的文件標記到暫存區,-A則是把工作區的所有改動及新增文件添加到暫存區。-i提供交互式界面,可以進行選擇性添加

$ git status -s

D  test3.txt

$ git commit -m "delete file test3.txt"

[master 8ce4b0e] delete file test3.txt
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test3.txt

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