przemo_li
przemo_li

Reputation: 4053

Move branch commits to current one as uncommitted changes to working directory?

Lets say that I work on branchA, and need to temporary commit all the uncommitted changes. So I create branchB, and commit everything.

After some time, I need to move all those changes back to branchA as uncommitted changes.

(Some changes where to files that where not previously tracked in repo)

How can I do it? Merge + reset? Is there simple command for it?

I could also rephrase this question as:

How to undo git branch -b branchB; git add .; git commit; ?

Upvotes: 1

Views: 125

Answers (3)

David Deutsch
David Deutsch

Reputation: 19015

Assuming you are going to throw branchB away, I would suggest doing git merge branchB --squash --no-commit. The --no-commit option makes sure the changes are just applied to your working directory, as opposed to committed. The --squash option prevents the commit that you eventually do from being a merge commit. The reason I suggest using that option is that it is not a good idea to do anything besides the merge itself in a merge commit, and it seems like you want to do some more work locally before doing a "real" commit. Plus, this prevents your temp commit on branchB from showing up in your history.

Upvotes: 2

Sébastien Dawans
Sébastien Dawans

Reputation: 4616

You could go for cherry-pick --no-commit:

git checkout branchA
git cherry-pick branchB --no-commit

In the above I assume all is in a single commit, but you can extend that to a sequence of cherry-picks.

See the cherry-pick reference for more info

Upvotes: 1

isherwood
isherwood

Reputation: 61036

If you aren't dealing with a large number of files, just check out the individual files from the other branch:

git checkout branchB <filename1>
git checkout branchB <filename2>

This brings in the files as they are at the head of the other branch.

Upvotes: 1

Related Questions