Git: Create a branch from unstaged/uncommitted changes on master

前端 未结 6 2122
醉酒成梦
醉酒成梦 2020-11-28 17:22

Context: I\'m working on master adding a simple feature. After a few minutes I realize it was not so simple and it should have been better to work into a new branch.

6条回答
  •  旧巷少年郎
    2020-11-28 17:47

    If you want your current uncommited changes on the current branch to move to a new branch, use the following command to create a new branch and copy the uncommitted changes automatically.

    git checkout -b branch_name
    

    This will create a new branch from your current branch (assuming it to be master), copy the uncommited changes and switch to the new branch.

    Commit your changes to the new branch.

    git commit -m "First commit"
    

    Since, a new branch is created, before pushing it to remote, you need to set the upstream. Use the below command to set the upstream and push it to remote.

    git push --set-upstream origin feature/feature/NEWBRANCH
    

    Once you hit this command, a new branch will be created at the remote and your new local branch will be pushed to remote.

    Now, if you want to throw away your uncommited changes from master branch, use:

    git checkout master -f
    

    This will throw away any uncommitted local changes on checkout.

提交回复
热议问题