#在當前目錄新建一個 git 代碼庫 $ git init #新建一個目錄,將其初始化爲 git 代碼庫 $ git init [project-name] #下載一個項目和它的整個代碼歷史 $ git clone [url]
git 的設置文件爲 .gitconfig
,它能夠在用戶主目錄下(全局配置),也能夠在項目目錄下(項目配置)git
#顯示當前 git 配置 $ git config --list #編輯 git 配置文件 $ git config -e [--global] #設置提交代碼時的用戶信息 $ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
#添加指定文件到暫存區 $ git add [file1] [file2] ... #添加指定目錄到暫存區,包括子目錄 $ git add [dir] #添加當前目錄的全部文件到暫存區 $ git add . #刪除工做區文件,而且將此次刪除放入到暫存區 $ git rm [file1] [file2] ... #中止追蹤指定文件,但該文件會保留在工做區 $ git rm --cached [file] #更名文件,並將這個更名放入暫存區 $ git mv [file-original] [file-renamed]
#提交暫存區到倉庫區 $ git commit -m [message] #提交暫存區的指定文件到倉庫區 $ git commit [file1] [file2] ... -m [message] #提交工做區自上次 commit 以後的變化,直接到倉庫區 $ git commit -a #提交時顯示全部 diff 信息 $ git commit -v #使用一次新的 commit,替代上一次提交 #若是代碼沒有變化,則用來改寫上一次的 commit 的提交信息 $ git commit --amend -m [message] #重作上一次 commit, 幷包括指定文件的新變化 $ git commit --amend [file1] [file2] ...
#列出全部本地分支 $ git branch #列出全部遠程分支 $ git branch -r #列出全部本地分支和遠程分支 $ git branch -a #新建一個分支,但依然停留在當前分支 $ git branch [branch-name] # 將原有分支名稱 old branch name 修改成 new name git branch -m <old branch name> <new name> # 修改當前分支名稱爲 new name git branch -m <new name> #新建一個分支,並切換到當該分支 $ git checkout -b [branch-name] #新建一個分支,指向指定 commit $ git branch [branch] [commit] #新建一個分支,與指定的遠程分支創建追蹤關係 $ git branch --track [branch] [remote-branch] #切換到指定分支,並更新工做區 $ git checkout [branch-name] #創建追蹤關係,在現有分支與指定的遠程分支以前 $ git branch --set-upstream [branch] [remote-branch] #合併指定分支到當前分支 $ git merge [branch] #選擇一個 commit,合併進當前分支 $ git cherry-pick [commit] #刪除分支 $ git branch -d [branch-name] #刪除遠程分支 $ git push origin --delete [branch-name] #刪除與遠程分支關聯 $ git branch -dr [remote/branch] #刪除遠程分支 2 $ git branch -r -d origin/[branch-name] $ git push origin :[branch-name]
#列出全部 tag $ git tag #在當前 commit,新建一個 tag $ git tag [tag] #在指定 commit,新建一個 tag $ git tag [tag] [commit] #刪除本地 tag $ git tag -d [tag] #刪除遠程 tag $ git push origin :refs/tags/[tagName] #查看 tag 信息 $ git show [tag] #提交指定的 tag $ git push [remote] [tag] #提交全部 tag $ git push [remote] --tags #新建一個分支,指向某個 tag $ git checkout -b [branch] [tag] #重命名 tag $ git tag -f [new-tagName] [old-tagName] $ git tag -d [old-tagName] #將本地 tag 推送到遠程 $ git push origin :refs/tags/[old-tagName] $ git push --tags # 拉取 tag git fatch origin tag tag_name
#顯示全部變動的文件 $ git status #顯示當前分支的版本歷史 $ git log #顯示 commit 歷史,以及每次 commit 發生變動的文件 $ git log --stat #顯示某個 commit 以後的全部變更,每一個 commit 佔據一行 $ git log [tag] HEAD --pretty=format:%s #顯示某個 commit 以後的全部變更,其「提交說明」必須符合條件 $ git log [tag] HEAD --grep feature #顯示某個文件的版本歷史,包括文件更名 $ git log --follow [file] $ git whatchanged [file] #顯示指定文件相關的每一次 diff $ git log -p [file] #顯示指定文件是什麼人在什麼時間修改過 $ git blame [file] #顯示暫存區與工做區的差別 $ git diff #顯示暫存區和上一個 commit 的差別 $ git diff --cached [file] #顯示工做區與當前分支最新 commit 之間的差別 $ git diff HEAD #顯示兩次提交之間的差別 $ git diff [first-branch] ... [second-branch] #顯示某次提交的元數據和內容變化 $ git show [commit] #顯示某次提交發生的變化的文件 $ git show --name-only [commit] #顯示某次提交時,某個文件的內容 $ git show [commit]:[filename] #顯示當前分支的最近幾回提交 $ git reflog
#下載運程倉庫的全部變更 $ git fetch [remote] #顯示全部遠程分支 $ git remote -v #顯示某個遠程倉庫的信息 $ git remote show [remote] #增長一個新的遠程倉庫,並命名 $ git remote add [shortname] [url] #取回遠程倉庫的變化,並與本地分支合併 $ git pull [remote] [branch] #上傳本地指定分支到遠程倉庫 $ git push [remote] [branch] #強行推送當前分支到遠程倉庫,即便有衝突 $ git push [remote] --force #推送全部分支到遠程倉庫 $ git push [remote] --all
# 先刪除遠程分支地址 $ git remote rm origin # 而後從新增長遠程分支地址 $ git remote add origin [url]
#恢復暫存區的指定文件到工做區 $ git checkout [file] #恢復某個 commit 的指定文件到工做區 $ git checkout [commit] [file] #恢復上一個 commit 的全部文件到工做區 $ git checkout . #重置暫存區的指定文件,與上一次 commit 保持一致,但工做區不變 $ git reset [file] #重置暫存區與工做區,與上一次 commit 保持一致 $ git reset --hard #重置當前分支的指針爲指定 commit,同時重置暫存區,但工做區不變 $ git reset [commit] #重置當前分支的 HEAD 爲指定 commit,同時重置暫存區和工做區,與指定 commit 一致 $ git reset --hard [commit] #重置當前 HEAD 爲指定 commit,但保持暫存區和工做區不變 $ git reset --keep [commit] #新建一個 commit, 用來撤銷指定 commit #後者的全部變化都將被前者抵消,而且應用到當前分支 $ git revert [commit]
#生成一個可供發佈的壓縮包 $ git archive
# 想要切換分支,可是還不想要提交以前的工做,能夠儲存修改信息,將新的儲藏推送到棧上 $ git stash / git stash save # 在這時,可以輕易的切換分支並在其餘地方工做,你的修改被存儲在棧上。要查看儲藏的東西,可使用 git stash list $ git stash list # 能夠將剛剛的儲藏從新加載回來 $ git stash apply # 也能夠經過儲藏的序號進行加載 $ git stash apply stash@{1}
# 從每個提交移除一個文件:指 git add . 的內容完整的上傳到倉庫,可是當但願開源這個內容的時候,須要移除一些無用的文件,--tre-filter 選項在的每個提交後,運行指定的命令,而後從新提交結果。 $ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD # 使一個子目錄做爲新的根目錄:假設已經從另外一個源代碼控制系統中導入,而且有幾個沒意義的子目錄(trunk/tags 等等)。若是想要讓 trunk 子目錄做爲每個提交的新的項目根目錄,filter-branch 也能夠幫助你那麼作,再在新項目根目錄是 trunk 子目錄且 Git 會自動移除全部不影響子目錄的提交。 $ git filter-branch --subdirectory-filter trunk HEAD # 在開始工做時忘記運行 git config 來設置你的名字與郵箱地址,或者你想要開源一個項目,而且修改全部你的工做郵箱地址爲你的我的郵箱地址。任何情形下,你也能夠經過 filter-branch 來一次性修改多個提交中的郵箱地址。須要當心的是隻修改你本身的郵箱地址,因此使用 --commit-filter 來修改: $ git filter-branch --commit-filter ' if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ]; then GIT_AUTHOR_NAME = "scott Chacon"; GIT_AUTHOR_EMAIL = "schacon@example.com"; git commit-tree "$@"; else git commit-tree "$@"; fi' HEAD
文章首發於:https://www.zucchiniy.cnshell