Abhishek Arya
Abhishek Arya

Reputation: 500

Sync master branch with develop

I'm a contributor to an open source organization and one of our developers was working with the master branch and wanted to sync master with develop. But due some conflicts and all the history got messed up a bit. I would like to know how can master be restored to it's previous state or the best thing be in synchronization with the develop.

This is the repo for reference

https://github.com/systers/vms

EDIT1

Till now, I have tried doing

git checkout master

git reset --hard develop

This made my master in sync with develop locally but I'm not sure whether it's the best practice to do.

Further, I plan to do

git push --force origin master

EDIT2

The above solution didn't work as origin/master is a protected branch. So, I got this error:

remote: error: Cannot force-push to a protected branch

Upvotes: 2

Views: 3927

Answers (1)

VonC
VonC

Reputation: 1324317

so what else can be done? That's what I am more interested in.

An alternative is to create a new commit, mirror copy of an old valid commit.
You can then push that new commit without having to use --force.

First, mark the current HEAD of master: we need to move that HEAD without modifying master, so let's create a new temporary branch called 'tmp' where master is.

git checkout master
git checkout -b tmp

Then let's go back to an older commit with the right content.

git reset --hard <old_commit>

That reset the index (and working tree) to the right content, but that also moves HEAD. Fortunately, that moves tmp HEAD, not master HEAD.

Now, let's move back tmp HEAD to where master is, but without modifying the index or the working tree (which are representing what we need for a new commit)

git reset --soft master

Let's make a new commit, on top of master/tmp HEAD, which represents the right content (the old commit).

git commit -m "new commit, image of an old one"

Finally, we force master to be where tmp is: one new commit later.

git branch -M tmp master
git checkout master

The idea behind creating a new commit, instead of changing the current history, is that a regular git push is enough, and your colleagues can simply pull as usual, and still get the old reset content:

git push

Upvotes: 1

Related Questions