打開配置文件
git config --global --edit
改變默認(rèn)編輯器
git config --global core.editor vim
刪除遠程git倉庫里面的文件
# 取消對某個文件/文件夾的追蹤
git rm -r --cached file_path/fold_path
git commit -m "刪除遠程文件"
git push
將遠程分支(還沒有merge到master的分支)拉到本地
# 先進入 master 分支, 保證新建的分支是以 master 為基準(zhǔn)
git checkout master
# 與遠程倉庫進行同步
git pull
# 在本地創(chuàng)建dev 分支, 并且關(guān)聯(lián)到dev分支
git checkout -b dev origin/dev
操作分支
# 刪除本地分支-
git branch -d dev
git branch -D dev # 強行刪除分支
# 刪除遠程分支
git push --delete origin branch
# 重命名分支
git branch -m [old_branch_name] new_branch_name # 重命名本地分支
git push origin :old_branch_name # 刪除遠程分支
git push origin branch_name # 將本地新分支推送到遠程
# 設(shè)置對應(yīng)的遠程(上游)分支
git push --set-upstream <remote> <branch>
# 給文件重命名
git mv old_name new_name
git mv old_folder new_folder
查看某次 commit 涉及的文件
git show commit_id --stat
git stash
- git stash apply: 繼續(xù)編輯最新的草稿;
- git stash list: 列出所做存儲的草稿;
- git stash == git stash push:
- drop:
- pop:
- clear:
- create:
- store:
換行符
Win 下?lián)Q行符是 \n\r (CRLF);
Linux/Mac 下?lián)Q行符是 \n (LF);
VSCode 下建議將默認(rèn)換行符都設(shè)置為 \n (LF). setting: FIle: Eol
git config --global core.autocrlf input
If you’re on a Linux or macOS system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input:
離線更新
- 將最近代碼打包成 bundle 文件:
git bundle create bundle_tag.bundle master - 將遠程分支打包成 bundle 文件: git bundle create bundle_tag.bundle origin master(推薦使用)
- 使用 bundle 文件離線更新倉庫:
git pull bundle_tag.bundle master - 將其他分支打包成 bundle 文件: git bundle create bundle_tag.bundle branch_name
- 使用 bundle 文件離線更細倉庫: git pull bundle_tag.bundle branch_name
合并多個 commit 有以下幾種方式
git commit --amend: 在上一個 commit 的基礎(chǔ)上添加本次改動;-
git log: 首先調(diào)出提交的 commit 歷史, 然后找到一個基準(zhǔn)點的commit id;git rebase -i commit_id: 可對該 commit_id 之后提交的所有 commit 進行整理- pick: 使用該次commit;
- squash: 將本次commit合并到上個commit, 并且之后編輯 commit 備注;
- fixup: 將該條 commit 合并到上個 commit, 合并之后使用上次的備注.
git diff 查看 2個 commit 之間的變動
git diff commit_id1 commit_id2: 可查看這2個commit之間每個文件的改動
-- name-only: 只查看發(fā)生改變的文件名: 如 git diff --name-only commit_id1 commit_id2
恢復(fù)部分文件
在某次改動中經(jīng)常會改動多個文件. 一般來說, 一次 commit 只包含一個文件.
如果需要將其中部分文件恢復(fù)至之前的某個狀態(tài), 可以采取一下措施:
- git log 確定需要回滾的 commit 編號.
- 恢復(fù)文件, 有以下2種方法:
- git checkout commit_id -- 需要恢復(fù)的文件
- git restore --source=commit_id 需要恢復(fù)的文件
- git commit -m "msg" 恢復(fù)的文件: 將恢復(fù)的文件提交
.gitignore
- ! 表示否, 如
!mining_log.py: 不排除 mining_log.py 文件 - 子文件夾中也可以有 .gitignore .gitkeep 文件.
git reset
git reset commit_id: 將 index and working tree 恢復(fù)至指定狀態(tài), 文件內(nèi)容不變;
--hard: 將文件內(nèi)容也恢復(fù)至 指定狀態(tài);
恢復(fù)文件
如果改動了某個/寫文件, 但是不需要提交這些改動, 而是需要將其恢復(fù)至改動之前:
- 老版 git: git checkout -- path/to/file;
- 新版 git: git restore path/to/file;