Frida M. O.
Frida M. O.

Reputation: 1

Does "git reset --hard HEAD~1" on main branch remove this commit from another branch's history (when that branch is based off main)?

Let's say I have a, b, c, d commits on main. I use "git checkout -b new-branch" while on d. On new-branch I make commits e, f.

I want to remove d from main, but keep it in new-branch. If i run "git reset --hard HEAD~1" in main, will that remove d from new-branch? And will If not, will I still keep d on new-branch if I run "git rebase 'main'?

Current commit histories:

main: a, b, c, d
new-branch: a, b, c, d, e, f

Desired commit histories:

main: a, b, c
new-branch: a, b. c, d, e, f

(Possible future desired histories:)

main: a, b, c, g
new-branch (after rebase): a, b, c, g, d, e, f

(I know how reset/rebase/git works normally, and am asking for this very specific scenario, as I can't seem to find any answers to this exact case)

Upvotes: -2

Views: 37

Answers (1)

Frida M. O.
Frida M. O.

Reputation: 1

After doing some testing I found out that the things I want to do work as I hoped.

To elaborate, running "git reset --hard HEAD~1" in main did not remove d from new-branch. I could also rebase new-branch on main without issue.

When I added a new commit, g, to main after resetting it, I was able to rebase new-branch on main without loosing d, but I had a conflict if there were changes to the same file. So I had to fix those (used 'accept both' in this case), and then was able to have the new-branch history containing commits à, b, c, g, d, e, f`.

Upvotes: -1

Related Questions