Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create commit to overwrite current branch contents with another's

Tags:

git

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.

like image 769
Nick Avatar asked Feb 16 '12 08:02

Nick


1 Answers

$ 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.

like image 121
Amber Avatar answered Oct 06 '22 18:10

Amber