git 如何清理操作日志_git如何清空所有的commit记录
為什么要清空 git 中的 commit 記錄?
大多數(shù)開發(fā)者喜歡在 github 創(chuàng)建自己的 Repository,而后進(jìn)行持續(xù)開發(fā),然后就是不斷的 add、commit、push 等,中間難免會把自己比較重要的隱私信息 push 到遠(yuǎn)端 origin,如果你刪除了再 push 遠(yuǎn)端 origin, 提交 commit 記錄里面也是存在的,并且大多是開發(fā)者創(chuàng)建的都是 Public Repository,免費的,所有人通過該倉庫的獨有鏈接可以直接 clone 到本地。那么就會造成隱式信息的直接暴露。
先通過 git checkout --help 了解一下 --orphan ,因為我們要用到它。
git checkout --help
--orphan 的 --help
對于--orphan < new_branch > 的解釋就是:
// Create a new orphan branch, named , started from
// 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.
// 中文翻譯:
// 創(chuàng)建一個獨立的new_branch分支,HEAD指向當(dāng)前分支,并自動切換到該分支;
// 分支沒有父級結(jié)點,它是一個新的根記錄,不與其他任何分支和提交記錄有連接。
// The index and the working tree are adjusted as if you had
// previously run "git checkout ". This allows you to
// start a new history that records a set of paths similar to
// by easily running "git commit -a" to make the root
// commit.
// 中文翻譯:
// 它會基于你之前執(zhí)行"git checkout "的 start_point 分支,調(diào)整新的索引和分支樹
// 你可以通過"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記錄,不想暴露他所有提交歷史,那么它就很有用。
// 如果你想通過這樣做發(fā)布一個開源分支工程,當(dāng)前所有包含專利記錄分支樹就會被清空,否則就是一堆冗余的代碼;
// If you want to start a disconnected history that records a set of
// paths that is totally different from the one of , 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分支不同,在你創(chuàng)建這個獨立分支后,
// 通過 'git rm -rf',從根工作空間刪除所有工作空間和索引里面的文件。
// 隨后,你可以通過從別處復(fù)制準(zhǔn)備你的新文件來從新填充你的工作空間。
復(fù)制代碼
解決方案
那么如何解決這個問題?
思路如下:
使用 git checkout --orphan new_branch ,基于當(dāng)前分支創(chuàng)建一個獨立的分支new_branch;
git checkout --orphan new_branch
復(fù)制代碼
添加所有文件變化至?xí)捍婵臻g
git add -A
復(fù)制代碼
提交并添加提交記錄
git commit -am "commit message"
復(fù)制代碼
刪除當(dāng)前分支
(我的當(dāng)前分支是master,個人小的項目沒有使用 gitflow 工作流管理,切記master謹(jǐn)慎刪除:grin:)
git branch -D master
復(fù)制代碼
重新命名當(dāng)前獨立分支為 master
git branch -m master
復(fù)制代碼
推送到遠(yuǎn)端分支
-f 是 --force 的縮寫, 一定要謹(jǐn)慎使用,好多項目中你或者是別人的代碼被覆蓋都是這么操作的,除非只有你一個人在開發(fā);
git push -f origin master
復(fù)制代碼
好了,沒了!
PS:
一定要記住,切記謹(jǐn)慎刪除本地 master分支;
-D 是--delete的縮寫;
-f是 --force 強(qiáng)制操作;
git rm -rf 謹(jǐn)慎使用;
以上不要隨意使用,切記!切記!切記!
不然你可能會被人砍死:grin::grin::grin:
總結(jié)
以上是生活随笔為你收集整理的git 如何清理操作日志_git如何清空所有的commit记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java异步servlet_Servle
- 下一篇: java jsoup html_使用JA