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?
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. ...
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.
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, ...
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.
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).
git checkout core
git branch core-experimental
git reset --hard <SHA of B>
git push -f <remote> core
Or more descriptively...
In core:
git branch core-experimental
git reset --hard <revision-B>
And then:
git push -f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With