-2

enter image description here

See picture, as probably easier to visualize. But basically, I reset to an old commit from main that occurred after I created the branch. After this happened it appeared that I lost all commits from my branch (filled dots on diagram).

I guess this makes sense as the commits from main had no knowledge of the branch, but just wanted to make sure this is the expected behaviour. In the future I think I will try to create new branches before doing this or using 'revert' instead of 'reset'

Thoughts?

toka1300
  • 1
  • 2
  • 1
    Don't post a picture of handwriting or text, please type it out. Also, there are parts of your question don't really make sense: "reset to commit" - that's not a thing, "I reverted ... [and] lost all commits from my branch" - reverting will not "lose commits", and your picture doesn't fully make sense to me either. You'd be better off pasting the output of: `git log --graph --oneline --decorate main your-branch-name` – Kache Jun 16 '23 at 16:43
  • @Kache Certainly reset to commit is a thing. All resetting is resetting to a commit. That's what reset is. – matt Jun 16 '23 at 16:47
  • The problem here is that reset and revert are completely different things so the question is very confusing. – matt Jun 16 '23 at 16:47
  • We need to know exactly what command you gave to Git, in order to answer the question. Did you say `git reset --hard`? Or simply `git reset`? Or what? – matt Jun 16 '23 at 16:52
  • 1
    @Kache And there is nothing wrong with posting a hand-drawn diagram. – matt Jun 16 '23 at 16:53
  • Nothing was lost, you just don't have a reference that points to those commits. Look in reflog and, and you can reset your branch back to its old location, or create a new branch there. – Useless Jun 16 '23 at 16:57
  • oh, "reset to _**a**_ commit", I was taking "commit" as a verb, there. – Kache Jun 16 '23 at 17:01
  • In that case, from given descriptions, it's likely that "resetting a branch to a commit on old main" has indeed "lost commits from the branch". Note that the "lost commits" will remain in git for a while, just hidden b/c nothing is referencing them. Will also need better specifics to say precisely what happened and steps to recovery, e.g. something like https://stackoverflow.com/questions/10099258/how-can-i-recover-a-lost-commit-in-git – Kache Jun 16 '23 at 17:06
  • Sorry for the confusion. I did mean to say that I 'reset', not 'reverted'. The command I used was 'git reset --hard' – toka1300 Jun 16 '23 at 17:12
  • See https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard – matt Jun 16 '23 at 17:38
  • And https://stackoverflow.com/questions/8358035/whats-the-difference-between-git-revert-checkout-and-reset – matt Jun 16 '23 at 17:43
  • @CaseyTokarchuk I super highly suggest this interactive git tutorial: https://learngitbranching.js.org – Kache Jun 17 '23 at 04:02

1 Answers1

0

I just wanted to make sure this is the expected behaviour.

Yes this is the expected behaviour. Because you have reset your main branch to a commit before the merge commit you lost all the changes that were made in this branch.

The correct way would have been to create a temporary branch at the last commit of the feature branch (the rightmost filled dot) and after you reset main you merge this temporary branch into main.

SebDieBln
  • 3,303
  • 1
  • 7
  • 21
  • The correct way to do what? Wouldn't that cause you to end up with the very same topology you started with? One of the things I have not understood here is what the OP's purpose was in resetting in the first place... – matt Jun 16 '23 at 21:55
  • @matt Based on the image the OP posted I assume he wanted to get rid of the commit immediately before the merge commit and the two commits after the merge commit. Based on the surprise of the OP finding out that all of the "branch" changes are now also gone I assume he does want to keep the changes made in that branch. – SebDieBln Jun 17 '23 at 08:44
  • @matt *"Wouldn't that cause you to end up with the very same topology you started with?"* Note that the temporary branch is not the "rightmost dot" but the "rightmost *filled* dot. – SebDieBln Jun 17 '23 at 08:50
  • If the idea is to impose the state of the arrowtail circle on the commit at the arrowhead circle, then the solution would be `reset --soft` and `commit`. But I don't think we know that that is what the OP wants. I don't know it, I don't think you know it, and I don't think the OP knows it. We just have to wait for the OP to describe the desired result rather an posing an xy problem that postulates a failed solution. And we can't do that until the OP knows what the OP wants, which I don't think is the case. – matt Jun 17 '23 at 13:41
  • The rightmost dot is still my branch, it is simply after main was merged into it, the main branch was never touched. – I was having unexpected behaviour on the branch, so all I wanted to do was to go back to a previous commit where I know it was working, as expected - I guess the issue is that I chose a commit that was merged in from the main branch and not made directly on my branch. – toka1300 Jun 17 '23 at 20:15