在Git的根提交之前插入提交? [英] Insert a commit before the root commit in Git?

查看:79
本文介绍了在Git的根提交之前插入提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过如何压扁前两次提交在git仓库中。



尽管解决方案相当有趣,并不像git中的其他一些东西那样令人头痛,但它们仍然有点儿如果你需要在项目开发过程中多次重复这个过程,那么这个问题就会受到伤害。

所以,我宁愿只经历一次痛苦,然后才能永远使用标准的互动式底线。

然后,我想要做的只是为了成为第一个目的而存在一个空的初始提交。没有代码,没有任何东西。只是占用空间,所以它可以成为rebase的基础。



然后,我的问题是,拥有一个现有的存储库,我该如何在插入一个新的空提交之前第一个,并转移其他人向前??

解决方案

2017年中期答案



创建一个没有副作用的新的完全空提交可能最好是直接使用Git的管道。这样做可以避免任何副作用:不触摸工作副本或索引,不需要临时分支来清理等。所以:


  1. 创建一个提交,我们需要一个目录树,所以我们首先创建一个空目录:

     树=`git hash-object -wt tree --stdin< / dev / null` 


  2. 现在我们可以围绕它提交一个提交:

      commit =`git commit-tree -m'root commit'$ tree` 
    $

     git rebase --onto $ commit --root master 




<这就是它。如果你知道你的shell足够好,你可以重新整理整个事物。



(注意:实际上我现在使用






历史回答(其他引用的过滤器分支答案)



下面是相同解决方案的一个更清晰的实现,因为它不需要创建额外的存储库,使用遥控器解决问题并更正分离的头部:

 #首先你需要一个新的空分支;我们称之为`newroot` 
git checkout --orphan newroot
git rm -rf。

#然后你应用相同的步骤
git commit --allow-empty -m'root commit'
git rebase --onto newroot --root master
git branch -d newroot

瞧,你已经结束了 master ,其历史重写为包含一个空的根提交。






Git缺少 - orphan 切换到 checkout ,你需要管道来创建一个空分支:

  git symbolic-ref HEAD refs / heads / newroot 
git rm --cached -r。
git clean -f -d


I've asked before about how to squash the first two commits in a git repository.

While the solutions are rather interesting and not really as mind-warping as some other things in git, they're still a bit of the proverbial bag of hurt if you need to repeat the procedure many times along the development of your project.

So, I'd rather go through pain only once, and then be able to forever use the standard interactive rebase.

What I want to do, then, is to have an empty initial commit that exists solely for the purpose of being the first. No code, no nothing. Just taking up space so it can be the base for rebase.

My question then is, having an existing repository, how do I go about inserting a new, empty commit before the first one, and shifting everyone else forward?

解决方案

Mid-2017 answer

Creating a new completely empty commit with no side effects is probably best done by using Git’s plumbing directly. Doing it that way avoids any side effects: no touching the working copy or the index, no temporary branches to clean up, etc. So:

  1. To create a commit, we need a directory tree for it, so we create an empty one first:

    tree=`git hash-object -wt tree --stdin < /dev/null`
    

  2. Now we can wrap a commit around it:

    commit=`git commit-tree -m 'root commit' $tree`
    

  3. And now we can rebase onto that:

    git rebase --onto $commit --root master
    

And that’s it. You can rearrange that whole thing into a one-liner if you know your shell well enough.

(N.B.: in practice I’d now use filter-branch. Will edit that in later.)


Historical answer (referenced by other answers)

Here’s a cleaner implementation of the same solution, in that it works without the need to create an extra repository, futz around with remotes, and correct a detached head:

# first you need a new empty branch; let's call it `newroot`
git checkout --orphan newroot
git rm -rf .

# then you apply the same steps
git commit --allow-empty -m 'root commit'
git rebase --onto newroot --root master
git branch -d newroot

Voila, you’ve ended up on master with its history rewritten to include an empty root commit.


NB.: on old versions of Git that lack the --orphan switch to checkout, you need the plumbing to create an empty branch:

git symbolic-ref HEAD refs/heads/newroot
git rm --cached -r .
git clean -f -d

这篇关于在Git的根提交之前插入提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆