How do I move some modifications from a feature branch into the main one?

33 views Asked by At

As title. I have created a branch to do specific tasks add-ui-components. Unfortunately, I also found some specific changes, let's call it upgrade-webpack-config, that I want to move out of the current branch. The latter one I just named is a fake branch, as I have neither created a branch for it nor committed those changes. Given that I'm at the branch add-ui-components Is it possible that I can move those unstaged file changes back to the main so that I can create another branch for it after I'm done with the current branch add-ui-components? Thanks

Let me use some graph for better understanding

Input

main: A-B-C
feat: A-B-C-D-E + (not related unstaged changes)

Expected Output:

main: A-B-C + (not related unstaged changes)
feat: A-B-C-D-E + (new related changes added here)
2

There are 2 answers

1
pwoolvett On BEST ANSWER
  • from the feat branch: stash your changes: git stash --include-untracked
  • go to main: git checkout main
  • apply the stash: git stash pop
  • commit git commit -m "not related unstaged changes"
  • go back to your branch: git checkout feat

as the final step, you'll have to merge the "recent changes" in main to feat - either with a merge or a rebase.

eg, for rebase: git rebase main -> this will kinda revert all your commits and apply them one by one as if you had branched feat after the "not related unstages changes" commit was applied to main

0
Vampire On

Just do git switch main, the unstaged changes will float over.

Unless of course the changed files are different between main/C and feat/E, but then git will tell you so.

In that case you would do a git stash, then git switch main and then git stash pop.

If the latter brings conflicts, you can resolve them and after you verified everything is proper can git stash drop the stash entry, because if there were conflicts, git stash pop behaves like git stash apply, leaving the changes on the stash in case you applied them to the wrong place or need to start over.