Git 實用技巧

打開配置文件

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:

離線更新

  1. 將最近代碼打包成 bundle 文件: git bundle create bundle_tag.bundle master
  2. 將遠程分支打包成 bundle 文件: git bundle create bundle_tag.bundle origin master(推薦使用)
  3. 使用 bundle 文件離線更新倉庫: git pull bundle_tag.bundle master
  4. 將其他分支打包成 bundle 文件: git bundle create bundle_tag.bundle branch_name
  5. 使用 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), 可以采取一下措施:

  1. git log 確定需要回滾的 commit 編號.
  2. 恢復(fù)文件, 有以下2種方法:
    1. git checkout commit_id -- 需要恢復(fù)的文件
    2. git restore --source=commit_id 需要恢復(fù)的文件
  3. 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;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文首次發(fā)表在 6 條 Git 實用技巧 -- 泰曉科技 本文匯總最近一段時間用到的幾則 Git 實用小技巧,歡迎...
    吳章金閱讀 399評論 2 1
  • 作為一名有多年開發(fā)經(jīng)驗的老兵,版本控制從最開始的SVN到Git,用著還算順手,今天總結(jié)下整理成文章,以便用時查閱,...
    jiantaocd閱讀 209評論 0 0
  • 1、新建 創(chuàng)建一個新的 git 版本庫。這個版本庫的配置、存儲等信息會被保存到.git 文件夾中 2、配置 更改設(shè)...
    WYL_99閱讀 268評論 0 0
  • 1. 撤銷操作技巧 任何時候,你都有可能需要撤消剛才所做的某些操作。本段總結(jié)git中常用的撤銷操作: 修改最后一次...
    OldChicken_閱讀 252評論 0 2
  • Git命令: --基礎(chǔ)使用-- 用戶名:git config --global user.name "<Your ...
    Dollkey閱讀 1,830評論 0 86

友情鏈接更多精彩內(nèi)容