-1

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?

Alain
  • 339
  • 3
  • 19
  • 2
    You were probably working on the first branch and created the second one. You have to checkout the master branch and *then* checkout the new branch. This will create the branch from the code without your changes. – Peter Krebs Nov 25 '21 at 14:33
  • Presumably it's too late for what would have been the correct commands and you're also looking for the commands to do some damage control and get into your desired state from your current state? – Wyck Nov 25 '21 at 14:36
  • 1
    I think you can find a good explanation in [this answer](https://stackoverflow.com/a/67823541/11261546) – Ivan Nov 25 '21 at 14:38
  • @Wyck Not sure I understand... I haven't started working on the changes for the 2nd branch, since I haven't been able to create a 2nd branch without the changes of the 1st one... So I'm not really trying to do damage control. Or you mean I should have created my 2 branches right at the start, before working on my 1st changes? Sorry, as I said this is my first time working with Git, and I'm not sure about what you asked... – Alain Nov 25 '21 at 15:44
  • @PeterKrebs Ok, got it... can you check my "Edit 1" on the original question? Does it make sense? – Alain Nov 25 '21 at 16:47
  • 1
    @Alain Yes that is reasonable. I would recommend the [Git Tutorials by Atlassian](https://www.atlassian.com/git/tutorials/). They are written well and give a good idea for commonly workflows and techniques. – Peter Krebs Nov 26 '21 at 10:42

2 Answers2

1

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
Ron Thomas
  • 737
  • 1
  • 8
  • 20
1

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]
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    You need a mental picture of what Git is. It might help you to pause and read my https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/ – matt Nov 25 '21 at 18:46