Home > OS >  Git workflow to create a new branch with uncomitted changes
Git workflow to create a new branch with uncomitted changes

Time:01-04

I am wondering what my best options are in this situation I am often in. Basically my team is very slow to approve PRs and my next task is building upon the previous.

So I have Branch A which is feature complete but sitting in a PR for a long time. I'd like to build upon it with Branch B which completely relies on non-merged changes from Branch A. What is my best workflow here?

In my current situation I actually have done all my changes for Branch B but they are sitting as uncommitted files on Branch A. Do I just git stash and git checkout -b BranchB and git stash apply? Will this include all my changes from Branch A (which should mean I won't face conflicts?)

If you are wondering why I don't just try and find out, I am very scared of what might happen.

CodePudding user response:

If your repository and working folder is in this state:

  • You have Branch A checked out
  • You have made modifications to files in Branch A
  • You do not yet have Branch B

Then you can simply do this:

git checkout -b Branch-B

This will create and check out Branch B, "on top" of Branch A. Your uncommitted files are still present and still uncommitted, basically, your working folder is left unchanged. You can then start adding and committing those files to your hearts content.


Though you said in the comments that you understand what you're doing in regards to the following remarks, I'm just going to put a caveat here for future visitors as well.

If Branch A is not yet merged into your main branch, and you do this, create Branch B based on Branch A, if you then try to merge Branch B back into your main branch before Branch A is merged, you will drag along all changes in Branch A as well. Essentially you will merge Branch A's changes into the main branch, even though the branch itself is considered unmerged.

CodePudding user response:

Create yet another Branch C and work there as you'd like, merging in the upstream changes later

stashed work can still cause conflicts and they can be more annoying to work with and are easier to accidentally lose or lose track of

  •  Tags:  
  • Related