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).

  • Maven: package does not exist
  • Eclipse Run Configuration - Working Directory for projects not in workspace
  • Checkout SVN svn+ssh repo with subversive in Eclipse Helios
  • How to use forked git project in Android Studio
  • Eclipse Egit will not import repo in workspace
  • Eclipse - Exclude root directory from git repository?
  • Thanks

  • Sharing rerere cache
  • Is it correct that subversion, but not git, allows empty folders?
  • git log -p vs. git show vs. git diff
  • Share only a subset of a git repository
  • Remove file in 'changes not staged for commit`
  • Using Git, if during a newfeature branch, when we need to do a hot fix, should we create a hotfix branch?
  • 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 WebContent/MyFolder

    1. Rename MyFolder to something like MyFolder.tmp
    2. Create a new repository in WebContent with name MyFolder
    3. Copy the content of MyFolder.tmp into MyFolder
    4. Make a first commit of MyFolder
    5. Delete MyFolder.tmp

    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.

    Git Baby is a git and github fan, let's start git clone.