Adding a folder from an Eclipse project to Git source control
I have an Eclipse Dynamic Web Project setup located in my local workspace. I want to place one of the folders of the project under Git source control. Is this possible?
I don’t want the entire project in the repository, just a specific folder within
WebContent. I have the EGit plugin and created a new Git repo from within Eclipse, but do not know how to add the folder to the repository (the repository and the folder are currently in different locations).
2 Solutions collect form web for “Adding a folder from an Eclipse project to Git source control”
Try to create a file within that folder, and add it.
For instance, an empty
.gitignore file, which:
- will be ignored by your web project
- won’t have any effect in the git repo
, but will allow for the Git tree to include its parent directory (i.e. the directory you want to add)
As Jefromi points out in the comment:
“A git repository in a given directory can only track content within that directory. There’s no way around this.
You need to either move the repository to contain your folder, or your folder to within the repository. “
Meaning if you already have a Git repo for a given working tree, and want to add the content of a directory located elsewhere, I would recommend:
- creating a new git repo within that new directory (the one within your Web project)
- referencing that new repo within your current git repo as a submodule, if you want to keep a tight link between the tow set of files.
Say you want to track
MyFolderto something like
- Create a new repository in
- Copy the content of
- Make a first commit of
However, I do not recommend putting just parts of an Eclipse project into source control. Also, the repository location is an absolute path in your workspace folder.