2013年9月18日 星期三

Git 簡介、常用指令

Git是用於Linux核心開發的版本控制工具。與CVSSubversion一類的集中式版本控制工具不同,它採用了分布式版本庫的作法,不需要伺服器端軟體,就可以運作版本控制,使得原始碼的發布和交流極其方便。Git的速度很快,這對於諸如Linux kernel這樣的大項目來說自然很重要。Git最為出色的是它的合併追蹤(merge tracing)能力。



常用指令


git clone {account}@{host}:{project}.git

git add
git add .
將資料先暫存到 staging area, add 之後再新增的資料,
於此次 commit 不會含在裡面.
git add filename
git add modify-file
修改過的檔案, 也要 add. (不然 commit 要加上 -a 的參數)
git add -u
只加修改過的檔案, 新增的檔案不加入.
git add -i
進入互動模式
git rm filename
刪除檔案
git mv filename new-filename
修改檔名、搬移目錄
git status
看目前的狀態

git tag
git tag
列出既有 tag
git tag -l "XX.*"
搜尋XX.*

git reset
git reset xxxx --hard
強制恢復到某一版本
git reset --soft HEAD^
執行 git commit,發現訊息寫錯想要修改,可以使用。
       會刪掉 commit,在執行一次 commit 就可。

git branch & git checkout
local 端產生新的 branch
git branch
列出目前有多少 branch
    git branch –r
列出所有 Repository branch
    git branch –a
列出所有 branch
git branch new-branch
產生新的 branch (名稱: new-branch), 若沒有特別指定,
會由目前所在的 branch / master 直接複製一份
git branch new-branch master
master 產生新的 branch(new-branch)
git branch new-branch v1
tag(v1) 產生新的 branch(new-branch)
git branch -d new-branch
刪除 new-branch
git branch -D new-branch
強制刪除 new-branch
git checkout -b new-branch test
產生新的 branch, 並同時切換過去 new-branch

local 端切換 branch
git checkout branch-name
切換到 branch-name
git checkout -b new-branch test
產生新的 branch, 並同時切換過去 new-branch
git checkout filename
還原檔案到 Repository 狀態
git checkout HEAD
將所有檔案都還原到上一版(最後一次 commit 的版本).
(git checkout -f 亦可)
git checkout xxxx
將所有檔案都還原到 xxxx commit 的版本
git checkout -- *
恢復到上一次 Commit 的狀態(*改成檔名, 就可以只恢復那個檔案)

local New Repository
git init
git add .
git commit -m "init"

git remote add origin git@10.1.100.xx:{Repository Name}.git
# Creates a remote named "origin" pointing at your GitHub repository

如何在 remote site 增加/移除一個 branch
    git push origin origin:refs/heads/branch_name
如果 branch_name 不存在的話
git push origin local_branch_name:remote_branch_name
如果 branch 已經在 local 了,只是要推上去
Or
git push origin HEAD
如果 branch 已經在 local 了,只是要推上去
加入branch對應設定
vim .git/config

如何移除 remote site branch
git push origin :heads/branch_name
branch_name 是你想要移除的 remote branch 名稱

移除 remote site branch 描述資料,branch –a 時就不會再列出
git remote prun origin

開始追蹤 remote branch (當你下一次 pull, 你會對那個新的 branch_name sync)
    git checkout --track -b branch_name origin/branch_name

git commit
     git commit -m "註解" -a
     git commit -F commit.txt -a

git pull & push
git push origin master –tags
master 是你想要推上的 branch, --tags 若有 tag 一起推上.
git pull origin master
master 是你想要 sync branch.
git blame
git blame
git blame hello.c

是一個看誰在哪一版修改了什麼的工具。

沒有留言:

張貼留言