mngeek206
mngeek206

Reputation: 5177

How to create a commit that makes one branch identical to another?

Assume I have two branches: master and deploy. The master branch is the current "edge" code - feature and bug fix branches are merged to master. The deploy branch on the other hand is what my CI system watches to trigger automatic deployment. The normal workflow will be that once code is considered "good" on master, it's merged to deploy which causes an automatic deployment.

In testing the CI environment I made a bunch of test commits to the deploy branch. The commits simply made changes for changes' sake, just to test the CI trigger. There's now maybe 12 or so commits that simply make silly random (but detectable in the deployment) changes. Now that the CI is working I want to "roll back" the deploy branch to the current master.

The problem is that I don't have the ability to use --force on the remote repo (permissions).

Therefore what I'd like to be able to do is to somehow make a commit that will make the master and deploy branches identical. Basically I want to diff the current deploy and master branches, and add that diff as a commit to deploy. Then I can merge deploy into master which will cause no changes to be made, and the branches will be back in sync.

Just merging master into deploy right now won't work since deploy's changes will be maintained.

I'm not worried about the commit history.

I know I can bring in an individual file from another commit or branch by doing git checkout <branch> <file>. What I'm basically looking for is "check out ALL files from another branch but do not switch to that branch." Then I can just make a commit and go from there.

Upvotes: 4

Views: 517

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 21998

Using the merge strategy called ours like this :

git checkout master
git merge deploy -s ours
git checkout deploy
git merge master
git push origin HEAD

will make a merge commit on deploy which takes 0 modifications from deploy whatsoever, thus taking all code from master. But it doesn't rewrite history, and you'll be able to push deploy without --force.

Or, for the benefit of a less clunky history (thank you eftshift0 for the neat trick), you can alternatively go for this approach :

git checkout --detach master
git merge -s ours deploy -m "setting content just like master"
git branch -f deploy
git push some-remote deploy

Upvotes: 4

Related Questions