大多數人對 Git 的常用命令都有一定的瞭解,這裏羅列一些具體案例說明需要使用哪些命令。
從網上clone一份代碼
舉例:在目錄 D:\work\WDEMO 下,下載代碼:
git clone https://github.com/miLLlulei/BasicLibrary
防止每次 push 代碼的時候輸入賬號密碼,所以需要保存帳號密碼:
打開 D:/work/WDEMO/BasicLibrary/.git/config 文件;
注意 windows 下 .git 文件夾默認是隱藏的,需要打開隱藏文件;
在 config 文件增加上以下配置:
[credential]
helper = store
提交代碼
# 先拉取遠程代碼,併合並:
git pull
# 添加當前目錄的所有文件到暫存區:
git add .
# 提交記錄:
git commit -m '記錄內容說明'
# 推送到遠程:
git push origin master
git add .
git commit -m '記錄內容說明'
# 可以用下面代替:
git commit -am '記錄內容說明'
永久刪除文件,包括歷史提交記錄
# 刪除某個文件 -- Test 目錄下的 test.txt
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch Test/test.txt' --prune-empty --tag-name-filter cat -- --all
# 刪除某個目錄 -- Test 目錄
git filter-branch --force --index-filter 'git rm --cached -r --ignore-unmatch Test' --prune-empty --tag-name-filter cat -- --all
# 推送到遠端,這步是不可逆的,注意備份源碼
git push origin master --force --all
# 推送到你的所有 tag
git push origin master --force --tags
# 清除本地倉庫 objects,這個是可選操作,因爲不弄,等待gc也會清除的。
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now