moving changed files to another branch for check-in

前端 未结 4 1548
独厮守ぢ
独厮守ぢ 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:08

    Sadly this happens to me quite regularly as well and I use git stash if I realized my mistake before git commit and use git cherry-pick otherwise, both commands are explained pretty well in other answers

    I want to add a clarification for git checkout targetBranch: this command will only preserve your working directory and staged snapshot if targetBranch has the same history as your current branch

    If you haven't already committed your changes, just use git checkout to move to the new branch and then commit them normally

    @Amber's statement is not false, when you move to a newBranch,git checkout -b newBranch, a new pointer is created and it is pointing to the exact same commit as your current branch.
    In fact, if you happened to have an another branch that shares history with your current branch (both point at the same commit) you can "move your changes" by git checkout targetBranch

    However, usually different branches means different history, and Git will not allow you to switch between these branches with a dirty working directory or staging area. in which case you can either do git checkout -f targetBranch (clean and throwaway changes) or git stage + git checkout targetBranch (clean and save changes), simply running git checkout targetBranch will give an error:

    error: Your local changes to the following files would be overwritten by checkout: ... Please commit your changes or stash them before you switch branches. Aborting

    0 讨论(0)
  • 2020-12-22 16:17

    git stash is your friend.

    If you have not made the commit yet, just run git stash. This will save away all of your changes.

    Switch to the branch you want the changes on and run git stash pop.

    There are lots of uses for git stash. This is certainly one of the more useful reasons.

    An example:

    # work on some code
    git stash
    git checkout correct-branch
    git stash pop
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 16:23

    A soft git reset will put committed changes back into your index. Next, checkout the branch you had intended to commit on. Then git commit with a new commit message.

    1. git reset --soft <commit>

    2. git checkout <branch>

    3. git commit -m "Commit message goes here"

    From git docs:

    git reset [<mode>] [<commit>] This form resets the current branch head to and possibly updates the index (resetting it to the tree of ) and the working tree depending on . If is omitted, defaults to --mixed. The must be one of the following:

    --soft Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

    0 讨论(0)
提交回复
热议问题