Create Git branch with current changes

I started working on my master branch thinking that my task would be easy. After a while I realised it would take more work and I want to do all this work in a new branch. How can I create a new branch and take all these changes with me without dirtying master?

  • Find Git commits that contain multiple specific commits
  • Can't push changes to git
  • opening .git/config : Permission denied
  • How can always ignore a file while merge
  • GIT - How to get all newly created branches
  • Do we really need to branch in Git?
  • Why does `git checkout <branch> <file>` stage the change?
  • Git - switching between branches in the middle of work
  • 5 Solutions collect form web for “Create Git branch with current changes”

    If you hadn’t made any commit yet, only (1: branch) and (3: checkout) would be enough.
    Or, in one command: git checkout -b newBranch.

    As mentioned in the git reset man page:

    $ git branch topic/wip     # (1)
    $ git reset --hard HEAD~3  # (2)
    $ git checkout topic/wip   # (3)
    
    1. You have made some commits, but realize they were premature to be in the “master” branch. You want to continue polishing them in a topic branch, so create “topic/wip” branch off of the current HEAD.
    2. Rewind the master branch to get rid of those three commits.
    3. Switch to “topic/wip” branch and keep working.

    Note: due to the “destructive” effect of a git reset --hard command (it does resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded), I would rather go with a:

    $ git reset --soft HEAD~3  # (2)
    

    , to make sure I’m not losing any private file (not added to the index).
    The --soft option won’t touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do).

    Like stated in this question: Git: Create a branch from unstagged/uncommited changes on master: stash is not necessary.

    Just use:

    git checkout -b topic/newbranch

    Any uncommitted work will be taken along to the new branch.

    When committing you will get the following message

    fatal: The current branch feature/NEWBRANCH has no upstream branch. To
    push the current branch and set the remote as upstream, use

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

    Just do as suggested to create the branch remotely:

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

    Follow these steps:

    1. Create a new branch:

      git branch newfeature
      
    2. Checkout new branch: (this will not reset your work.)

      git checkout newfeature
      
    3. Now commit your work on this new branch:

      git commit -s
      

    Using above steps will keep your original branch clean and you dont have to do any ‘git reset –hard’.

    Since you haven’t made any commits yet, you can save all your changes to the stash, create and switch to a new branch, then pop those changes back into your working tree:

    git stash  # save local modifications to new stash
    git checkout -b topic/newbranch
    git stash pop  # apply stash and remove it from the stash list
    

    To add new changes to a new branch and push to remote:

    git branch branch/name
    git checkout branch/name
    git push origin branch/name
    

    Often times I forget to add the origin part to push and get confused why I don’t see the new branch/commit in bitbucket

    Git Baby is a git and github fan, let's start git clone.