188

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"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user1988385
  • 2,337
  • 3
  • 16
  • 16
  • Just one commit or several? – Carl Norum Feb 01 '13 at 22:21
  • 8
    Possible duplicate of [Move existing, uncommited work to a new branch in Git](http://stackoverflow.com/questions/1394797/move-existing-uncommited-work-to-a-new-branch-in-git) – Slava Babin Dec 17 '16 at 19:37
  • This question is unclear. Is your branch new, i.e. not pre-existent (as the title suggests) or not (as the question suggests)? – dremodaris Feb 09 '21 at 17:22

3 Answers3

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

double-beep
  • 3,889
  • 12
  • 24
  • 35
John Brodie
  • 5,209
  • 1
  • 16
  • 29
91

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

See the answer by joeytwiddle on this potential duplicate. Follow any of the above steps as appropriate, then roll back the original branch:

git branch -f <original branch> <earlier commit id>

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.

miguelmorin
  • 3,611
  • 2
  • 15
  • 41
  • 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 09 '19 at 06: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 03 '19 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. – miguelmorin Sep 13 '19 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 '19 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. – miguelmorin Sep 14 '19 at 08:22
  • The `git stash` is what I needed. I made changes in the wrong branch and wanted to move that to a different branch based on master, not the current branch. – Alexis Wilke Oct 20 '20 at 18:05
26

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
Carl Norum
  • 201,810
  • 27
  • 390
  • 454