1

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; ?

isherwood
  • 46,000
  • 15
  • 100
  • 132
przemo_li
  • 3,703
  • 3
  • 31
  • 52

3 Answers3

2

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.

David Deutsch
  • 13,210
  • 3
  • 41
  • 45
1

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.

isherwood
  • 46,000
  • 15
  • 100
  • 132
1

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

Sébastien Dawans
  • 3,968
  • 16
  • 26