0

I have a question concerning git, which probably has been solved already, but I am not able to find the right keywords.

I have two branches master and feature. I merged feature in master to see if there are any conflicts. Afterwards I discarded the changes of the merge because I did not want to merge it yet. However, I forgot to undo the merge and now master apparently includes the commits of feature but not the actual changes made. So master is "already up-to-date" but does not include any changes of my feature branch.

How do I get the changes of feature into the master branch?

Thanks in advance!

Florian
  • 39
  • 6
  • Does this help? https://stackoverflow.com/q/2389361/5024726 (be careful with the commands shown and maybe make a local backup if you have changes that are not pushed yet) – Matt Jun 04 '21 at 10:22
  • There are further commits in the master branch now so doing a reset doesn't work :( I guess I need to somehow figure out the diffs between the two branches and do a merge but usually that also works with the commit-sha which is apparently included in the master – Florian Jun 04 '21 at 10:27
  • How did you discard the changes of the merge? – Lasse V. Karlsen Jun 04 '21 at 10:33
  • I used the tool in VS Code to discard the changes. – Florian Jun 04 '21 at 10:38
  • Using `git replace --graft`, you can pursade Git that it has not yet merged the `feature` branch into `master`, as described in [this answer](https://stackoverflow.com/a/57905240/6868543). – j6t Jun 04 '21 at 11:33
  • [This question](https://stackoverflow.com/q/67801433/3216427) had a similar problem, but I'm not sure my solution there applies here, since you have different commits you want to keep on both branches. It might still be informative reading, though. – joanis Jun 04 '21 at 12:43
  • To circumvent this issue: use the --no-commit parameter when merging – TheDudeWithHat Jun 04 '21 at 14:24

1 Answers1

0

As I understand it, your situation is this:

  • you have a feature branch with some commits we'll call X, Y, Z
  • you have a master branch where the history shows those commits in between other commits, e.g. A, B, C, X, Y, Z, D, E, F
  • the actual changes in X, Y, Z weren't applied, and now you want to apply those changes

The bad news is, as you've found, that a plain "git merge" will just tell you that everything on your feature branch is already on master.

The good news is that it should be fairly straight-forward to create a new feature branch, with all the changes, but as new commits. Then when you merge to master, it will merge the changes in as you expect.

The way to do this is with git rebase, which takes a series of commits and re-creates them with a new parent. The argument you need to pass to git rebase is the last commit before the changes you want to rebase; there are a few ways to specify it:

  • Find the relevant commit hash in the logs
  • Find the first commit hash you do want to keep, and add "^" which means "parent of", e.g. "abc123^"
  • Count the commits you want to keep, and use "~" notation, e.g. "HEAD~3" means "go back 3 commits"

To confirm what's going to be rebased, use the "-i" (interactive) flag; that will pop up an editor: save and exit to go ahead, or delete all the lines shown to abort the rebase.

git rebase -i HEAD~3
IMSoP
  • 89,526
  • 13
  • 117
  • 169