Git: move changes off of master branch

后端 未结 6 457
南旧
南旧 2021-02-03 20:53

Basic question but this happens to me all the time:

  • Make changes in a working-branch
  • Switch to master
  • git merg
6条回答
  •  后悔当初
    2021-02-03 21:26

    From your description, I assume that you did not commit any changes yet – is that correct?

    If yes, here’s your answers:

    How to prevent direct edits to master

    You would need to set that in your editor, but that will probably be difficult. Displaying your current branch in your prompt and your editor helps a lot.

    How to move the changes into a new branch new-working-branch and then discard working-branch

    git checkout -b new-working-branch
    git add …
    git commit -m "mycommit" 
    

    As you didn’t commit anything to master yet, you don’t need to change anything on master. You can now discard your working-branch if you feel like it.

    How to move the changes over to working-branch

    git checkout -b temp-branch
    git add …
    git commit -m "mycommit" 
    git rebase --onto working-branch master
    git checkout working-branch
    git reset --hard temp-branch
    git branch -d temp-branch
    

    If your changes don’t conflict with any changes that are on master, but not in working-branch, this can be done a lot simpler:

    git stash
    git checkout working-branch
    git stash pop
    

提交回复
热议问题