git:通过文件删除进行变基提交时如何不删除文件

发布于 2024-08-25 13:40:02 字数 885 浏览 8 评论 0原文

我有一个分支,我想将其重新设置为我的主控上的最新提交。问题在于 master 上的干预提交之一是删除并忽略一组特定文件(请参阅 这个问题)。

如果我直接进行变基操作,这些文件将再次被删除。无论如何,在 git 内部,是否可以这样做,而不是手动复制所有文件,然后再将它们复制回来?

或者我应该做一些事情,比如创建一个新的 master 分支,然后合并旧分支的提交?

尝试ascii art:

master    branch
  |        w  work in progress on branch
  C        |  committed further changes on master
  |        |
  B        /  committed delete/ignore files on master
  |       2  committed changes on branch
  |      /  
  A     /  committed changes on master which I now need to get branch working
  |    1  committed changes on branch
  0___/  created branch

(在进行艺术创作时,我意识到我可以从A开始变基分支,然后在完成后合并,但我仍然想知道是否有办法“正确”地做到这一点)

更新警告任何尝试此操作的人。这里提出的解决方案很好,但是当您再次签出 master 时,B 提交将被重新应用,并且您再次丢失所有文件:(

I have a branch that I would like to rebase onto the lastest commit on my master. The problem is that one of the intervening commits on master was to delete and ignore a particular set of files (see this question).

If I just do a straight rebase, those files will get deleted again. Is there anyway of doing this, inside git, rather than copying all the files out by hand, then copying them back in again afterwards?

Or should I do something like create a new branch off master, then merge in just the commits from the old branch?

Attempts ascii art:

master    branch
  |        w  work in progress on branch
  C        |  committed further changes on master
  |        |
  B        /  committed delete/ignore files on master
  |       2  committed changes on branch
  |      /  
  A     /  committed changes on master which I now need to get branch working
  |    1  committed changes on branch
  0___/  created branch

(Doing the art, I realise that I could just rebase branch from A, then merge when I've finished, but I'd still like to know if there's a way to do this 'properly')

UPDATE Warning to anyone trying this. The solution proposed here is fine, but when you checkout master again, the B commit will be re-applied, and you lose all your files again :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-09-01 13:40:02

从您的评论中,我认为您需要在 master 之上重新建立 dev 分支,除了 B.
您不希望看到在一个分支中删除的文件在另一分支中被删除。

一种可能的解决方案是:

  • rebase --interactive master to rewrite commits order (if possible)
    0--A--C'--B'  master
     \
      --1--2--w     dev
  • rebase devbranch on top of C' (representing all the master commit except B)
    0--A--C'--B' master 
           \
            --1'--2'--w' dev

这意味着您还没有将 master 推送到另一个存储库(否则,对于那些希望在自己的 repo/master 分支中拉取 master 的人来说,这将是一场合并噩梦)

From your comment, I gather you need to rebase dev branch on top of master except B.
You do not want to see files you removed in one branch be removed in another.

One possible solution would be:

  • rebase --interactive master to rewrite commits order (if possible)
    0--A--C'--B'  master
     \
      --1--2--w     dev
  • rebase dev branch on top of C' (representing all the master commit except B)
    0--A--C'--B' master 
           \
            --1'--2'--w' dev

That means you do not have already pushed master to another repository (otherwise, that would involve a merge nightmare for those who wish to pull master in their own repo/master branch)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文