Nick
Nick

Reputation: 5805

Create commit to overwrite current branch contents with another's

I would like to create a single commit that will make the current branch identical in contents (and implicitly in sync) with another branch.

This is practically the equivalent of copy pasting the checkout contents of another branch on top of the current branch and committing everything in one go.

Upvotes: 12

Views: 1587

Answers (1)

Amber
Amber

Reputation: 526753

$ git reset --hard <another branch>
$ git reset --soft HEAD@{1}
$ git commit

The first (hard) reset grabs the contents of the other branch into your working directory. The second (soft) reset puts your commit pointer back at the tip of your original branch, but doesn't change the files in your index at all, thus leaving them as they were in the other branch. You can then commit that state on top of your current branch's latest commit.

Upvotes: 18

Related Questions