0

Having some dissertation panic here :|

Basically, I did something silly, I had two branches off of the master branch working on two separate things: branch A and branch B. This meant 0 conflicts. I first merged branch A into master. All good so far.

I then wanted to update branch B with the changes in master after merging branch A. However, now I made the stupid mistake of merging master into branch B, instead of first merging that branch into master, to then merge master into it.

So I have now lost all my commits since branch B was created. I've read something about revert, but not 100% what this will cause since its not a branch into master, but master into a branch.

To kind of show what I did:

Branch A -> master -> Branch B

...instead of (I think this is how it's supposed to be done):

Branch A -> master <- Branch B and then master -> Branch B

Thanks in advance for any help :)

(also if this is the wrong place to put this question please tell me and I'll move it)

1 Answer 1

0

Merging never loses commits, no matter what direction you do the merge in. You sometimes don't see some commits in git log or in GitHub's commits view, because they attempt to show a linear view of the commit history (while in reality the log has multiple simultaneous commit lines). The commits you just merged into the branch push the original commits so far down the list that you're unable to find them.

Open a tool such as gitk, tig, gitg, or git log --graph --oneline. The topmost "merge" commit will show two lines of work, one belonging to master (or branchA), the other going to what you originally had in branchB.

To throw away the merge commit, while being on branchB, use git reset --hard HEAD^1 (the number indicating which merge parent to select). Before doing so, maybe check the log of HEAD^1 to see what you'll get after the reset.

If you write down the "latest" good commit IDs before doing anything else, you'll be able to return to them even if some git commands don't succeed, as the commits remain stored in .git/ for several more days.

2
  • Ok I so I can see the commits in gitk (thank you!) however when I try git reset --hard HEAD^1 I get the following: fatal: ambiguous argument 'HEAD1': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
    – Sev
    Feb 16, 2019 at 18:44
  • Use HEAD^^1 if you're doing this within Windows' Cmd (Command Prompt). It uses the ^ character for its own purposes. Feb 16, 2019 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .