How to move unstaged changes from branch A to branch B in Git?


Hi,

I’m working on a personal project and I find myself often get “distracted” when working on a feature branch.

I have a feature branch called contact-styling, and when I was working on the CSS of contact section, I wanted to fix something in the landing page because I figured it would look nice with the style I just applied in contact section. But the changes I made in the landing page shouldn’t be in contact-styling branch, right?

So, what’s the best solution in this situation? I tried git stash before but it kind of messed up my code, probably because I didn’t know what I was doing.

Any help would be appreciated.

If the changes haven’t been committed, you can stash them, create a new branch, and then destash them. If you’ve already committed the changes, then you create a new branch from your current branch and then rebase both branches to remove the commits that shouln’t be there.

I guess you might also be able to cherry-pick the commit and then revert the changes that don’t belong to the feature branch.

It would be best to only make changes in feature branches that belong to that branch. Commit the current changes, switch branches, make the “other” changes then switch back to the feature branch. Obviously, if they are both feature branches you need to merge them back into main without conflicts.



Source link

Leave a Comment