Consequences
Before doing this you should be aware of the consequences. If anyone else is using the same remote and also has a local copy of stable
, them pulling the new branch will lead to a merge of your new stable
and their local version of the old stable
, resulting in a combination of the changes from both branches (supposedly with huge merge conflicts).
Probably the best solution to this problem is that everyone who is using that remote deletes the stable
branch in their local repository as soon as you have replaced the old version and then fetches the updated version of stable
from the remote:
git checkout develop
git branch -D stable
git fetch
git checkout stable
This first makes sure something different than stable
is checked out as the branch currently checked out cannot be deleted. Then the local stable
is deleted, the current changes from the remote are fetched and the new stable
is checked out.
Solution
First you should create a local stable branch with the content of the previous develop branch by deleting the old local stable branch and then creating a new one:
git checkout develop
git branch -d stable
git checkout -b stable
Trying to push this branch to the remote will fail as the remote branch contains changes your local branch does not contain. You will receive some message like this one:
Updates were rejected because the tip of your current branch is behind
its remote counterpart. Integrate the remote changes (e.g. 'git pull
...') before pushing again.
There are two ways to work around this. They both lead to the same result, deleting the old stable
from the remote and replacing it with your local new version of stable
.
git push --force
You can tell git to ignore and discard the remote changes by using the parameter --force
.
git push --force stable
Delete the remote branch first
Alternatively you could manually delete the remote branch and afterwards push the new version:
git push :stable
git push origin stable
The first parameter of git push
is where to push something and the second parameter is what to push where, divided by a colon. git push origin a:b
would push your local branch a
to origin using the name b
on origin. By leaving out the a
part "nothing" will be pushed to stable
, leading to deletion of stable
on the remote.
Process improvement
It might be a good idea to change your process such that develop and stable stay closer together so you don't need to discard the old stable
but can merge your changes from develop
to stable
.
Maybe you could do regular merges of stable
to develop
to keep them in sync. To simplify this you could also do fixes and changes required in both versions in stable
and then merge them to develop
. However how good this might work depends heavily on your processes and how far stable
and develop
drift apart.