0

I left my main branch to checkout a specific commit and forgot to go back, resulting in my subsequent commits as being part of that checked-out commit rather than the main branch. Shown in git reflog

f0420e4 HEAD@{1}: commit: :brain: `redesign` attributes as single number -> Attribute object encapsulating .base .modifier
cb4a198 HEAD@{2}: commit: :brain: `redesign` Item Rarity type: string literals -> enum
1d61b75 HEAD@{3}: checkout: moving from main to 1d61b75
70c9cf5 (HEAD -> main, origin/main) HEAD@{4}: reset: moving to 70c9cf5ab06b1838b1b7c4b8278728bedbaecbf5

in main, I don't see the most recent two redesign commits - they seem to be only existing on the 1d61b75 commit. How can I move them to be on the main branch, without having a merge register in my git history?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Michael Moreno
  • 947
  • 1
  • 7
  • 24
  • Does this answer your question? [How to fix committing to the wrong Git branch?](https://stackoverflow.com/questions/2941517/how-to-fix-committing-to-the-wrong-git-branch) – mkrieger1 Apr 12 '23 at 07:13
  • Sorry, I misread, it's this one: https://stackoverflow.com/questions/7124486/what-to-do-with-commit-made-in-a-detached-head – mkrieger1 Apr 12 '23 at 07:15
  • @mkrieger1 you edited my post from "checked-out commit" to "checked-out branch". Does `1d61b75` constitute it's own "branch"? I did not see any additional branches on GitHub after pushing, making me think the commit is simply a commit, probably detached from any branch. – Michael Moreno Apr 12 '23 at 07:16

2 Answers2

3

You can also use rebase:

git rebase HEAD~2 HEAD --onto main

That way you are applying the last 2 commits from where you are standing onto main (main branch won't move, you will need to do that after the fact):

git branch -f main # set main over here
git checkout main 
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 1
    I think the last 2 commands could also be a single command with: `git switch -C main` or `git checkout -B main`. – TTT Apr 12 '23 at 21:12
1

Go back to main and do a cherry-pick for the commits you need:

$ git checkout main
$ git cherry-pick cb4a198 f0420e4

Naturally, there can be conflicts, but if so, just fix them and do git cherry-pick --continue.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • or `git cherry-pick 1d61b75..f0420e4`. Just 2 commits is fine to list one by one but if they were 10 commits, a range is simpler (notice how the range starts on the commit _before_ the first one to apply). – eftshift0 Apr 12 '23 at 07:34
  • 1
    @eftshift0: Yes, nice addition. You can also write `git cherry-pick cb4a198^..f0420e4` to avoid having to find the parent of the original commit manually. – rodrigo Apr 12 '23 at 10:25