Note: Git Command 須在 repository 目錄下操作, Ctrl+C, 退出指令.
1、版本庫(repository)操作
版本庫又名倉庫, 此處可以簡單理解成一個目錄,在這個目錄裏面,每個文件的修改、刪除,Git都可以跟蹤管理。
版本庫的上級目錄稱爲工作目錄,只有工作目錄中的文件才能保存到版本庫中。
key word |
command |
function |
init |
git init |
創建版本庫,創建成功,會在此目錄下生成一個.git的隱藏目錄 |
remote |
git remote add origin [url for the repository] |
本地創建的Git管理項目關聯到GitHub上某個Repository |
clone |
git clone [url for the repository] |
從Github獲取項目 |
2、分支(branch)操作
key word |
command |
function |
status |
git status |
查看當前分支處於哪個分支,有哪些變動 |
|
git status -s |
簡單模式查看本地庫/暫存區/工作目錄的文件變動 |
reset |
git reset --hard origin/master |
將本地分支的HEAD切回遠程分支master最新的version |
branch |
git branch |
查看本地所有分支,當前分支前標有* |
|
git branch -r |
查看遠程所有分支 |
|
git branch -a |
查看本地及遠程所有分支,當前分支前標有* |
|
git branch [branch] |
創建本地分支 |
|
git branch -m [old branch] [new branch] |
重命名已merge的分支 |
|
git branch -M [old branch] [new branch] |
強制重命名分支 |
|
git branch -d [branch] |
刪除已merge的分支 |
|
git branch -D [branch] |
強制刪除,適用於本地分支存在未提交或者未merge的改動 |
diff |
git diff |
對比當前分支緩衝區與暫存區 |
|
git diff --cached |
對比當前分支暫存區文件及其更新內容 |
|
git diff [version 1] [version 2] |
對比當前分支兩個版本哪些文件更新了哪些內容 |
|
git diff [branch 1] [branch 2] |
對比[branch 1]和[branch 2],哪些文件有更新,更新了哪些內容,選定分支可以是本地也可以是遠程分支 |
|
git diff [branch 1] [branch 2] --stat |
對比[branch 1]和[branch 2],哪些文件有更新,只展示文件名,不展示文件內容的差異 |
merge |
git merge [branch] |
合併[branch]到當前分支 |
checkout |
git checkout [branch] |
切換到[branch] |
|
git checkout -b [branch] |
創建並切換到[branch] |
|
git checkout -b [local branch] origin/[remote branch] |
創建本地分支[local branch] 並與遠程分支 [remote branch] 建立映射關係 |
commit |
git commit -m [commit message] |
提交代碼到本地倉庫 |
fetch |
git fetch |
將本地branch,遠程branch,更新code到最新狀態 |
|
git fetch -p |
更新本地遠程分支,刪除遠程分支中已被刪除的branch |
pull |
git pull |
獲取遠程分支最新code, 同步到當前分支 |
|
git pull origin [branch] |
獲取指定遠程分支[branch]最新code, 同步到當前分支 command == git fetch + git merge origin [branch] |
push |
git push origin [local branch]:[remote branch] |
推送本地到遠程倉庫, push的一般形式 |
|
git push origin [local branch] |
省略遠程分支名時,表示將本地分支[local branch]推送到與之存在追蹤關係的遠程分支上【通常情況下,兩者同名】,當這個遠程分支不存在時,則會被新建 |
|
git push origin :[remote branch] |
省略本地分支名時,表示刪除指定的遠程分支,因爲此時等同於推動一個空的本地分支到此遠程分支[remote branch], command == git push origin --delete [remote branch] |
3、文件(file)操作
file支持任意格式,.xml,.json, .java, .groovy ...
key word |
command |
function |
cat |
cat [file] |
查看[file]內容 |
add |
git add [file] |
提交指定的修改後的文件[file]到暫存區 |
|
git add -all |
提交所有修改後的文件到暫存區 |
commit |
git commit [file] -m [message] |
提交修改後的文件到本地倉庫 |
reset |
git reset --cached [file] |
從緩存區移除文件,將其變爲未跟蹤的狀態 |
diff |
git diff [file] |
對比尚未暫存的[file]更新了哪些內容,退出按q鍵 |
|
git diff --cached [file] |
對比暫存區的[file]更新了哪些內容,退出按q鍵 |
|
git diff [version1:file] [version2:file] |
對比[file]兩個版本的不同,退出按q鍵 |
checkout |
git checkout --[file] |
恢復[file]到最後一次添加到暫存區的狀態 |
vim |
vim [file] |
編輯[file] |