0

Consider the following Git graph

master:   A - B - C - D - E*
               \         /
feature:        X - Y - Z

I want to make a new commit in master, E, such that E precisely matches Z. In other words, any changes that may have been made in commits C and D are completely discarded. However, I would still like to retain the property that doing git checkout HEAD~1 in master would checkout D.

Doing git checkout master; git reset --hard feature gets the working directory in the proper state (i.e., all tracked content precisely matches that of Z). However, is does not leave the history in the way that I intend.

Doing something like git checkout feature -- . almost works, but retains any files created in B or C, so it won't always precisely match the state of Z.

Doing something suggested in other responses to similar questions seems quite inefficient, as it involves several different checkouts and merges and is something I need to specifically avoid, as this operation will be done frequently and on rather large repositories.

It seems like years ago someone asked a similar question and got this response: https://stackoverflow.com/a/4912267/927604

Part two of this question is: Seriously? Unless I'm missing something major, why is this not obvious to do?

Thanks!

Bill VB
  • 2,115
  • 8
  • 25
  • 34
  • why don't you `git revert` C and D, and then merge feature? – Rom Grk Nov 27 '18 at 22:23
  • Interesting, so do like `git revert A` (to be sure it's in a descendant branch of _feature_) and then `git merge feature`? – Bill VB Nov 27 '18 at 22:28
  • This is really a "why isn't there a `git merge -s theirs`" question. You linked one of them; the other that I recall is https://stackoverflow.com/questions/173919/is-there-a-theirs-version-of-git-merge-s-ours – torek Nov 27 '18 at 22:35
  • My preferred method for doing this (not that I've actually ever needed or wanted to do this!) is in [Michael R's answer](https://stackoverflow.com/a/29806926/1256452), but [jthill's answer](https://stackoverflow.com/a/16526138/1256452) is good too. Note that you can wrap either one into an alias, if you really need to do it repeatedly. – torek Nov 27 '18 at 22:36
  • @torek I mention Michael's command in an alias in https://stackoverflow.com/a/46741538/6309 – VonC Nov 28 '18 at 05:50

0 Answers0