M
M
massef2019-05-07 18:14:35
git
massef, 2019-05-07 18:14:35

How to revert to an old branch?

Hello. The point is the following.
An external developer created a branch conditionally [branch-1] made changes to it and pushed it to a remote repository.
Next, I fetched all the new branches and switched to the [branch-1] branch that appeared, made a lot of changes and pushed. In parallel, other developers performed their tasks and eventually all this was merged into the master, including the [branch-1] branch with my changes.
Found a bug. The master will simply roll back, there is a risk of breaking something, because a lot of the code base was affected, it is also difficult to fix in the current version. You need to roll back the [branch-1] branch to the initial state and push as is. I have doubts about how to do it right.
1. Create a branch from the current master/dev, rollback the [branch-1] branch locally with git reset --hard HEAD^ to the commit you want, and merge it into a new branch?
2. The same thing, but instead of git reset, switch to the desired commit by its hash?
3. ....?
In both cases, it is not clear what will happen when pushing, since there are already rolled changes above.
I am sure of one thing for sure, that specifically in this branch nothing more is affected and you can safely roll it back.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shitskov, 2019-05-07
@Zarom

The correct way is to do a git revert on the problem branch and then merge the changes.
In all other cases, there is a high chance of breaking something, and at the same time, you will have to force push in at least 2 branches, incl. to master.

I
Igor Deyashkin, 2019-05-12
@Lobotomist

Briefly
In your case, you need to branch from the current master, reverse unnecessary changes (all the changes of the branch, apparently) and merge with the master. If after branching the "rollback branch" new changes appear in the master, you should first merge them into the branch, resolve possible conflicts, test, and only then merge this branch into the master.
git reset --hard commitref , executed in master will transfer its pointer to the commitref commit and this is not an option in your case under any circumstances, since master is a public branch and you cannot overwrite history in it.
You can no longer remove commits from the master. The only option left is to create a commit(s) that rolls back some of the changes. That's what the team does.git revert. If you need to rollback everything that was merged into the master when branch-1 and master were merged, you can use the -m option to specify which commit to rollback relative to.
First, find out what the previous (before branch-1 ) commit number was on master . One option is to run a command where branch-1-mergeTo-master is the merge commit of branch-1 and master .
The commit information will contain a line like this:

*   commit 5788ae1df77ce0911f802530b1208f1709c102d4
|\  Merge: 04d469b 57ed6bd

Let's say the master commit has id = 04d469b . Then its number is 1 . We do a reverse. Example commands:
git checkout master
git checkout -b branch-1-revert

git revert -m 1 branch-1-mergeTo-master

git commit -m "branch-1 reverted from master"

git merge master
git checkout master
git merge branch-1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question