How to Commit Changes to Another Pre-Existent Branch
I Just Made Changes to a Branch. How Can I Commit the Changes to the Other Branch? I Am Trying to Use: Git Checkout "The Commmit to the Changed Branch" -B "The...
I just made changes to a branch. How can I commit the changes to the other branch?
I am trying to use:
git checkout "the commmit to the changed branch" -b "the other branch"
However, I don't think this is the right thing to do, because in this case I'm creating a new branch instead of committing the changes to "the other branch".
Should I use the following command instead?
git merge "the other branch"
3 Answers
git checkout -b your-new-branch
git add <files>
git commit -m <message>
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.
Must Read
If you haven't committed changes
If your changes are compatible with the other branch
This is the case from the question because the OP wants to commit to a new branch and also applies if your changes are compatible with the target branch without triggering an overwrite.
As in the accepted answer by John Brodie, you can simply checkout the new branch and commit the work:
git checkout -b branch_name
git add <files>
git commit -m "message"
If your changes are incompatible with the other branch
If you get the error:
error: Your local changes to the following files would be overwritten by checkout:
...
Please commit your changes or stash them before you switch branches
Then you can stash your work, create a new branch, then pop your stash changes, and resolve the conflicts:
git stash
git checkout -b branch_name
git stash pop
It will be as if you had made those changes after creating the new branch. Then you can commit as usual:
git add <files>
git commit -m "message"