0

I am working on a project with some mates.

Yesterday I cloned the project with the intention to add a functionality.
I have 2 local branches that are develop (the main branch) and pageContent (my feature branch).

The problem I am currently encountering is when I edit something on my feature branch, it automatically edits it on my developp branch too (I did not commit anything).

I checked out on my developp branch to delete the edition and when I checked out on my feature branch, the edition was deleted too ...

The branches seem to be auto-synchronised.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Ch1ken
  • 81
  • 8

1 Answers1

1

I checked out on my develop branch to delete the edition and when I checked out on my feature branch, the edition was deleted too ...

This is how git works.


In the following diagram you can see the 3 states.
Git has three main states that your files can reside in.

enter image description here


They are all shared between your branches. but when you checkout branch you change the HEAD so you end up with the staging area && working directory shared between your repository even when you checkout branches.


Since you did not commit (i assume that what happened) when you switch branches you see the changes following you to your new branch.

If you don't want the changes to follow you you need to commit (or stash) your work before switching to the branch.

How to checkout different branch with clean working directory and stage area?

If you wish to checkout clean branch without any "leftovers" in your working directory and staging are you can create a new worktree which will result in shared view of your repository (all the content is shared) but with a different working directory and staging area.


From git v2.5

git worktree add <new_path>

Now do whatever you want in any of your branches. It will create 2 separate working folders separated from each other while pointing to the same repository.

Using wortree you don't have to do any clear or reset in order to remove all your staged and untracked content.

Here is demo of how to do it:

enter image description here


Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167