0

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?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • that doesn't make anysense. if you have those other commits in your history, then its not the same. – Daniel A. White Mar 17 '19 at 02:14
  • why not just create a new branch, and leave the old one alone? – jdigital Mar 17 '19 at 02:15
  • @DanielA.White Sorry, I don't mean "same" as in same history as well. I mean, same code and file structure. So, `brancha` should have the same code and file structure as `origin/master`. – SilentDev Mar 17 '19 at 02:16
  • @jdigital the branch is called `front-end-dev`. I will still be working on front end development. That is why it would be nice to have the same name. Now that I'm thinking about it, it would also be good to create a new branch for front end development (and leave the old branch alone, since the changes no longer are needed). It's just because of the name that I wanted to keep the same branch (the reason for the branch will be the same). – SilentDev Mar 17 '19 at 02:18
  • see [How do I rename a local Git branch?](https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch) – jdigital Mar 17 '19 at 02:21

3 Answers3

1

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
WofWca
  • 571
  • 3
  • 11
0

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-2
git checkout master
git reset --hard && git clean -dffx
Fran Bonafina
  • 132
  • 1
  • 8