How to make git branch's lastest codes same to another branch?

Viewed 38

For example, I have branch dev and master. Now they are different on codes and some binary files. And I want to make the branch dev's last commit exactly same to master branch's last commit. How?

My method is:

git diff dev master --binary > 1.patch
git checkout dev
git apply 1.patch

Is there other easy way?

2 Answers

Branch are just pointers, you can set any ref to them

git branch -f dev master

which means they'll have the exact same history afterwards. (as a sidenote, just checkout another branch before you move this one, or git will complain)

Then again, if you needed to keep everything from dev which is not on master, this is not the way to go. ElpieKay's second solution will then work like a charm.

If you don't mind losing dev's own history,

git checkout dev
git reset master --hard

It makes dev point at master's head.

If you want to keep dev's own history,

git checkout dev
git merge $(git commit-tree -p HEAD -m "xxx" master^{tree})
# edit the commit message
git commit --amend

It creates a commit on dev and the commit has the same code with master.

Related