Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3211890
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:46:52+00:00 2026-05-17T14:46:52+00:00

I started working on my master branch thinking that my task would be easy.

  • 0

I started working on my master branch thinking that my task would be easy. After a while I realized 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:46:53+00:00Added an answer on May 17, 2026 at 2:46 pm

    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

    With Git 2.23+ (Q3 2019), the new command git switch would create the branch in one line (with the same kind of reset --hard, so beware of its effect):

    # First, save your work in progress!
    git stash
    
    # Then, one command to create *and* switch to a new branch
    git switch -f -c topic/wip HEAD~3
    

    Or, as suggested in Alia‘s answer, use git switch -m, without git stash:

    git switch -c topic/wip -m
    

    --merge

    If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context.

    However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

    When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).


    As mentioned in the git reset man page:

    $ git stash                # (0) Save your work in progress
    $ git branch topic/wip     # (1)
    $ git reset --hard HEAD~3  # (2)  NOTE: use $git reset --soft HEAD~3 (explanation below)
    $ 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.

    Again: new way (since 2019 and Git2.23) to do all that in one command:

    git switch -f -c topic/wip HEAD~3
    

    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:

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

    This would 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).


    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started working on my master thesis in C that I haven't
Started working on a new application this week that is running the latest rails
Ive started working on a product that uses license files. These files need to
The company that I'm working is working with SVN but I would like to
I just started working on a project that uses git as a repository. I've
After converting an ASP.NET webform (v3.5) to use a master page, I started getting
I am working locally on a branch (master). I previously made a rebase to
I started out with a basic site.master. After I laid out several pages, I
I've started working on my master's project and decided to try something new and
I started working on a project which uses Net_URL_Mapper from PEAR (php) as the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.