滿天的星座2017-04-28 09:08:29
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:
git rebase -i <ref>
git rebase --onto <ONTO_BASE_ref> <START_ref> <END_ref>
,其中 START
到 END
之间的是需要保留的部分,而 ONTO_BASE
则是最新的基点;换言之,从 ONTO_BASE
到 START
will be deleted. git checkout --orphan new_start
,这条命令会创建一个叫做 new_start
的分支,该分支没有任何历史记录,但是所有的文件都会原封不动的存在,你可以据此开始重新提交。完成之后甚至可以把旧的分支直接废弃。另外,也可以指定新分支的起点,默认当然是从 HEAD
begins. 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.