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

  • Importing with EGit clones repository but doesn't let me import project
  • Changing the git branch on eclipse
  • Private Key authentication failed error in Eclipse for openshift
  • How can I delete a remote tag in eGit?
  • SVN blame in Eclipse
  • Package contains object and package with same name
  • Thanks

  • Issues with Git - rebase / squash
  • git svn and working with private branches?
  • Will I ever run into problems if I don't clean before I build?
  • Git Commit during Git Rebase - what really happens?
  • Git contains between arbitrary commits
  • Showing which files have changed between two revisions
  • 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.