Home > Net >  Git: working with 2 branches on the same project
Git: working with 2 branches on the same project

Time:11-26

I'm working with Git for the first time. I have a project cloned and I am working on a branch with some changes... However, these changes had to be paused. So, while this is paused, I want to work on another feature for the same project. The changes for this new feature wouldn't affect the files modified/created in the first branch, they are in another directory in the same project.

Currently, if I create a new branch, I see the that the changes for the first branch appear also on the new branch...

What would be the correct way of doing this with Git (and the correct commands), without causing conflicts and have only each feature's changes in its respective branch?

Edit 1:

Sorry if this sounds like a dumb question, but I don't want to mess up the project repository... Would this work for what I'm trying to do?:

  1. Create a 2nd branch from my master.
  2. With my 2nd branch checked out, I make the changes for my second feature, and ONLY stage and commit these new changes to the 2nd branch (with git add <folder containing only 2nd feature changes>).
  3. When I'll be able to resume my 1st feature, I should check out my 1st branch, finish my changes, and only stage and commit these changes (with git add <folder containing only 1st feature changes>).

Does this make sense?

CodePudding user response:

In my opinion, this can be tried.

Lets assume you are now working on feature_1 branch. Once you make changes to the code, either commit or stash it.

    git fetch --all
        git checkout -b feature_1 origin/<remote_branch_you_want_to_track>
        <<do your code changes>>
        git status
        git add <files>
        git commit -m "commit_msg"

Now you can pause it and start work on another feature, say feature_2. The debatable question here is what your base for feature_2 should be. If its the same remote branch, from which you created feature_1. If so, then

git checkout -b feature_2 origin/<remote_branch_you_want_to_track>

Do your work for feature_2. Add and commit as mentioned earlier.

This would ensure that both feature_1 and feature_2 are created from same base for the remote tracking branch and you can switch between branches

Always commit your code. You can always add files to the existing commits or create a new commit or squash multiple commits together.

Add files to existing commit

    git add <left_out_file>
    git commit --amend --no-edit (provided you don't want to edit the commit message)

If you want to stash, always add a message to the stash, so you won't get lost in the bundle of stashes

     git stash save "a meaning message for what you are stashing"

If you want to check if your feature_1 and feature_2 are tracking which branch, run

      git branch -vv

CodePudding user response:

without causing conflicts

First you need to stop worrying about "conflicts". The way merge works is the way it works. Don't try to second guess yourself (or Git).

So the answer to your question is simply:

[working on branch1]
git add .
git commit -mwip 
git switch master
git pull
git switch -c branch2
[work on branch2]
  •  Tags:  
  • git
  • Related