moving changed files to another branch for check-in

前端 未结 4 1624
独厮守ぢ
独厮守ぢ 2020-12-22 15:24

This often happens to me: I write some code, go to check in my changes, and then realize I\'m not in the proper branch to check in those changes. However I can\'t switch to

4条回答
  •  粉色の甜心
    2020-12-22 16:17

    If you haven't already committed your changes, just use git checkout to move to the new branch and then commit them normally - changes to files are not tied to a particular branch until you commit them.

    If you have already committed your changes:

    1. Type git log and remember the SHA of the commit you want to move.
    2. Check out the branch you want to move the commit to.
    3. Type git cherry-pick SHA substituting the SHA from above.
    4. Switch back to your original branch.
    5. Use git reset HEAD~1 to reset back before your wrong-branch commit.

    cherry-pick takes a given commit and applies it to the currently checked-out head, thus allowing you to copy the commit over to a new branch.

提交回复
热议问题