How to merge my local uncommitted changes into another Git branch?

How can I do this in git:

My current branch is branch1 and I have made some local changes. However I now realize that I actually meant to be applying these changes to branch2. Is there a way to apply/merge these changes so that they become local changes on branch2 without committing them on branch1?


Since your files are not yet committed in branch1 :

git stash
git checkout branch2
git stash pop

or

git stash
git checkout branch2
git stash list       # to check the various stash made in different branch
git stash apply x    # to select the right one

As commented by benjohn (see git stash man page):

To also stash currently untracked (newly added) files, add the argument -u , so:

git stash -u

Stashing, temporary commits and rebasing may all be overkill. If you haven't added the changed files to the index, yet, then you may be able to just checkout the other branch.

git checkout branch2

This will work so long as no files that you are editing are different between branch1 and branch2. It will leave you on branch2 with you working changes preserved. If they are different then you can specify that you want to merge your local changes with the changes introduced by switching branches with the -m option to checkout.

git checkout -m branch2

If you've added changes to the index then you'll want to undo these changes with a reset first. (This will preserve your working copy, it will just remove the staged changes.)

git reset

Here is a shorter alternative to the previosly mentioned stash approach:

Temporarily move the changes to a stash.

git stash

Create and switch to a new branch and then pop the stash to it in just one step.

git stash branch new_branch_name

Then add and commit the changes to this new branch.

链接地址: http://www.djcxy.com/p/3832.html

上一篇: 我如何查看GitHub拉取请求?

下一篇: 如何将我的本地未提交更改合并到另一个Git分支中?