1

I tried revert but it kept giving me annoying conflictions.

I want something like this:

A-B-C-D-E-F

A-B-C-D-E-F-D'

is there a simple way of doing this.

4
  • Why would you want to play a copy of a Git commit on the HEAD, and what would even be the meaning of doing that? Commented Aug 16, 2016 at 5:00
  • i messed up my previous commits but I want to keep them as history Commented Aug 16, 2016 at 5:01
  • Are there specific elements of E and F that you want to preserve? Why not just revert F and E and be done with it? A revert on the current HEAD will never cause a conflict. Commented Aug 16, 2016 at 5:01
  • @TimBiegeleisen I think this makes sense in a number of situations when commits are not necessarily bad code. In my particular case, E and F are commits where I made my model more accurate but far slower compared to D. By doing what the OP wants, one can revert to D but still revisit E and F and keep the learnings from the development process. Commented Sep 22, 2023 at 0:23

2 Answers 2

2

As far as I can tell from your post and comments, you want to revert F and E, but keep them as history. What you can do is this:

git revert HEAD~n..HEAD 

Where you replace n with far however back from HEAD (your current commit) the commit after D is.

What this will do is revert every commit between HEAD and the commit after D in reverse order, ensuring no conflicts.

This becomes substantially more complicated when there are merge commits in that range. If there are, do something like this:

git revert -m 1 HEAD~n..HEAD 

Which will prefer the first parent branch of a merge commit, and remove changes introduced by merging in other branches. Do this with great care, as it may not end up doing what you actually want.

Sign up to request clarification or add additional context in comments.

Comments

0

In opposition to the accepted answer, I would suggest (assuming that you are talking about the master branch, which you want to set back a few commits while keeping the skipped ones around somewhere):

git checkout master git checkout -b old_master git branch -f master D git checkout master 

This gives you a very clear history, and the junk you removed is still around

 master | v A-B-C-D \ E-F ^ | old_master 

Any further commits will lead to a diverging history...

 master | v A-B-C-D-G-H \ E-F ^ | old_master 

So E and F will never muddle up your "real" history, but you will always be able to get back to them. If and when you decide that you finally do not need them anymore, you just git branch -D old_master and they are gone without any trace.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.