Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branching from an older revision and rolling back the current branch

Tags:

git

I don't know Git all that well and for one of our repositories, I made a mistake.

I committed and pushed changes to a branch named "core". But then I realised that my changes should not be there - I should've created a new branch several revisions ago, say, "core-experimental".

To explain, I have:

A---B---C---D---E     "core"

But now I want to change it to

A---B              "core"
    \
     C---D---E     "core-experimental"

No one else in my team has pulled my changes yet, so any reverts I do shouldn't cause pain to anyone.

Is this possible for Git?

like image 369
aberrant80 Avatar asked Nov 04 '10 04:11

aberrant80


People also ask

How to work with Git branches?

Git Branch 1 Working with Git Branches. In Git, a branch is a new/separate version of the main repository. ... 2 New Git Branch. Let add some new features to our index.html page. ... 3 Switching Between Branches. Now let's see just how quick and easy it is to work with different branches, and how well it works. ... 4 Emergency Branch. ...

Can I create a branch from a previous commit in Git?

You can create a branch from a previous commit on an existing branch. Remember, a commit is just a snapshot in time of the files in a repository. You create a branch from a commit if you want to work on a specific snapshot of the files. Before creating the branch, you need the SHA-1 identifier of the commit.

How do I REBASE changes in Git?

In Git, this is called rebasing . With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch. For this example, you would check out the experiment branch, and then rebase it onto the master branch as follows: $ git checkout experiment $ git rebase master First, ...

How do I find a stale branch in Git?

Go back to previous commit and discard all the new updates after that. This is probably the easiest one. The steps to follow are, git log to check the commit hash for the previous commit you are looking for This will automatically go to the commit and show the stale branch.


3 Answers

The other two answers work fine, but you can actually avoid having to do anything in your work tree:

# create core-experimental using core as starting point
git branch core-experimental core
# move core
git branch -f core <SHA1 of B>

This way you can do it even if you have local modifications in the work tree, and without updating a bunch of timestamps during checkout/resets which will cause you to have to rebuild (assuming this is a compiled project).

like image 79
Cascabel Avatar answered Oct 11 '22 05:10

Cascabel


git checkout core
git branch core-experimental
git reset --hard <SHA of B>
git push -f <remote> core

Or more descriptively...

  1. Checkout core
  2. Create the experimental branch at core's HEAD
  3. Reset core's HEAD back to where you want it
  4. Force a push of the update core
like image 33
dahlbyk Avatar answered Oct 11 '22 04:10

dahlbyk


In core:

git branch core-experimental
git reset --hard <revision-B>

And then:

git push -f
like image 21
Adam Vandenberg Avatar answered Oct 11 '22 04:10

Adam Vandenberg