In Git, I have an experimental branch (EXP-12). I have done some changes there, but I am not ready for commit yet. I need to make adjustments on master.
I go to master.
Two cases are possible:
- If I included my changes into index while on
EXP-12, they will appear in index on master branch as well. - If I didn’t include my changes into index while on
EXP-12, those files will appear as changes not staged for commit on master branch as well.
My problem is that I want everything on master and EXP-12 to be separate. I want to do some “experimental” changes on EXP-12 > go to master > do changes on master > stage everything for commit (git add -A) > commit > switch back to EXP-12 and continue experiments. Now if I do that my commit at the master branch will include all changes done on EXP-12.
What is the logic behind that? Or am I missing something?
A classic solution to this problem is to use
git stash– when you’re onEXP-12you can do:… then change branch to master as usual, do some work, change back, and apply that most recent stash:
However, I personally prefer to create a work-in-progress commit, and later either:
git commit --amendgit reset HEAD^git rebase -i <EARLIER-COMMIT>to squash several work-in-progress commits together.The advantage of creating an actual commit over using the stash is that you can apply a stash on any commit, not just the one you saved it at, whereas a normal commit moves forward that branch and is tied to the earlier commits.