-2

I have this problem, i'm working in a new feature for my code, but accidentally i wrote my code in dev branch and commited my changes (i had to create a new branch).

I need to put those changes in feature_branch and revert dev to the previous commit.

I was thinking on create a branch from dev and then revert dev but i'm not doing well.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • 1
    Please be more specific about what problem you're facing. Don't make us guess what "not doing well" means. – isherwood Oct 13 '21 at 13:42
  • You can use [`git cherry pick`](https://stackoverflow.com/q/9339429/4862445) to apply the new commits to the other branch, and [`git reset`](https://stackoverflow.com/q/927358/4862445) to unwind them. – Richard Smith Oct 13 '21 at 13:42
  • Do your dev branch tracks to any remote ? If so did you push after commit? If not then everything seems to be pretty simple. Just do `git checkout -b feature_branch` on your current local dev, then `git branch -D dev` (only locally) and then `git checkout dev`. – running.t Oct 13 '21 at 13:55
  • [This answer](https://stackoverflow.com/a/63905413/184546) contains the exact steps from the command line (in the side note at the bottom), even though the question asked how to do it in Visual Studio also. – TTT Oct 13 '21 at 15:43
  • @RichardSmith cherry pick will definitely work, but is unnecessary in this case since the current `dev` branch is what the new branch needs to be. – TTT Oct 13 '21 at 17:15

3 Answers3

1

A few important points:

  • Commits in git are immutable
  • Commits point to zero or more "parents", and history is always examined by following these backwards
  • Branches are just a pointer to a particular commit, and from there other commits can be reached
  • Branches can be freely moved to point to any commit you want

So, your history looks something like this (the backwards arrows represent the "parents" recorded in each commit):

A <--B <--C <--D
               ^
               |
             (develop)

And you want it to look like this:

A <--B <--C <--D
          ^    ^
          |    |
   (develop) (feature_branch)

So, firstly, with your altered develop checked out, run git branch feature_branch, giving this:

A <--B <--C <--D
               ^
               |
              (develop, feature_branch)

Then find the actual commit hash of commit C (where develop should be pointing) using git log, and use git reset --hard the_commit_hash_you_found to point develop back at that commit. (Important: This will wipe out any uncommitted changes. Commit them or use git stash before you begin!)

A <--B <--C <--D
          ^    ^
          |    |
   (develop) (feature_branch)

An important note: If you have pushed the version of develop with the changes to a shared server, or anyone else has based anything on it, there is more to do. But if you've just done this locally and not pushed anywhere, the above is all you need.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • What is it that i have to do if i pushed develop ? – Santiago Arevalo Gómez Oct 13 '21 at 15:54
  • @SantiagoArevaloGómez The short answer is 1) force push the new develop branch and 2) rebase any branches which were based on the "wrong" history. It's really important to understand the implications of that, though, otherwise you'll end up with even more confusion, so rather than giving you the commands here, I recommend you read some general advice on "rebasing", "force push" and "rewriting shared history". – IMSoP Oct 13 '21 at 16:17
0

if I understand, you could try with git stash.

you have to run these commands:

on your dev branch:

git stash

on the branch you want to work:

git checkout
git stash pop
A.Seddighi
  • 1,695
  • 1
  • 20
  • 43
jchenaud
  • 35
  • 5
0

If I understand correctly then you have made two commits to dev branch instead of a feature branch. Now you would like to create a new feature branch containing all your feature commits. And you want to remove those commits from dev branch.

At this point you can create a new branch from you dev. It will have all your feature commits in the new branch. Run the following command from your dev branch.

git checkout -b {newFeatureBrnach}

Now to revert your commit from dev branch, first you will need to find the hash for those commits. Go back to your dev branch:

git checkout {devBranch}

List all the recent commits

git log

Find your commits in the log and note down the hash. Then you can use revert to remove those commits.

git revert {hash}

You would have to run it twice to revert both of your commits.

Note that git revert will make more new commits on your dev branch to reverse your changes. If you want to completely remove your commits from the history you will have to use git rebase and force push to the remote branch. This is not advisable if multiple people are working on the repo.

pallavdubey
  • 109
  • 6