roomwithaview
roomwithaview

Reputation: 75

How to use git merge to revert master? (i.e. revert VIA merge)

Note: There is no way this is the first time someone has asked this question. However, having searched for it, and only been able to find the answers to questions with similar words in the title but entirely different meanings, I have given up.

If you find the duplicate question, please close this and link to that question.

Background: Let's say you are working on master, and you have a lot of debug logging features. You want to share your code with someone else, but they hate your debug logs.

So, not wanting to distract them, you first make a new branch*, debug_logging, which contains the old (good) version of your code with all of the debug logs in it. Then you delete the debug logs from master. Once the other person has seen your work and is satisfied with it, maybe helped you make some changes, you don't need to hide the debug logs from them and you can put the debug logs back into master. The problem is how to do this.

*Yes, the ideal would be to make the non-master branch the one with the logs deleted, but there are considerations external to this question which make that impossible.

Question: If you want to restore the debug logs, you try to merge debug_logging onto master. But since debug_logging is technically behind master in history, git checkout master; git merge debug_logging does nothing (everything up-to-date).

How can one merge debug_logging onto master in the sense of taking the union of the contents of their files (i.e. re-add the debug logs while keeping any additions made to master while the debug logs were gone)?

This looks something like merge-file would do if it were possible to apply merge-file to all files in both branches in the repository simultaneously. Basically I want Git to stop acting smart, and merge debug_logging onto master the same way it would if they had no history in common.

Upvotes: 0

Views: 24

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83577

Merging only makes sense when one branch is ahead of another. This means that you can only do a merge if you make commits on debug_logging that you then also want in master. You cannot use git merge to revert.

Instead you can use git revert. This command will create a new commit that effectively undoes the changes in one or more commits that you specify. The new commit will contain the reverse changes of the specified commits.

You will need a reference to the commits that you want to revert. The most direct way to refer to a commit is by its SHA hash. You can find the hashes for any commits with git log.

Upvotes: 1

Related Questions