0

I have some work in the index ready to be committed, and some changes in the working tree.

Now Ive been asked to commit now the changes in the working tree (on master) and later the work currently in the index (in a different branch). Is this possible?

user815129
  • 2,236
  • 2
  • 20
  • 40

1 Answers1

3

It should be possible:

git stash --keep-index
git commit -m "work in progress"
git branch wip
git reset --hard HEAD~
git stash pop
git add .
git commit -m "commit now the changes"

The work currently in the index is now in branch wip.
The changes (not added to the index) are stashed first (hence the --keep-index)

And the index of the branch you were in initially represents the changes you were working on and that you can commit on master.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks, but doesnt stash with no args store away both index & working tree (mixing them)? – user815129 Apr 29 '14 at 12:05
  • @user815129 yes, I forgot the `--keep-index` argument. I have edited my answer. – VonC Apr 29 '14 at 12:07
  • ok but even with the '--keep-index', the commit at line 2 will not have anything to commit after the stash.. or maybe Im missing something? – user815129 Apr 29 '14 at 12:09
  • @user815129 it will have to commit everything you already added to the index: that is the "some work in the index ready to be committed" you mention in your question: if it is ready to be committed, it means you have added it to the index already. If you didn't add that work to the index, you can start by doing so, before following the rest of the answer. – VonC Apr 29 '14 at 12:10
  • Aah.. I see. thanks.. wow Ive been reading the documentation for an hour and didnt get this point – user815129 Apr 29 '14 at 12:14
  • @user815129 yes, the index is very important for Git: see http://stackoverflow.com/a/3690796/6309. – VonC Apr 29 '14 at 12:34
  • is the 'git reset --hard HEAD~' line really necessary? – user815129 Apr 29 '14 at 12:34
  • @user815129 yes, you want to reset `master` to its state before wip (before the "work in progress" you just committed). You then re-apply your changes on top of that state. – VonC Apr 29 '14 at 12:35