Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I merge my local uncommitted changes into another Git branch?

Tags:

git

branch

How can I do the following 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?

like image 867
solsberg Avatar asked Feb 17 '09 14:02

solsberg


People also ask

How do you transfer uncommitted changes from one branch to another?

Create a new feature branch, say feature, and then switch to that branch. Implement the feature and commit it to our local repository. Push to the feature branch to the remote repository and create a pull request. After other teammate's review, the new change can be merged into the master or release branch.

What happens to uncommitted changes when you switch branches?

If the file is different on your other branch, Git will not let you switch branches, as this would destroy your uncomitted changes. It is safe to try switching branches, and if Git warns you that "uncommitted changes would be overwritten" and refuses to switch branches, you can stash your changes and try again.


2 Answers

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 

Above is the longer more explicit version of rbento's answer:

git stash git stash branch branch2 

It uses:

git stash branch <branchname> [<stash>]

  • Creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created,
  • applies the changes recorded in <stash> to the new working tree and index.

If that succeeds, and <stash> is a reference of the form stash@{<revision>}, it then drops the <stash>.

This is useful if the branch on which you ran git stash push has changed enough that git stash apply fails due to conflicts.
Since the stash entry is applied on top of the commit that was HEAD at the time git stash was run, it restores the originally stashed state with no conflicts.


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 
like image 170
VonC Avatar answered Oct 09 '22 03:10

VonC


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 
like image 23
CB Bailey Avatar answered Oct 09 '22 04:10

CB Bailey