Search code examples
javagitjgit

Is it possible with Git to commit a file in a specific folder in the repository?


I'm new with git and I'm trying to use JGit in a java application. I would like to know if Git has a 'commmit of files into a specific directory' or is Git only based on the working tree where files are stored?

For example, let's say I have a file saved in C:/tmp/myfile.docx. Is it possible to tell Git to store it in the repository as /myProject/myfile.docx ?

Thank you :)


Solution

  • That is not possible. Think of your Git repository as a time machine for a tree of directories. You can only add files to git that are inside that directory tree. And don't try to make C:\ a Git repository... :)

    So, let's say your Git repository resides at C:\tmp\myRepo. To add your file to the repository you'd do this:

    1. move myfile.docx to C:\tmp\myRepo
    2. add the file to Git: (using Unix style command line) cd C:/tmp/myRepo && git add -A
    3. create a commit: git commit -am "added myfile.docx"