SilentDev
SilentDev

Reputation: 22777

How to make branch the same as origin/master

suppose I branched from origin/master to brancha.

I made, committed and pushed changes to brancha (this includes creating, committing and pushing new files).

Now, I want to make brancha the exact same content as origin/master again (because, say my changes were wrong and a better solution was put on master).

So I want my branch to basically be a new branch off of origin/master, but I want my old changes to have been recorded so I can see it later on in the history.

Is there a way to do this?

Upvotes: 0

Views: 3835

Answers (3)

Fran Bonafina
Fran Bonafina

Reputation: 150

git checkout master
git reset --hard && git clean -dffx

Upvotes: -3

VonC
VonC

Reputation: 1329082

If you want to create a new commit on top of your brancha commits (that you want to keep), reflecting origin/master, I would suggest a reset --hard then soft (as in "Practical uses of git reset --soft?").

m--m--m (origin/master)
       \
        a--a--a (brancha, HEAD)

First, reset the index and worktree to what you want to see: origin/master

git checkout brancha
git branch tmp
git reset --hard origin/master

m--m--m (origin/master, brancha, HEAD)
       \
        a--a--a (tmp)

Problem: brancha history is not longer referenced: move brancha HEAD back to its original position, but without changing the index and working tree (which both reflect origin/master)

git reset --soft tmp

m--m--m (origin/master)
       \
        a--a--a (tmp, brancha, HEAD)

Now you can add and commit: that should compute the relevant delta to set a new commit with the same content as origin/master.

git add .
git commit -m "Restore origin/master content in a new commit on brancha"

m--m--m (origin/master)
       \
        a--a--a----M (brancha, HEAD)
             (tmp)

M and origin/master commit should have the same content.

Upvotes: 0

WofWca
WofWca

Reputation: 706

For changes to be saved you need a pointer to its current tip – e.g. tag or another branch.

git tag <tag_name> brancha

Then reset the branch so it matches the current origin/master:

git checkout brancha
git reset --hard origin/master

Upvotes: 4

Related Questions