How to commit and version changes in a git fork/branch but not merge them back
Published 2017-09-21, 11:38
Recently I tried to figure out how to have both a production and staging environment of a Jekyll website hosted with Github Pages.
This turned out to be not that simple, as both repositories (Github Pages can only host one branch per repository, so you need two – one organization repository and one personal for example) need to have a CNAME
and _config.yml
file with different content.
So the actual question to answer was how can one commit and version changes to files in a git fork/branch but then not merge them back into the source, while still having a fully merged branch (so actual changes can be merged back via Pull Requests)?
The answer is to use the ours
merging strategy when merging the changes that should be skipped:
- Assume we have our
production
repo andstaging
repo both checked out locally, for now being identical (and having the sameCNAME
and_config.yml
). - Check out
staging
, make the changes to the files and commit and push them. - If we were to do a normal merge to
production
, this change would now get merged over. - But now we run these commands:
git checkout production
git merge --strategy=ours staging
(Source)
--strategy=ours
makes sure that during this merge, theproduction
file remain unchanged. But the commit is still marked as merged.
If we want, we can now also edit the merge commit messageto be a bit more descriptive what we just did: We merged the two branches, but made it keep „our“ (in this case:production
) version of the files. - Then push the changes to production:
git push production production:master
Your next changes to staging
can the be merged with production
normally, without getting the CNAME
and _config.yml
changes.
( 1 )
Yep, great design choice Jan. Super readable…
Meh.
Comment von Jan am 21. September 2017