guettli
guettli

Reputation: 27796

Clean up history of a git branch

I have two branches feature/foo and feature/foo-clean-history.

Both branches contain the same source code, there is not difference, except that the first one has way too long history, and the second has a clean history.

Since AFAIK (Changing source branch for PR on Github) it is not possible to change the source branch of a github PR, I would like to feature/foo to have the same clean history like the feature/foo-clean-history.

How could I get to my desired state?

Upvotes: 0

Views: 289

Answers (2)

Philippe
Philippe

Reputation: 31097

I would like to feature/foo to have the same clean history like the feature/foo-clean-history

# Save changes and clear your working directory
git stash save

# Checkout the `feature/foo` branch
git checkout feature/foo 

# Reset to let `feature/foo` point the same commit than `feature/foo-clean-history`
git reset --soft feature/foo-clean-history

# Here you should have no changes in the staging area.
# If there are some, then the code in the 2 branches wasn't exactly the same,
# do what you think is good with these changes (i.e. commit or reset changes)

# Update remote `feature/foo` and so the PR
git push origin --force-with-lease

Upvotes: 2

The only thing you really have to do is remove "same" branch (git branch -d branch-name) and, maybe, create one with the same name from the clean one with git checkout -b new-branch-name, being in the branch, you want to "clone".. if you want to delete branch remotely, use git push origin --delete remoteBranchName

Upvotes: 0

Related Questions