12

I have a git repository on GitHub, with 3 different folders and only the master branch. How can i create a new branch on one single directory instead of all the three?

Blackecho
  • 1,226
  • 5
  • 18
  • 26

5 Answers5

14

You can't. Git operates on the entire repository, not on individual directories. You can:

  • Create a new branch, and on that branch only modify things in your target directory.
  • You could obviously follow william.eyidi's suggestion and delete the other directories, but this will make things difficult if you want to merge changes back into your master branch.
  • You could set up the specific directory as a git submodule with it's own history separate from the parent repository, but this is probably more complex than it's worth.
larsks
  • 277,717
  • 41
  • 399
  • 399
1

I guess you should delete that repository and do it again, but this time with the right branch.

You mentioned you have 3 folders, but you want to versionize just one folder. So go to your folder with git.

This is an example in a Git-Bash

cd /path/to/your/folder
git init
git add --all
git commit -m "My first commit."
git push origin master

Origin has to be the github-repository address.

Rubinum
  • 547
  • 3
  • 18
0
  1. git checkout -b
  2. delete localy the 2 others directories
  3. git push
william.eyidi
  • 2,315
  • 4
  • 27
  • 39
0

After creating a new branch, instead of checking out the branch for the entire repository, you can have add directory that tracks this branch while the other directories are tracking an other branch.

git worktree add masterBranchDirectory master

Now the directory masterBranchDirectory is a copy of the entire repository. After changing into this directory you are automatically on master branch.

Sadık
  • 4,249
  • 7
  • 53
  • 89
0

You can just create a new repository for the single folder within your surrounding project.

e.g. for a project named App:

lets say within your App project you want folder1 to be your new Repo.
You wold have to create a new Repo on your github. And then inside your local App open git bash:

# create a Git Repository:

$ cd /folder1
$ git init -b main
$ git add . && git commit -m "initial commit"  

# link local Repo with remote Repo:

$ git remote add origin  <REMOTE_URL TO NEWLY CREATED REPO> 
# Sets the new remote
$ git remote -v
# Verifies the new remote URL  

# push your new Repo to Github:

$ git push origin main

then
if your App project was your whole developement folder
and folder1 was your distribution folder, where the build files were in

you could always just clone folder1's Repo for presentation purposes

and the App Repo for developement purposes.

Also basically you have a seperate branch for your single folder just inside a different Repository. But since its within your App it makes no difference.

Option B:

Also there is the Option of git submodules as mentioned in previous posts.
Where you could have foler1 even separatley (but not necessarily seperate) from your main Repo and only "embed" it into your App.
https://git-scm.com/book/de/v2/Git-Tools-Submodule
How do I work with a git repository within another repository?

Jachub123
  • 1
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30938252) – Simas Joneliunas Feb 03 '22 at 04:45