1. 程式人生 > >Git入門及其workflow

Git入門及其workflow

Git的基本使用方法:

PART1  Basic

1  >> git init

用來初始化。建立一個git倉庫來管理檔案。注意這個命令可以在不同資料夾下建立,但是在子檔案或者父檔案下提交的規則會有所不同。

在父檔案中提交commit之前要在子檔案提交commit

2  >> git statusus

用來檢視當前狀態,就像linux裡面用>> ls一樣

3  >> git add

用來新增檔案,新增單個檔案就是

>> git add <filename>

當然也可以新增多個檔案,只需要這些檔案之間用空格隔開就可以了。

>> git add <filename1> <filename2>

用來新增所有改變的檔案就用

>> git add .

4  >> git diff

首先要用>> git add 追蹤了某個檔案,然後再用git diff可以顯示出不同的地方。

還可以用

>> git diff <file1> <file2>

來進行對比,其中file可以是origin, master, branch.

也可以是兩個commit的雜湊。

5  >> git commit

在新增檔案後用來提交,注意這裡提交後相當於改變了本地的儲存。和提交到遠端倉庫比如github是不一樣的。

一般的用法是

>> git commit -m “comment here” 

6  >> git log

最後是用git log檢視自己提交的一些詳情,而且還會顯示每次提交的一個雜湊值,通過這個值可以進行回退。

7  >> git show HEAD

HEAD commit一般來說是最近一次提交的commit,通過這條命令可以顯示最近一次提交。

8  >> git checkout HEAD <filename>

Checkout結合HEAD(HEAD是最近一次提交)很有用,比如在一次迭代測試中,感覺自己哪裡錯了或者刪除錯了哪次檔案,就可以回退到上一次的狀態。當然,我們更喜歡回退到某一個具體的狀態,這樣就需要將HEAD變為具體一次commit的雜湊的前六位。注意恢復某一次的檔案並不會把那一次commit沒有的檔案刪掉,但是會恢復那一次commit中存在的檔案。

注意這裡<filename>可以用  . 來代替,這樣就可以恢復那一次的所有檔案。

PART2 Teamwork

>> git clone <remote_location> <clone_name>

<remote_location>可以是github給出的web網址,也可以是一個檔案的路徑。也就是說可以在本地操作git複製專案。

>> git remote -v

這也是展示資訊,顯示遠端伺服器的資訊。這裡的v是指verbose,詳細版本的意思。

注意這要在clone了來自遠端伺服器或者某個路徑的時候用

>> git fetch

這也是在有remote資訊下進行,這個操作會把遠端伺服器更新的檔案取到本地,但是不會馬上改變本地檔案,而是會在一個origin/master的branch。注意當git status提示你behind origin/master就說明遠端伺服器有更新。

如果再fetch完之後,想要把本地的檔案替換成遠端伺服器更新後的內容,就需要用到merge了。

>> git merge origin/master

意思就是把origin/master這個branch融合到主branch中。

注意在merge後不可回退了,整個git log就變成了遠端伺服器的git log了。

git merge失敗的解決方法

如果本地有修改,想要fetch遠端伺服器的程式碼時,可能會報錯

error: Your local changes to the following files would be overwritten by merge:

這時候有兩種解決方法:

一種是保留原生代碼的同時並且把遠端伺服器的程式碼merge到本地

>> git stash

>> git merge origin/master

>> git stash pop

第二種方法是完全覆蓋,也可以先branch出一個副本,再讓遠端伺服器的檔案完全覆蓋

完全覆蓋使用

>> git reset --hard

>> git merge origin/master

----

>> git branch <branch_name>

顯示branch,知道了不同branch的名字,可以用

>> git checkout <branch_name><branch_name>

來切換branch,切換不同branch提交之後,再切換會master會改變資料夾的內容。

注意如果不提交commit,再切換回master可能會出現錯誤。所以一次只操作一次,如果修改了branch,記得一定要提交。

>> git push <origin> <your_branch_name>

把你建立的分支融合到遠端地址(origin)。注意如果你把你的master而不是分支push上去,在github中,如果你是賬號的使用者,會直接修改master。也就是是說除非你是那個賬號的使用者,否則你是不能直接修改master的。

Git workflow

Now that you've merged  origin/master  into your local  master  branch, you're ready to contribute some work of your own. The workflow for Git collaborations typically follows this order:

  1. Fetch and merge changes from the remote
  2. Create a branch to work on a new project feature
  3. Develop the feature on your branch and commit your work
  4. Fetch and merge from the remote again (in case new commits were made while you were working)
  5. Push your branch up to the remote for review

Steps 1 and 4 are a safeguard against merge conflicts, which occur when two branches contain file changes that cannot be merged with the git merge command. Step 5 involves git push, a command you will learn in the next exercise

注意git裡面的origin是fetch取過來的,是可以離線存在的。

意思是說離線狀態可以使用

>> git diff master origin

或者說git status提示超前或落後的origin離線儲存下來的。