I have two branches main and feat1. Unfortunately, due to a serious of events, the commit history in feat1 is super weird such that I cannot squash commits anymore. This makes rebasing on main a nightmare. Is there a way to create a new branch, with has the code of feat1 but git views all differences from this new branch and main as if I had just done them in the code myself?
A brute force solution would be: (1) checkout feat1 and copy and paste all project files and temporarily save them "somewhere else". (2) checkout main and create a new branch, (3) overwrite all files on the new branch with the copied and pasted files.
Is there a less hacky way to achieve this?
CodePudding user response:
If you do not care anymore about history, then just do a merge and squash.... that is, if all changes that you have in your branch outside of the main branch are related to your branch only (did not pick up stuff from other people). If that is the case:
git checkout my-branch
git merge main-branch
# when you wrap that up:
git reset --soft main-branch
git commit -m "All changes in a single shot"
CodePudding user response:
You can soft reset to the old commit (commit_hash) in feat1
branch, the recently changed files will be unstaged
git reset commit_hash
CodePudding user response:
eftshift0's answer should work fine, but I'd like to propose a one-liner solution using the low-level plumbing tools:
git branch -f your_branch "$(git commit-tree -p master -m 'new commit message' your_branch^{tree})"
git commit-tree
will create a new commit object with the specified parent commit, commit message, and tree object; and prints the hash of the created commit. This allows you to recreate your branch from the given commit with git branch -f
.
After that, you can switch to your branch or execute other commands with it.