Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Can I commit my working directory to a new branch without committing it to a current branch?

Tags:

git

branch

commit

I am working on a project, and had all of my tests passing on the master branch. I then made some changes, and when everything started failing, I realized that maybe I should have made those changes on a different branch. Is there a way I can commit the changes to a new branch without committing them to my master branch, so that the master still has my passing tests?

like image 373
noli Avatar asked Mar 22 '10 01:03

noli


People also ask

How do I switch to another branch without committing?

when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.

How we can git push the changes to other branch without checkout to that branch?

Sometimes, you cannot merge a branch B into branch A without checking out A first if it would result in a non-fast-forward merge. This is because a working copy is needed to resolve any potential conflicts. git fetch with a refspec. This will help you to push the changes without any checkouts.

How do I commit changes to a new branch?

First, checkout to your new branch. Then, add all the files you want to commit to staging. Lastly, commit all the files you just added. You might want to do a git push origin your-new-branch afterwards, so your changes show up on the remote.


2 Answers

Yes, just create the new branch and check it out:

$ git checkout -b new-branch

Then commit any changes you have. They'll be applied to the new, checked-out branch.

like image 192
mipadi Avatar answered Oct 12 '22 13:10

mipadi


You can stash your current changes (git stash), switch to the new branch and then apply the changes to the new branch using

git stash pop
like image 43
Dominik Avatar answered Oct 12 '22 15:10

Dominik