How do I create a git branch so that files I add to it are not added to master?
Asked Answered
C

3

11

I want to create a git branch that will work independent of the master branch. I want all the code that is present in the master till now, but any further changes in the master should not reflect in the branch and any changes in the branch should not reflect in the master.

I used this command to create branch:

git branch test

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

Communicative answered 27/1, 2012 at 14:14 Comment(9)
That's not supposed to happen at all. Could you detail a bit more how you're doing your commits and branch switching?Jaquith
Are you sure you are changing branch through the git checkout command?Defend
yes, I am changing the branch with git checkoutCommunicative
By default I am in the master branch. I created a new branch using the command: git branch test Then, switch to the new branch using: git checkout test Add a new file in test branch: git add newfile Switch to master: git checkout master then: git status and I find the newfile I added in test branch in the master Why do I see the newfile in test branch in master? I want it to be visible only in test branchCommunicative
Do you commit your changes before switching?Jaquith
No Mat, I am not doing commit before switching. I just do a git add and then switchCommunicative
Then the behavior is expected. Commit if you want to save. Use git stash if you don't want to commit yet.Jaquith
Changes aren't put into a branch until you commit them. You're only making changes in your "working copy" which is a separate entity from any branch that you're committing toSynchronic
I was able to commit only to the branch. Thanks Mat.Communicative
W
5

What you've asked for is exactly what git branch test does (a point from which the two histories are no longer shared). There is two levels of confusion here though:

  • When you git add newfile, you are adding it to the index, but not committing it or updating any branch with it's contents. When you check out a new branch then, as long as there is no conflict, git will not reset those changes from the index. This is a desirable behavior, for example if you are working on another branch and want to take only a subset of the uncommitted changes over to another branch. If you git commit the state of the index on your original branch, the index won't retain those changes when you go back to the test branch.

  • When you move between trees that have new files added or removed, a git checkout will not necessarily remove them from the filesystem (if there is a reset operation). Then you may "see" these files when you switch branches, e.g. in the output of ls or git status -- but they are "untracked" 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're OCD about it, you can delete them (or use git clean).

Whitleywhitlock answered 27/1, 2012 at 14:58 Comment(1)
More detailed than my answer. +1. Note, it is not OCD. It is CDO, ie like OCD but in alphabetical order. As it should be. (Sorry, it is Friday, couldn't resist)Gusset
S
29

I think what you wanted was:

git checkout --orphan somebranch

this will create a somebranch with no history whatsoever, you can add which files you want to that one, and they might differ 100% from your previous branches.

Skyway answered 28/4, 2014 at 12:23 Comment(1)
Not what the OP wanted, but what most people finding this question do.Frisbee
W
5

What you've asked for is exactly what git branch test does (a point from which the two histories are no longer shared). There is two levels of confusion here though:

  • When you git add newfile, you are adding it to the index, but not committing it or updating any branch with it's contents. When you check out a new branch then, as long as there is no conflict, git will not reset those changes from the index. This is a desirable behavior, for example if you are working on another branch and want to take only a subset of the uncommitted changes over to another branch. If you git commit the state of the index on your original branch, the index won't retain those changes when you go back to the test branch.

  • When you move between trees that have new files added or removed, a git checkout will not necessarily remove them from the filesystem (if there is a reset operation). Then you may "see" these files when you switch branches, e.g. in the output of ls or git status -- but they are "untracked" 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're OCD about it, you can delete them (or use git clean).

Whitleywhitlock answered 27/1, 2012 at 14:58 Comment(1)
More detailed than my answer. +1. Note, it is not OCD. It is CDO, ie like OCD but in alphabetical order. As it should be. (Sorry, it is Friday, couldn't resist)Gusset
G
3
git branch test
git add .
git checkout test
git checkout master

As long as you don't commit, you will see your working tree and your index (which references what you have added) in both branches test and master.

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

Then you won't see in master the files you have just committed in test.
You new branch test will have recording a new commit (with new content), independent from master.

Gusset answered 27/1, 2012 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.