之前寫個一個git小結,可是實際上並不夠用。因而結合實際工做上碰到的一些狀況,參考了一些資料,從新總結了一下。目標是在平常工做中不用再去查閱其餘的資料了,若是有什麼遺漏或者錯誤的地方,請評論指出!html
Workspace:工做區git
Index / Stage:暫存區github
Repository:倉庫區(或本地倉庫)正則表達式
Remote:遠程倉庫shell
# 在當前目錄 git init
# 顯示當前的Git配置 git config –list # 編輯Git配置文件 git config -e [–global] # 設置提交代碼時的用戶信息 git config [–global] user.name "example" git config [–global] user.email "example@gmail.com" # 配置自動換行,提交到git時自動將換行符轉換爲lf git config --global core.autocrlf input # 配置密鑰 ssh-keygen -t rsa -C example@gmail.com # 生成密鑰 ssh -T git@github.com # 測試是否成功 # 配置別名,--global 表示全局配置 git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 建立一個本地倉庫的克隆版本: git clone /path/to/repository # 若是是遠端服務器上的倉庫: git clone username@host:/path/to/repository # 克隆到自定義文件夾: git clone username@host:/path/to/repository my-cloned-repo
# 初始化 git init # 獲取狀態 git status git add README.md git commit -m "message" # 鏈接遠程倉庫 git remote add origin git@github.com:example/test.git # 推送內容到遠程倉庫的同時設置默認跟蹤分支 git push -u origin master
vim .gitignore !爲模式取反 *.a !lib.a
# 添加指定文件到暫存區 git add [file1] [file2] ... # 添加指定目錄到暫存區,包括子目錄 git add [dir] # 添加當前目錄的全部文件到暫存區,.或*表明所有添加 git add . # 添加每一個變化前,都會要求確認 # 對於同一個文件的多處變化,能夠實現分次提交 git add -p # 刪除工做區文件,而且將此次刪除放入暫存區 git rm [file1] [file2] ... # 中止追蹤指定文件,但該文件會保留在工做區 git rm --cached [file] # 文件重命名,並加入暫存區 git mv [file-original] [file-renamed] # 通配符批量移動 git mv *.html src/
git commit -m "message" # 補提交文件,提交時漏掉了某些文件時不該該再單獨提交一次 git commit --amend # 覆蓋提交日期,不知道有啥實際用途 git commit -m "message" --date "2017-01-01 00:00:00"
# 列出全部本地分支 git branch # 新建一個分支,但依然停留在當前分支 git branch [branch-name] # 新建一個分支,並切換到該分支 git checkout -b [branch] # 切換到指定分支,並更新工做區 git checkout [branch-name] # 切換到上一個分支 git checkout - # 創建追蹤關係,在現有分支與指定的遠程分支之間 git branch --set-upstream [branch] [remote-branch] # 根據一個特定的提交建立新分支,忘記開新的分支就修改並提交了代碼時的處理 git branch test-branch HEAD~1 # 刪除分支 git branch -d [branch-name] # 重命名分支 git branch -m oldBranch newBranch # 推送分支到遠程倉庫 git push origin [branch-name] # 刪除遠程分支 $ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
# 列出全部tag git tag # 顯示 tag list 和註解 git tag -n # 在當前commit新建一個輕標籤 git tag [tagname] # 在指定commit新建一個tag git tag [tagname] [commit] # 添加註解標籤 git tag -a [tagname] # 添加註解標籤並添加註解 git tag -am "message" [tagname] # 刪除本地tag git tag -d [tagname] # 刪除遠程tag git push origin :refs/tags/[tagName] # 推送全部tag git push --tags # 新建一個分支,指向某個tag git checkout -b [branch] [tagname] # 切換到tag git checkout [tagname] # tag和分支同名時,顯示指定切換到tag git checkout tags/[tagname]
git remote # 查看遠程倉庫的 URL git remote -v # 添加遠程倉庫 git remote add origin git@github.com:example/test.git # 刪除遠程倉庫 git remote rm origin # 修改遠程倉庫地址 git remote set-url origin [url] # 將本地的遠端和遠端進行同步 git fetch origin # 將本地的遠端合併到本地分支 git merge origin/master # 拉取遠程倉庫,這至關於上面兩條命令 git pull origin master # 使用rebase可使提交的歷史記錄顯得更簡潔 git rebase mater # 也能夠指定分支: git pull origin remote:local # 推送: git push origin local:remote
# 摘出某個提交 git cherry-pick [hash] # 交互式提交,當涉及提交修改時使用,如 squash、調整 commit 順序等 git rebase -i git rebase -i HEAD~4 # 將 upstream 後的 commit 節點嫁接到 newbase,若是有 branch ,會先 checkout 到這個 branch,再 rebase git rebase --onto <newbase> <upstream> <branch> # 刪除 topicA~三、topicA~4 這兩個 commit git rebase --onto topicA~5 topicA~3 topicA # 中途要中止rebase操做 git rebase --abort # 復原到rebase以前的狀態 git reset --hard ORIG_HEAD
1)squash合併多個提交:vim
在編輯 commit message 時 hash 值前的p(pick)表示 use commit,s(squash)表示 use commit, but meld into privious commit安全
因此咱們能夠在 push 到遠程倉庫以前,把多個 commit 合併成一個。服務器
2)edit使提交成退出狀態網絡
除了p、s,經常使用的還有e(edit)表示 use commit, but stop for amending。保存退出後,修改過的提交呈現退出狀態。用 git commit --amend 保存修改,而後 git rebase --continue 。這時,有可能其餘提交會發生衝突, 請修改衝突部分後再執行 git add 和 git rebase --continue 。這時不須要提交。app
3)merge squash:
# 在 merge 特性分支時,把全部的新提交合併成一個 git merge feature-branch --squash
# 暫時將未提交的變化移除,稍後再移入 git stash # 查看全部被隱藏的文件列表 git stash list # 恢復暫存的文件,不從list中刪除 git stash appl # 從list中刪除 git stash drop # 恢復暫存的文件,並從list中刪除 git stash pop
# 遺棄提交 git reset # 把當前的 HEAD 重置到前一個,丟棄最新commit git reset HEAD~1 # 撤銷上一次提交,而且變動還保持在 staging area git reset HEAD~1 --soft --soft 只取消提交,將上一次的修改放入 staging area,不修改索引和工做樹 --mixed 默認模式,復原修改過的索引的狀態,將上一次的修改放入 working directory,修改索引,不修改工做樹 --hard 完全取消最近的提交,直接將上一次的修改拋棄,修改索引和工做樹 三種都會修改HEAD位置 # 拋棄某一次的修改,使用上次提交的版本 git checkout # 安全地取消過去發佈的提交 git revert git revert HEAD~2
1)git reset與checkout區別:
git reset 使用倉庫中的版本覆蓋 staging area 中的,若是 working directory 該文件沒有其餘修改,則 staging area 中的修改將應用到 working directory 中。反之working directory 中的版本將被保留,丟棄 staging area 中的修改。
git checkout 則是使用 staging area 的中的版本覆蓋 working directory。
2)git revert與reset區別:
與 reset 不一樣的是,revert 只會撤銷當前的 commit,而以後的 commit 操做的修改還會保留,可是 reset 還會將以後的全部 commit 操做的修改所有退回 staging area 或丟棄。
3)撤消合併:
假如還沒推送到遠端,能夠reset掉 git reset --hard HEAD~ 若是已經推進到遠端,能夠用revert git revert -m 1 HEAD
# 顯示暫存區和工做區的差別 git diff # 顯示暫存區和上一個commit的差別 git diff --cached [file] # 顯示工做區與當前分支最新commit之間的差別 git diff HEAD # 顯示兩次提交之間的差別 git diff [first-branch]...[second-branch] # 顯示今天你寫了多少行代碼 git diff --shortstat "@{0 day ago}"
# 以圖形展現 git log –graph # 能夠顯示包含標籤資料的歷史記錄 git log --decorate # 單行顯示過去5次提交 git log -5 --pretty --oneline # 顯示第一個父commit節點,而不顯示merge的commit節點,這樣會有一個更清晰的視圖 git log –first-parent # 顯示所有分支 git log –all # 更清晰地顯示 git log --graph --decorate --pretty=oneline --abbrev-commit --all git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# 查看某個文件的修改人 git blame # 優化git倉庫,git repack 將版本庫未打包的鬆散對象打包 git repack -d # 條件搜索及正則表達式搜索 git grep git grep "TODO" # 二分法查找快速定位到bug位置 git bisect git bisect HEAD origin git bisect run make test git bisect reset # 能夠列出全部的操做記錄,經過reflog找到 commit 的hash,而後 cherry-pick 找回 git reflog # 獲取命令的幫助信息 git help [命令] git [命令] --help # 獲取當前的狀態,git會提示接下來的能作的操做 git status # Git 組織大型項目的一種方式 git submodule
Git倉庫下有一個.git目錄,通常包括下面的內容:
1)config
倉庫的配置文件
2)index
索引,index文件保存暫存區信息
3)HEAD
HEAD文件指示目前被檢出的分支
4)objects
你提交到一個Git代碼倉庫中的全部文件,包括每一個提交的說明信息(the commit info)都在目錄 .git/objects/中存儲爲實體。
一個實體以一個40字符長度的字符串(該實體內容的SHA1哈希值)來標識。
實體有4類:
5)refs
Git中,一個分支(branch)、遠程分支(remote branch)或一個標籤(tag)(也稱爲輕量標籤)僅是指向一個實體的一個指針,這裏的實體一般是一個commit實體。
這些引用以文本文件的形式存儲在目錄.git/refs/中。
feat: 添加了xx功能 xx功能描述
阮一峯Commit message 和 Change log 編寫指南
1)Git flow:項目存在兩個長期分支,主分支 master 和開發分支 develop。這個模式是基於"版本發佈"的,而補是"持續發佈"
2)Github flow:只有一個長期分支,就是 master,"持續發佈"
3)Gitlab flow:Gitlab flow 的最大原則叫作"上游優先"(upsteam first),即只存在一個主分支 master,它是全部其餘分支的"上游"。只有上游分支採納的代碼變化,才能應用到其餘分支。
阮一峯Git 工做流程
pwd 顯示當前工做目錄 process working directory cd 切換目錄 change directory mkdir 建立目錄 make directory mkdir -p 遞歸建立多個目錄 --parents no error if existing, make parent directories as needed ls 列出目錄內容 list ls -a 能夠顯示.開頭的文件 all ls -l 列出文件的詳細信息 long listing format touch 建立文件 rm/rmdir 刪除文件/目錄 remove rm -r 刪除目錄 --recursively mv 文件或目錄的移動或改名 move cp 將文件拷貝至另外一文件 copy echo 輸出到文件 like echo 1 > test.txt cat 顯示文件內容和合並多個文件 like cat test.txt,also like cat file1 file2 > file less 按頁顯示文件 clear 清屏(ctrl + l) history 查看最近用過的命令 du 統計目錄/文件所佔磁盤空間的大小 disk usage du -sh 以易讀的方式顯示總共大小 --summarize --human-readable ps 報告程序情況 process status head 顯示開頭某個數量的文字區塊 like head -n 3 test.txt tail 顯示結尾某個數量的文字區塊 ping 網絡管理經常使用的3個命令 ipconfig netstat xxx -h 至少能夠經過如下三種中的一種獲取命令的幫助信息 xxx --help man xxx
Ctrl + A :光標移動到行首 Ctrl + E :光標移動到行尾 Ctrl + U :刪除光標所在位置以前的全部字符(不含當前位置字符) Ctrl + K :刪除光標所在位置以後的全部字符(含當前位置字符) Ctrl + W :刪除光標所在位置以前的一個單詞 Ctrl + R :根據輸入搜索以往使用過的命令
Alt + . 使用上一次命令的最後一個參數 !! 上一次命令 ↑ 顯示上一次命令 . 當前目錄 .. 上一層目錄 ~ 根目錄 - 上一次目錄 | 管道(pipe),前一個命令的輸出爲後一個命令的輸入,like cat test.txt | less
多個命令之間能夠用;或&&分隔,前者命令錯誤不中斷