Git 使用


官方網站:http://git-scm.com/

配置

jackluo@jackluo:~$ git config --global user.name "jackluo"

jackluo@jackluo:~$ git config --global user.email "net.webjoy@gmail.com"

jackluo@jackluo:~$ git config --global color.ui true

jackluo@jackluo:~$ git config --global color.ui true

jackluo@jackluo:~$ git config --global core.editor "vi"

從頭建立Repository(Demo):

$ mkdir sandbox

$   cd sandbox

$   git init

第一次 commit (demo):

$   touch README

$   git add README

$   git status

$  git commit -m "First Commit"

修改看看Demo

編輯README 做些變更

$  git status

$  git diff

$  git add .

(一次加入所有變更跟新增檔案,但不包括刪除的檔案)

$  git status

$  git diff --cached

$  git commit -m "Update README"

只Commit 部分檔案(demo)

$   touch a.rb

$   touch b.rb

$   git add a.rb

$   git commit "update a"

$   git add b.rb

$   git commit "update b"

修改 a.rb 

$  git add a.rb

再次修改 a.rb

git commit -m "commit a"

這時commit 的內容是第一次修改當時的內容而已

只commit 同一檔案部分內容(demo)

$  git add --patch

    -y 加到staging

    -n 不要加到staging

    -s 查以折小一點hunk

或者用 GUI 例如gitx 來選取

刪除和搬移檔案(demo)

$ git rm a.rb

$ git mv b.rb c.rb

$ git add .

$ git commit "Remove a ,Rename b to c"

沒有copy ?因為Git 是追跟內容,你只要cp 即可,不用擔心浪費空間.

revert (還原 commit 記錄)

新增一筆 commit 來做還原

例如本來的commit 是新增一行,那么revert commit 就會移除那一行

$  git revert

$  git revert HEAD^

下標簽(demo)

$ git tag foo

$ git tag foo <SHAI>

$ git tag bar -m "some message"

$ git tag 

$ git tag -d foo

$ git log 

$ git log --oneline

$ git log --oneline --decorate --graph

git log 很多參數可以用,可以用GUI

比較差異Diff

git diff <SHAI>拿 working ree 比較

砍掉 untracked 檔案

git clean -n 列出打算要清除的檔案

git clean -f 真的清除

git clean -x 連 gitignore 里列的檔案也要清掉

Git 建立Local Repository

  1. $ mkdir project; cd project
  2. $ git init
  3. $ echo "hello" > hello.txt
  4. $ git add .
  5. $ git commit -m 'initial'

Git clone 資料, 資料修改后上傳

  1. $ git clone http://git.example.com/project.git
  2. $ cd project
  3. $ touch new_file.txt
  4. $ git add .
  5. $ git commit -m 'add new_file.txt'
  6. $ git push origin master
  7. $ git pull # 拉看看有沒有更新

Git clone 資料, 資料修改后上傳.(分兩個目錄測試)

  1. $ mkdir /tmp/a /tmp/b
  2. $ cd /tmp/a
  3. $ git clone http://example.com/project_name.git
  4. $ cd /tmp/b
  5. $ git clone http://example.com/project_name.git
  6. $ echo "hello" > hello.html
  7. $ git add hello.html
  8. $ git commit -m 'add hello.html' # local commit.
  9. $ git push # 推到Server 上.
  10. $ cd /tmp/a
  11. $ git pull # 會看到hello.html

Local Repository

建立Local Repository 測試
  1. $ mkdir test; cd test
  2. $ git init
建立新的branch (new-branch), 並於branch 去新增檔案
  1. $ git branch new-branch # master
  2. $ git branch # master 
     * master 
       new-branch
  3. $ git checkout new-branch # 切換到新的branch
  4. $ git branch # new-branch 
       master 
     * new-branch
測試Git staging area (git add . 之后的修改, 不會被commit 進去)
  1. $ touch new-branch_file.txt # new-branch
  2. $ git add . # new-branch
  3. $ echo "contents." > new-branch_file.txt # new-branch
  4. $ git commit # new-branch, commit的new-branch_file.txt會是空的,因為修改是在git add .之后.
修改過的資料, 不要commit, 想直接切換到master(使用Git stash 將修改紀錄暫存, 將目前new-branch 的資料merge 到mastermaster)
  1. $ git status # new-branch, 會顯示new-branch_file.txt 有修改, 尚未commit.
  2. $ git checkout master # new-branch, 切換回master, 會出現錯誤: "You have local changes"
  3. $ git stash # new-branch, 先把修改的先暫存下來, 先不commit, 之后取出可用git stash pop 或git stash apply
  4. $ git status # new-branch, 會顯示nothing to commit (暫時先不丟進commit 里面)
  5. $ git checkout master # master
  6. $ git merge new-branch # master, 會將new-branch_file.txt 的空檔案合並進來.
Git 於master 將檔案砍掉, branch 是否還能存取此檔案.
  1. $ ls # master, master_file.txt, new-branch_file.txt
  2. $ rm new-branch_file.txt # master, 刪掉此檔案
  3. $ git checkout new-branch # master, 切換到new-branch, 會出現錯誤: "pathspec 'branch' did not match any file(s) known to git."
  4. $ git stash # 先把修改的部份存起來(砍掉new-branch_file.txt)
  5. $ ls # master, 此時new-branch_file.txt 出現了. (因為尚未commit, stash 的動作並未做寫入)
  6. $ git stash pop # master, 回復剛剛砍掉的狀態, new-branch_file.txt 就消失了.
  7. $ git commit -m 'delete new-branch_file.txt in master' -a # 先砍掉.
切換到Branch, 去跟master 做Merge
  1. $ git checkout new-branch
  2. $ git stash pop # new-branch
  3. $ git merge master # new-branch, 錯誤: "Entry 'new-branch_file.txt' not uptodate. Cannot merge.", 因為檔案有修改.
  4. $ git diff master # 與master 做diff, 發現/dev/null vs file, 所以要把此檔案砍掉.
  5. $ rm new-branch_file.txt
  6. $ git merge master # new-branch, 合並完成
  7. $ ls # new-branch, 只剩master_file.txt 這個檔案
由branch(new-branch) 環境和Master 分別建立新的branch (from-branch, from-master), 並測試未commit 資料狀況, 新branch 的狀態.
  1. $ touch new-branch_file.txt # new-branch, 測試未commit 資料狀況, 新branch 的狀態.
  2. $ git branch from-branch new-branch # new-branch, 會將new-branch 目前所有狀態和資料都復制過去
  3. $ git checkout from-branch # from-branch
  4. $ git status # from-branch, 會看到new-branch_file.txt, 且這個檔案尚未commit.
  5. $ git branch from-master master # from-branch, 依照master 開from-master 的branch
  6. $ git branch # from-branch 
      from-branch 
      from-master 
      master 
    * new-branch
  7. $ git branch -d from-master # from-branch, 砍掉from-master 的branch
  8. $ git checkout -b from-master master # from-branch, 建立from-master 的branch, 並同時切換過去.
  9. $ git branch # from-master 
      from-branch 
    * from-master 
      master 
      new-branch
測試由Repository 還原檔案內容
  1. $ echo "test" > master_file.txt
  2. $ git checkout master_file.txt # 還原回空檔案(Repository 的版本是空檔案)
git pull 出現error: Entry 'filename' not uptodate. Cannot merge. 解法
  1. git stash # 目前目錄有修改的資料, 先丟進暫存區
  2. git pull # 合並拉下來的修改
  3. git stash pop # 將修改的暫存區資料取出
  4. 去看unmerge 的部份, 修改完成commit + push 即可.

Repository 測試

建立local 端master
  1. $ mkdir /tmp/a /tmp/b
  2. $ cd /tmp/a
  3. $ git clone http://git.example.com/project.git
  4. $ cd project/
  5. $ touch master-file
  6. $ git add .
  7. $ git commit -m 'add master-file'
  8. $ git push origin master
  9. $ git pull
  10. $ cd /tmp/b # 由此處來建立branch
  11. $ git clone http://git.example.com/project.git
建立Repository 的branch
  1. $ git pull
  2. $ git push origin origin:refs/heads/reps-branch
  3. $ git fetch origin # 更新到最新版本(origin 是Repository 的版本)
  4. $ git branch -r
  5. $ git checkout --track -b reps-branch origin/reps-branch # 抓取reps-branch, 並將此branch 建立於local 的reps-branch
  6. $ git pull
  7. $ git branch 
     * reps-branch 
       master

測試

A 操作, 新增一個檔案, commit 進入reps-branch, 於reps-branch commit
  1. $ cd /tmp/a/project
  2. $ git pull
  3. $ git push origin origin:refs/heads/reps-branch
  4. $ git fetch origin
  5. $ git branch -r
  6. $ git checkout --track -b reps-branch origin/reps-branch # 抓取reps-branch, 並將此branch 建立於local 的reps-branch
  7. $ git pull
  8. $ git branch 
     * reps-branch 
       master
  9. $ touch reps-branch.txt
  10. $ git add reps-branch.txt
  11. $ git commit -m 'add reps-branch.txt'
  12. $ git push
  13. $ git pull
B 抓取reps-branch, 並修改資料, 再抓取reps 的branch
  1. $ cd /tmp/b/project
  2. $ git clone http://git.example.com/project.git
  3. $ cd project
  4. $ git fetch origin
  5. $ git pull
  6. $ git checkout --track -b reps-branch origin/reps-branch # 丟到reps-branch 去
  7. $ vim reps-branch.txt # 隨便加些內容
  8. $ git add reps-branch.txt
  9. $ git commit -m 'add some content'
  10. $ git push
  11. $ git pull
A 操作, 更新, 會抓到B commit 的資料(於reps-branch)
    1. $ cd /tmp/a/project
    2. $ git pull # 更新reps-branch.txt 內的資料(B commit 的)

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM