1

I have 100s of changes that I don't want to push to master. Usually I start a new branch, and then merge later.

How can I create a new branch without losing my current changes?

https://i.stack.imgur.com/01UUu.jpg

Tom Crosman
  • 1,137
  • 1
  • 12
  • 37

4 Answers4

2

To Save all changes in new branch without losing any data First stash changes

git stash -m "write_your_own_message"

Make your new branch

git checkout -b "new_branch"

And get all changes in the current branch by this command

git stash apply

If you have more than one stash run this command to know stash number

git stash list

And have a stash list like this

stash@{0}: On master: all chages in the files
stash@{1}: WIP on master: 44381e5 add files project
stash@{2}: On master: my changes

Apply stash by index number like this

git stash apply 0

Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34
0

Using visual studio

  1. Commit your changes
  2. Create new branch (as shown in your image, but uncheck checkout branch)
  3. Right click the commit before -> reset -> hard reset -> ok (Now you are at the commit before, and your changes are in safe backup branch)
  4. Checkout your new backup branch -> push (this will create a new branch at origin side).
Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
0

Just do

git stash -m "my awesome changes"

After creating a new branch, just apply

git stash apply --index 0

If you have other stashes, then make sure you use the right index.

0

If you have been making local changes, and you want to commit them to a new branch, just create the new branch and add them to it. It's ok to create a branch when there are uncommited changes present.

e.g.

On branch main: Change 7 files. These changes are not committed.

git checkout -b my_new_branch

git add names-of-files-changed

OR

git add -u (means add any files which are in the git repo, but have changed. Does not add newly created files)

git commit -m"message describing changes"

main is where it was before your changes. my_new_branch has your changes.

Randy Leberknight
  • 1,363
  • 9
  • 17