如何为单个文件创建分支?
我有一个有两个版本的产品,到目前为止,我只能使用编译器标志和#ifdefs 来管理它们之间的差异。现在我想为每个产品都有一个单独的图标。代码库的其余部分,包括引用图标文件的源代码保持不变。我想将这些开发流保留在 git 分支(例如 master、pro 和 lite)中,但希望分支 pro 和 lite (a) 仅包含不同的 icon.png 文件,并且 (b) 自动包含对任何内容所做的所有更改分支 master 中的文件。
在 Subversion 中,这可以通过分支单个文件 (icon.png) 来完成。有没有办法在 git 中做类似的事情?
I have a product which comes in two versions, and up to now I was able to manage the differences between them only using compiler flags and #ifdefs. Now I want to have a separate icon for each of the product. The rest of the codebase, including source referencing the icon file remains the same. I want to keep these development streams in git branches (say, master, pro, and lite), but want branches pro and lite to (a) only contain different icon.png files, and (b) automatically contain all changes made to any files in branch master.
In Subversion this could be done by branching a single file (icon.png). Is there a way to do something similar in git?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需创建一个分支: git checkout -bbranchName 并替换图标。当然还有 git commit。之后,您可以在父分支中工作(如果您愿意,也可以在 master 分支中工作)并使用 git rebase 将更改拉入branchName
Just create a branch: git checkout -b branchName and replace the icon. And git commit, of course. After that you can work in the parent branch (or in master if you prefer) and use git rebase to pull the changes into branchName
您无法在 Git 中为单个文件创建分支。创建分支时,您会为整个存储库创建一个分支。 (当然,与 Subversion 不同,分支不会创建文件的实际磁盘副本,因此分支很便宜,并且不会像 Subversion 分支占用那么多额外空间。)
听起来像是此图标更改的分支将是一个长期运行的分支,即,它将有从 master 合并的更改,但它的更改不会经常合并回 master,并且您几乎可以永久保留该分支。在这种情况下,您应该将分支设置为自动跟踪主分支。在创建新分支时,您可以通过将
--track
选项传递给gitbranch
或git checkout
来进行设置(请参阅手册页欲了解更多详细信息,请参阅git分支
)。You cannot create a branch for a single file in Git. When you create a branch, you create a branch for the entire repo. (Of course, unlike Subversion, a branch does not create actual on-disk copies of your files, so branches are cheap and don't take up as much extra space as a Subversion branch.)
It sounds like the branch for this icon change is going to be a long-running branch, i.e., it'll have changes merged from master, but it's changes won't be merged back to master often, and you'll pretty much keep the branch around permanent. In that case, you should set up the branch to automatically track the master branch. You can set this up by passing the
--track
option togit branch
orgit checkout
, when creating the new branch (see the man page forgit branch
for more details).只需分支并提交图标即可。像这样在 Git 中应该没问题。然后,当您想要包含其他分支的更改时,只需合并即可。
Just branch and commit the icon. Should be just fine in Git like that. Then you'd just merge when you want to include the changes from other branches.