[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 谨慎使用;
  • 以上不要随意使用,切记!切记!切记!
  • 否则你可能会被人砍死😁😁😁
相关文章
相关标签/搜索