0

I have been working with git for some time now and I have come to this situation.

Say I am working on one feature adding_feature_1 on my feature1 branch and then suddenly I got asked to add feature 2.

The obvious would be to go to the root where feature1 branch started and checkout a new branch feature2. Probably when I am done merge it

However, if I do that, (meaning I checkout to the root branch) I won't be able to see the things I was working on feature1.

What if I want to see it?

Worse still what if I want to run things with the feature1 code, and since running it takes time I want to work on feature2. I suspect checking out to a different branch would mess things with the feature1 run

What I do now and I think it is very wrong and messy is to start a new branch2 from branch1 but it gets disorganized after a while because then if I merge it a branch has two features added and not one.

How do others work with this situation?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • After starting your new feature branch from master/main, I think what you want is to merge another branch into your current branch temporarily. Does this help you? https://stackoverflow.com/questions/29680646/git-temporarily-merge-others-branch-into-my-current-branch. Or, this post: https://stackoverflow.com/questions/67334184/temporarily-use-commits-from-another-feature-branch – Christian Nov 17 '22 at 00:34

1 Answers1

0

If you want to leave everything as is (feature1 in progress), create a new folder where you can use the same repository, on feature2.

Use git worktree for that: it avoids cloning the repository again.
And you will have a separate folder for feature2, in which you can code, while comparing your code with feature1.

cd /path/to/local/clone
git worktree add  --checkout -b feature2 ..\feature2 $(git merge-base feature1 main)
cd ../feature2
# start working on feature2
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250