git 倉庫特點
- 使用快照流
- 近乎所有操作都是本地執行
- 一般只添加數據
三種狀態:modified -> committed -> staged
在 Git 中任何已提交的東西幾乎總是可以恢復的。甚至那些被刪除的分支中的提交或使用 --amend 選項覆蓋的提交也可以恢復。然而,任何你未提交的東西丟失后很可能再也找不到了。
config
全局讀:~/.gitconfig(在無.git目錄下 或者 加上--global 參數)
項目讀:.git/config
- git config --list
列出當前所有配置信息 - git config <key>
查詢某一項配置信息
status
git status -s 簡短輸出各文件狀態
diff
git diff 查看未放入暫存區的文件變化
git diff --cached 查看已放入暫存區的文件變化
commit
git commit -a 可以跳過 git add . 步驟
git commit --amend 提交后發現忘記了暫存某些需要的修改,可以使用此命令,最終只會有一個提交 - 第二次提交將代替第一次提交的結果
reset
- git reset:回滾 git add 操作
- git reset --soft HEAD^:回滾最近一次提交(commit)
- git reset --hard HEAD~n:永久刪除最近的 n 個提交
- git reset --hard:回滾 git pull 操作
- git reset -- {fileName}:回滾 git add fileName 操作
- git reset --keep {tagName}:回滾到 tagName 之前
rm
git rm 從已跟蹤文件清單中移除(確切地說,是從暫存區域移除)
git rm --cached 把文件從 Git 倉庫中刪除(亦即從暫存區域移除),但仍然希望保留在當前工作目錄中
mv
git 不會顯示地跟蹤文件的重命名和移動
所以需要使用git mv命令
git mv {originFile} {targetFile}
相當于以下三條命令
mv {originFile} {targetFile}
git rm {originFile}
git add {targetFile}
log
git log 查看提交歷史
-p 查看提交歷史的變化
-2 查看最近兩次提交
--stat 查看簡略信息
--pretty=oneline 一行表示一次提交信息
--pretty=format:"%h - %an, %ar : %s" 指定格式顯示提交信息
tag
- git tag:列出已有的標簽
- git tag -a {tagName} -m {‘description’}:創建標簽
- git tag -s {tagName} -m {‘description’}:使用私鑰創建標簽
- git tag -d {tagName}:刪除標簽
- git tag {tagName}:創建輕量級標簽(無描述)
- git tag -a {tagName} {HEAD} -m {‘description’}:對某一次提交打上標簽
- git push --tags:分享標簽
show
git show 命令指定提交ID(hash HEAD)來查看具體的變化
stash
git stash 將當前工作區的更改隱藏起來 保存在一個棧中
git stash pop 將隱藏棧頂部的工作去更改推出
git stash list 顯示當前的隱藏棧
git stash 詳解
git工作流程
- 將Git的一個存儲庫克隆為工作副本。
- 可以通過添加/編輯文件修改工作副本。
- 如有必要,還可以通過讓其他開發人員一起來更改/更新工作副本。
- 在提交之前查看更改。
- 提交更改:如果一切正常,那么將您的更改推送到存儲庫。
- 提交后,如果意識到某些錯誤并修改錯誤后,則將最后一個正確的修改提交并將推送到存儲庫。
rebase
好用的 git rebase
delete branch
- 刪除本地分支:git branch -d 分支名稱
- 強制刪除本地分支:git branch -D 分支名稱
- 刪除遠程分支:git push origin --delete 分支名稱
shortlog
git shortlog 統計提交(commit)次數
-s 只顯示次數,不顯示commit描述
-n 從多到少排序
強行將遠程代碼回滾
git reset HASH_HEAD
git push -f
.gitignore 存放忽略文件
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf