Search code examples
gitbranchcommitgit-branchgit-commit

Git push commit on different branch


I have a git repository with two branches: master and new_feature. The new_feature branch is created based on master to implement a new feature, so before implementing this new feature they are the same.

Now I implemented this new feature and accidentally committed these changes in the master branch, because this branch was still checked out locally. Before pushing to the remote, I realized I committed the changes on the wrong branch. If I now checkout new_feature, the changes do not come along.

How do I undo this commit to master and redo the same commit to new_feature?


Solution

  • Go to the branch where you have accidentally committed your code

    git checkout master
    

    then reset to latest-head

    git reset HEAD-1
    

    And you will get your changes unstaged in master branch, then checkout to new_feature branch and commit your changes.