230

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

3 Answers 3

394
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.

5
  • 2
    since I got the new-branch from git checkout v3.0(the branch before I made changes) new-branch, Do I still require to use "git add <files>"? Feb 1, 2013 at 22:18
  • You can do a git status to see what is currently staged and what is not. Feb 1, 2013 at 22:20
  • 4
    how about if I already tracked a files and I want to switch to a new branch add those files to the newly created branch? Jun 19, 2020 at 2:47
  • 2
    New branch does not have a upstram so you need to set the upstram while pushing the changes as shown: $ git push --set-upstream origin your_branch Jan 9, 2021 at 10:10
  • if branch already exists on remote, you need git fetch before all these.
    – Sahib Yar
    Mar 25, 2022 at 20:52
116

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"

If you have committed changes

If you want to keep the commits in the original branch

See the answer by Carl Norum with cherry-picking, which is the right tool in this case:

git checkout <target name>
git cherry-pick <original branch>

If you don't want to keep the commits in the original branch

Create a new branch from your last commit (which will keep your work), then reset the original branch:

git checkout -b <new-branch-name>
git checkout <original-branch-name>
git reset <commit-id> --soft

If you have pushed your changes to a shared remote like GitHub, you should not attempt this roll-back unless you know what you are doing.

See also the answer by joeytwiddle on this potential duplicate.

6
  • 4
    Every one shoud try this code. Because most of the people who seeks for the answer, they have already have some changes in your existing code.
    – Sushin Pv
    Apr 9, 2019 at 6:03
  • 2
    Why did you add the stash and stash pop ? When you checkout a new branch, your changes will be avaialable to add and commit, no need to stash.
    – Tristan
    Sep 3, 2019 at 11:50
  • @Tristan You're right in the case where the changes are compatible and the files do not overwrite. Otherwise, I get error: Your local changes to the following files would be overwritten by checkout: ... Please commit your changes or stash them before you switch branches. Do you want to write a new answer or edit my answer? If not, I can edit it myself. Sep 13, 2019 at 16:50
  • Ok, you are totally right. I think you should complete your answer though, to show what it brings in addition to the accepted answer.
    – Tristan
    Sep 13, 2019 at 17:41
  • @Tristan I did that. The OP was asking about a new branch, in which case the accepted answer is the right one. I keep mine as other people found my answer useful. It's become a long answer and I'm happy to edit with any feedback. Sep 14, 2019 at 8:22
32

If I understand right, you've made a commit to changed_branch and you want to copy that commit to other_branch? Easy:

git checkout other_branch
git cherry-pick changed_branch
1
  • 6
    This is the right answer for what he is trying to accomplish.
    – Sean
    Apr 8, 2019 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.