Reset remote git branch

I have two remote branches in git, master and test. master reflects whatever will be deployed to our production environment next. test is the same, but for our testing environment.

Because of stalled or scrapped projects test could, over time, theoretically accumulate a lot of commits not present in master. How do I avoid this?

I know one solution would be to delete my local and remote test branch. Then create a new one based on master and push that to remote. However this approach forces my colleagues and build/deploy systems to also delete their local branches before pulling a the new one.

  • GitHub collaborators cannot download private repo via NPM package
  • Difference between git log with and without <path> when only <path> was changed?
  • Jenkins git submodule update fails
  • Deploy to AWS Elastic Beanstalk without polluting my Git repository?
  • Finding diff between current and last versions?
  • commit not visible on github after push
  • How to put disk size quota on a git repository?
  • Merge an untracked directory into the tracked directory
  • One Solution collect form web for “Reset remote git branch”

    Another approach, allowing to pull the test branch is to reset the index of test to the one of master, not rewriting the history of test, but adding a new commit which would reflect the same state as master.
    That would however leave in test history all the extra commits.

    git checkout master
    git merge -s ours test # make master believe that everything from test has been
                             merged to master up to this point.
    git checkout test
    git reset master -- . # reset the index content of test to the index of master
    git checkout # reset the working tree of test to its new index
    git commit -m "test reset to master"
    git merge master # should be trivial merge
    git push origin master
    git push origin test
    
    Git Baby is a git and github fan, let's start git clone.