Question:
I added some files to my staging area. I want to temporarily save this information. How to?
Background:
Sometimes I perform some bigger refactorings. It only really makes sense to commit the complete result.
During this refactoring, I also make some unrelated changes which I would like to commit independently.
For example, the refactoring renamed a method. The unrelated change fixed a spelling error on one parameter of another method in the same class.
Now, assume I already added most of the files to the staging area when I realize that I forgot to commit one of the unrelated changes beforehand.
Adding the files to the staging area takes time, because I check every single file to make sure that I really only commit what I want to. So, simply removing them all from the staging area is not the solution.
What I would like to do instead:
Is this somehow possible?
An alternate solution would be the possibility of multiple staging areas, but I don't think that's possible.
The following chain of commands should do the trick
git commit -m "temporary" # save current stage are as an actual commit
git commit unrelated_files -m "The other feature"
git stash # hide the rest
git rebase -i HEAD~2 # change the order of two last commits
git reset HEAD^ # rollback the "temporary" commit
git add . # add everything from that commit back to staging
git stash pop # and return back all initially unstaged stuff
You definitely need git stash
This will save your current work, and leave your directory as if you did a reset to HEAD. You can now switch branches, do some work on another topic.
When you're ready to work on your feature again, re-apply your stashed changes with git stash apply
You can combine multiple stashes, git stash list
will list them, then you can select wich one to apply, such as git stash apply @{1}
Edit :
As @nevik-rehnel said in the comments, you can also use interactive adding.
Unstage everything, use git add -p
to stage the unrelated changes, commit, then restage everything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With