Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always overwrite a git deployment branch

We work with front-end code, e.g. HTML, CSS, JS etc

We have a preview branch (qa) on Github that, when pushed to, automatically deploys the code to a preview website for our QA team to test.

We compile / build our code into a dist directory and this is served on the preview website. Of course, typically the dist directory would be ignored.

How can we push our development branches and always overwrite our preview branch?

We want to ignore any conflicts and always use the new code we are pushing. The code in the preview branch should never seep back into the development branches. We never ever work on code directly in the preview branch.

Context

We are a small, but geographically distributed team and we work on large front-end projects with several release waves. There is no production as such because our work is supplied to other teams who manage integration with internal CMS systems etc.

The master branch represents the last release. Typically we will work sequentially, i.e. work on wave1 and release to master, work on wave2 and release to master and so on. However sometimes we will need to apply a hotfix to wave1 while we are working on wave2. The fix in wave1 will need to be temporarily deployed to qa for testing.

We create feature branches and use Pull Requests to merge them into the current wave branch.

Currently we use this strategy to deploy the current wave to qa:

git push origin wave2:qa

However this often results in a conflict in the dist directory, in which case we do this:

  1. git checkout qa (checkout the qa branch locally)
  2. git pull (make sure it is up to date)
  3. git checkout wave2 (switch back to wave branch)
  4. git merge -s ours qa (merge qa branch in, using the ours strategy)
  5. git checkout qa (switch back to qa)
  6. git merge wave2 (merge wave2 in)
  7. git push (push to deploy)

Now I've noticed that this really screws with our commit history and if we ever need to deploy an older wave to qa for a hotfix test we can end up with commits (and code a couple of times) in our wave1 branch that should only be in our wave2 branch.

It all feels a bit wrong to me, but I'm not sure what the best solution is here. I think perhaps we're trying to fit a square peg into a round hole using this ours strategy.

Should we be using git push origin wave2:qa --force instead?

Or perhaps we should have a different remote for the preview branch?

Or maybe the build step should happen on the server so that the dist directory isn't in the repo?

Happy to work outside our current strategy here as we'd like to move to a more strict git flow model anyway.

All we really need is a technique that allows us to easily deploy our work to a preview site without encountering conflict issues or cross-contamination of branches / releases.

like image 376
Daniel Crisp Avatar asked Dec 21 '15 16:12

Daniel Crisp


People also ask

How do I replace all files from one branch to another in git?

Use git merge and deal with the merge conflicts or git rebase and rewrite branch history on admit_card. In both cases, git reflog will allow you got go back to an older version. You can even tag the current state of the two branches to make it easier to checkout the original versions at a later time.

Does git checkout overwrite changes?

Checkout old commits Since this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.

How do I force a branch to checkout?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

Would be overwritten by merge Cannot merge?

The “Your local changes to the following files would be overwritten by merge” error occurs when you try to pull a remote repository to your local machine whose contents conflict with the contents of your local version of the repository. To fix this error, either stash your changes away for later or commit your changes.


1 Answers

How can we push our development branches and always overwrite our preview branch?

Should we be using git push origin wave2:qa --force instead?

git push -f ... 

The -f (--force) is for force push. It will overwrite the current content of your remote branch. You just have to verify that you have the permissions of doing so.

like image 193
CodeWizard Avatar answered Oct 03 '22 10:10

CodeWizard