如何批量删除git仓库中的提交纪录?
某草草
某草草 2017-04-28 09:06:29
0
1
841

如何批量删除一些很久之前的提交记录,但保留那些修改的内容。

某草草
某草草

reply all(1)
滿天的星座

This question is a bit A/B, because you did not explain the scenario of deleting the commit record long ago, which is very important because it will affect the subsequent choice of using Git. For example:

  1. If I want to delete the submissions from the history that were submitted before the exam, but there are still many submissions that need to be retained later, then:
    1.2 If the historical records to be deleted are scattered, you can consider Interactive Rebase, self-picking/merging, etc. Such as git rebase -i <ref>
    1.1 If the historical records to be deleted are continuous, for example, all the historical records from the beginning to a certain moment can be deleted or a section in the middle can be deleted, you can consider Onto Rebase, for example, the historical records between git rebase --onto <ONTO_BASE_ref> <START_ref> <END_ref>,其中 STARTEND 之间的是需要保留的部分,而 ONTO_BASE 则是最新的基点;换言之,从 ONTO_BASESTART will be deleted.
  2. If I want to delete a lot of historical records but want to keep very few (for example, just keep the most recent one, I don’t want it anymore), then I can simply create an Orphan Branch to reconstruct the historical records. As git checkout --orphan new_start,这条命令会创建一个叫做 new_start 的分支,该分支没有任何历史记录,但是所有的文件都会原封不动的存在,你可以据此开始重新提交。完成之后甚至可以把旧的分支直接废弃。另外,也可以指定新分支的起点,默认当然是从 HEAD begins.
  3. You can also divide the historical record into two (or more) parts, some of which are complete, some of which are simplified, etc. For details, please refer to this document about git replace: http://git-scm.com/2010/ 03/17/replace.html

In fact, there are many scenarios that can be said. The usage of Git is very flexible. Even if you don’t use it temporarily, it is worth reading it carefully to know what kind of things it can do. Then you can deduce and solve it yourself when encountering various complex scenarios. Planned.


As a maintainer of Repo, the most common thing is to keep it from a certain ref to HEAD, and then delete the previous history. Because this task is relatively common, here is also a shell script to share with you:

sh#!/bin/bash
git checkout --orphan temp 
git commit -m "截取的历史记录起点"
git rebase --onto temp  master
git branch -D temp

When used like this (for example, if the script is saved as git-detach): git-detach <ref>,其中 <ref>, it is the starting point of the history record you want to keep.

It should be noted that this script only "separates" the history record, and then part of it has no visible references and is therefore invisible in the history record. However, their git object still exists (in other words, you can still restore it) Come here and look it up yourself git-reflog),如果你真要彻底丢掉这些历史(为了给 repo 减肥),可以用 git gc --prune, then you’ll never find it again

.

P.S. This script depends on Orphan Branch, which is not supported by lower versions of Git (probably < v1.7.x). If you have an alternative, please Google it yourself.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template