繁体   English   中英

如何从一个 Git 分支中删除文件并将它们添加到新分支

[英]How can I remove files from one Git branch and add them to a new branch

对于上下文,我正在使用 repo 来管理我的 dotfiles,并希望将特定于操作系统的配置从main移动到新分支,以便main更便携。 然后,我将定期在main上重新设置新的、特定于操作系统的分支。

我想我希望历史看起来像这样

main:       old commit - old commit - remove some files
                                                       \
new branch:                                             add those same files

我不太在乎历史,我意识到责任可能会因此而被摧毁。

我会这样做:

git checkout main
git rm the-file
git commit -m "Removing some file"
git checkout the-other-branch
git rebase main
git checkout main~ -- the-file # get the file back from a revision where it existed
# no metadata is brought for that file
git commit -m "Getting back the-file"

保存历史比较棘手:

git checkout main
git rm the-file
git commit -m "Removing the file"
git checkout main~
git merge --no-ff -m "merging removal, but keeping the file" main
# here is where we _really_ get the file back:
git checkout main~ -- the-file
git commit --amend --no-edit
# this merge revision keeps the file _and_ it is already a child of main _without_ the file
# this revision is where the feature branch should be now
git branch temp
git checkout the-feature
git rebase temp
# now..... you have the-feture, which is a descendant of main and _has_ the file.... and history of the file is kept

最重要的是? 您可以合并 master 中的内容,并且该文件不会被删除。

您可以将这些提交文件推送到 GitHub 中的新存储库,并删除您不需要的文件/提交并下载它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM