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

前端 未结 6 2097
醉酒成梦
醉酒成梦 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:24

    If you are using the GitHub Windows client (as I am) and you are in the situation of having made uncommitted changes that you wish to move to a new branch, you can simply "Crate a new branch" via the GitHub client. It will switch to the newly created branch and preserve your changes.

    0 讨论(0)
  • 2020-11-28 17:29

    Try:

    git stash
    git checkout -b new-branch
    git stash apply
    
    0 讨论(0)
  • 2020-11-28 17:30

    Two things you can do:

    git checkout -b sillyname
    git commit -am "silly message"
    git checkout - 
    

    or

    git stash -u
    git branch sillyname stash@{0}
    

    (git checkout - <-- the dash is a shortcut for the previous branch you were on )

    (git stash -u <-- the -u means that it also takes unstaged changes )

    0 讨论(0)
  • 2020-11-28 17:39

    In the latest GitHub client for Windows, if you have uncommitted changes, and choose to create a new branch.
    It prompts you how to handle this exact scenario:

    The same applies if you simply switch the branch too.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 17:49

    No need to stash.

    git checkout -b new_branch_name
    

    does not touch your local changes. It just creates the branch from the current HEAD and sets the HEAD there. So I guess that's what you want.

    --- Edit to explain the result of checkout master ---

    Are you confused because checkout master does not discard your changes?

    Since the changes are only local, git does not want you to lose them too easily. Upon changing branch, git does not overwrite your local changes. The result of your checkout master is:

    M   testing
    

    , which means that your working files are not clean. git did change the HEAD, but did not overwrite your local files. That is why your last status still show your local changes, although you are on master.

    If you really want to discard the local changes, you have to force the checkout with -f.

    git checkout master -f
    

    Since your changes were never committed, you'd lose them.

    Try to get back to your branch, commit your changes, then checkout the master again.

    git checkout new_branch
    git commit -a -m"edited"
    git checkout master
    git status
    

    You should get a M message after the first checkout, but then not anymore after the checkout master, and git status should show no modified files.

    --- Edit to clear up confusion about working directory (local files)---

    In answer to your first comment, local changes are just... well, local. Git does not save them automatically, you must tell it to save them for later. If you make changes and do not explicitly commit or stash them, git will not version them. If you change HEAD (checkout master), the local changes are not overwritten since unsaved.

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