eabates
eabates

Reputation: 898

Existing files in new, presumably empty, Git branch?

I am learning Git. When I create a new (empty) branch, checkout to it and then list its contents, it shows all of the files from the master branch (which I thought I had left). Why is this?

Upvotes: 0

Views: 84

Answers (4)

Richard Topchii
Richard Topchii

Reputation: 8165

I guess what you've just mentioned is expected behavior. If you create new branch from non-empty branch it will include all the files of that branch + uncommited changes.
In this state your new branch is even with master (has all the commits that master has).
Since you commit changes to your new branch, it will become different with master.

I guess, you would like to proceed with one of the following options:

  1. Create new branch
  2. Commit changes
  3. Merge to master

Or if you want to do something different and not alter your current project, you should create a new repository.

Upvotes: 1

user743382
user743382

Reputation:

The command you've run -- whichever it is, there are multiple -- doesn't create a new empty branch. It creates a new branch based on the exact same history that your prior branch was on, allowing you to divert from that point on.

If you really want to create a completely new branch without any shared history with any existing branch, look into git checkout's --orphan option.

Upvotes: 4

MicroVirus
MicroVirus

Reputation: 5477

You don't (typically) check out a new empty branch. You create a new branch that starts of at the current commit. It also doesn't touch the index or working directory, so all modifications that are not yet committed show up in git status.

Upvotes: 1

Thijs Riezebeek
Thijs Riezebeek

Reputation: 1812

Whenever you checkout to a new branch for example like this: git checkout -b my-new-branch, all the contents of the branch you are currently in will also exist in your new branch.

If you are creating a new branch, with the intention of starting a new project, you should probable be creating a new project/repository. Branching is used when you want to work on a specific part of your project, but keep it isolated from the main part of the project untill it's finished and you can merge the branches back together.

Upvotes: 2

Related Questions