Reputation: 2302
I have three branches Master, Branch1 and Branch2, I am currently working on Branch2. I have done some local changes but I want to commit these changes to a new branch (Branch3).
After doing some research I see that I should create a new command using "git checkout -b [name_of_your_new_branch]"
I am scared that if I do a checkout new branch all my local changes will be gone.
Can someone help confirm what is the safest way to commit and push to a new branch when working on branch2.
Upvotes: 0
Views: 790
Reputation: 4653
Git will not overwrite or destroy your changes unless you ask it to. Be careful with commands that have the flag --force
or -f
. If you check out another branch while having uncommitted changes, Git will "float" these changes over. If it is not possible to apply the changes cleanly, Git will refuse to check out the other branch.
Simply create and check out the new branch just as you mention and commit the changes as usual.
git checkout -b Branch3
git add <path>
git commit
As a general advice, try to keep things that you care about committed. Recovering a change that has been part of a commit is fairly trivial, but it can be hard to recover files that have never been part of a commit or added to the index.
Upvotes: 1
Reputation: 118031
You will not loose your changes if you checkout a new branch. You should:
create a new branch
git checkout -b Branch3
stage your changes
git add .
commit your changes to that branch
git commit -m "Adding current changes to my newly created branch"
push your branch up
git push --set-upstream origin Branch3
Upvotes: 1