[git使用] 如何清空全部的commit記錄

前言


爲何要清空 git 中的 commit 記錄?javascript

大多數開發者喜歡在 github 建立本身的 Repository,然後進行持續開發,而後就是不斷的 add、commit、push 等,中間不免會把本身比較重要的隱私信息 push 到遠端 origin,若是你刪除了再 push 遠端 origin, 提交 commit 記錄裏面也是存在的,而且大可能是開發者建立的都是 Public Repository,免費的,全部人經過該倉庫的獨有連接能夠直接 clone 到本地。那麼就會形成隱式信息的直接暴露。java

先經過git checkout --help瞭解一下--orphan,由於咱們要用到它。git

git checkout --helpgithub

git checkout --help

--orphan 的 --helpbash

--orphan的說明

對於--orphan < new_branch > 的解釋就是:this

// Create a new orphan branch, named <new_branch>, started from
// <start_point> and switch to it. The first commit made on this new
// branch will have no parents and it will be the root of a new
// history totally disconnected from all the other branches and
// commits.
// 中文翻譯:
// 建立一個獨立的new_branch分支,HEAD指向當前分支,並自動切換到該分支;
// 分支沒有父級結點,它是一個新的根記錄,不與其餘任何分支和提交記錄有鏈接。

// The index and the working tree are adjusted as if you had
// previously run "git checkout <start_point>". This allows you to
// start a new history that records a set of paths similar to
// <start_point> by easily running "git commit -a" to make the root
// commit.
// 中文翻譯:
// 它會基於你以前執行"git checkout <start_point>"的 start_point 分支,調整新的索引和分支樹
// 你能夠經過"git commit -a"提交一個新commit記錄做爲根提交記錄,這樣的話你就有個一個新的歷史記錄,
// 相似於 start_point 分支的一系列提交記錄痕跡;

// This can be useful when you want to publish the tree from a commit
// without exposing its full history. You might want to do this to
// publish an open source branch of a project whose current tree is
// "clean", but whose full history contains proprietary or otherwise
// encumbered bits of code.
// 中文翻譯:
// 若是你想把你的分支樹變成只有一個commit記錄,不想暴露他全部提交歷史,那麼它就頗有用。
// 若是你想經過這樣作發佈一個開源分支工程,當前全部包含專利記錄分支樹就會被清空,不然就是一堆冗餘的代碼;

// If you want to start a disconnected history that records a set of
// paths that is totally different from the one of <start_point>, then
// you should clear the index and the working tree right after
// creating the orphan branch by running "git rm -rf ." from the top
// level of the working tree. Afterwards you will be ready to prepare
// your new files, repopulating the working tree, by copying them from
// elsewhere, extracting a tarball, etc.
// 中文翻譯:
// 若是你想開始一個全新的提交記錄,徹底與start_point分支不一樣,在你建立這個獨立分支後,
// 經過 'git rm -rf',從根工做空間刪除全部工做空間和索引裏面的文件。
// 隨後,你能夠經過從別處複製準備你的新文件來重新填充你的工做空間。
複製代碼

解決方案

那麼如何解決這個問題?spa

思路以下:翻譯

  • 使用 git checkout --orphan new_branch ,基於當前分支建立一個獨立的分支new_branch;
git checkout --orphan  new_branch
複製代碼
  • 添加全部文件變化至暫存空間
git add -A
複製代碼
  • 提交併添加提交記錄
git commit -am "commit message"
複製代碼
  • 刪除當前分支
    • (個人當前分支是master,我的小的項目沒有使用 gitflow 工做流管理,切記master謹慎刪除😁)
git branch -D master
複製代碼
  • 從新命名當前獨立分支爲 master
git branch -m master
複製代碼
  • 推送到遠端分支
    • -f 是 --force 的縮寫, 必定要謹慎使用,好多項目中你或者是別人的代碼被覆蓋都是這麼操做的,除非只有你一我的在開發;
git push -f origin master
複製代碼

好了,沒了!code

PS:

  • 必定要記住,切記謹慎刪除本地 master分支;
  • -D 是--delete的縮寫;
  • -f是 --force 強制操做;
  • git rm -rf 謹慎使用;
  • 以上不要隨意使用,切記!切記!切記!
  • 否則你可能會被人砍死😁😁😁
相關文章
相關標籤/搜索