How to create a git branch so that the files that I add to it are not added to master?

I want to create a git branch that will work regardless of the leading branch. I want all the code present in the main still, but any further changes in the master should not be reflected in the branch, and any changes in the branch should not be reflected in the main.

I used this command to create a branch:

git branch test 

But any file that I add to master I see in test . And any new file added to test I see in master . How to avoid this? I did not use any --track option when creating the branch.

+6
source share
3 answers

What you requested is exactly what git branch test does (the point from which the two stories are no longer shared). There are two levels of confusion:

  • When you git add newfile , you add it to the index, but do not commit it or update any branch with its contents. When you check out a new branch, then while there is no conflict, git will not reset those changes from the index. This is desirable behavior, for example, if you are working in a different branch and want to take only a subset of uncommitted changes in another branch. If you git commit state of the index in the source branch, the index will not save these changes when you return to the test branch.

  • When moving between trees that have added or deleted new files, git checkout will not necessarily remove them from the file system (if there is a reset operation). Then you can β€œsee” these files when switching branches, for example. in the output is ls or git status - but they are not β€œtracked” and git will not try to do anything with them ( git commit -a , for example, will not add this file to the index before writing a new tree). This is nothing to worry about, but if you OCD about it, you can remove them (or use git clean ).

+4
source

I think you would like:

 git checkout --orphan somebranch 

this will create somebranch without any history, you can add the files you want to this, and they can be 100% different from your previous branches.

+14
source
 git branch test git add . git checkout test git checkout master 

Until you commit, you will see your working tree and your index (which refers to what you added) in both the test and master branches.

 git checkout test git commit -m "files for test" git checkout master 

Then you will not see in the master files that you just committed to test .
In the new branch test , a new commit (with new content) will be written, independent of master .

+2
source

Source: https://habr.com/ru/post/907027/


All Articles