Keep specific files of two Git branches in

I have a repository consisting of four files in the master branch: two project files (PHP files btw) plus a README.md and .gitignore . I would like to create an additional branch, that consists of just the two PHP files at the same commit-level. What's the best practice to keep those two branches in-sync, if possible I would like to commit once only to update my PHP files?

The reason why I'm looking into this, is that I would like to create a branch called dist, that only comes with the essential files.


Create your dist branch, recording the deletion of the files you don't want.

git checkout master
git checkout -b dist
git rm .gitignore
git rm README.md
git commit -m "Remove non-dist files"

Then, each time you want to update your dist branch, simply rebase it:

git checkout dist
git rebase master

That replay your dist commit (which you made when you created that branch, and where you git rm some files) on top of master , which means it will always remove the extra files.

You will have to force push it to your remote, but that isn't an issue since nobody is supposed to contribute on that branch.


That being said, release management is generally not maintained through branching, but with a script able to package and release from the sources (meaning here one script in the master branch that you execute in order to copy what is needed to where you want to deploy).


As @VonC says, Git's not set up as a release-management or a deployment tool. You're far from the first to notice that it's tantalizingly close :-) -- so it's pretty easy to get it to do what you want.

Here's how you make a commit-able tree using just the content you want, without needing a checkout. Put this in .git/post-commit and maybe link to it from .git/hooks/post-merge , from then on the branch is maintained automatically.

#!/bin/sh
files="this.php that/other.php"
branch=pub

# build a tree with selected already-committed content
new_tree=$(
  export GIT_INDEX_FILE=.git/sideband-index
  git read-tree --empty
  git ls-tree HEAD --  $files 
  | git update-index --index-info --add
  git write-tree
);

# commit the new tree to the tip of "$branch"
# if it doesn't match what's already there
current=$(git rev-parse -q --verify $branch:)
test $new_tree != "$current" 
&& git update-ref refs/heads/$branch $(
    git commit-tree ${current:+-p $branch} -m new content $new_tree
)

The unfamiliar commands (pretty much all of them, here) are the ones built for use by scripters, they're meant for this.

链接地址: http://www.djcxy.com/p/49240.html

上一篇: Github:从主人合并到功能分支

下一篇: 保留两个Git分支的特定文件