4

I created a repo directly on GitHub using by browser, than using git bash cloned the repo to my machine, I was able to do a test commit with bash using the readme file.

I keep getting fatal: This operation must be run in a work tree when running git --work-tree=yourSrcFolder add .

Was looking here and here for some clue but after 4 hours ready to give up on ever using Git.

E_net4
  • 27,810
  • 13
  • 101
  • 139
OutOFTouch
  • 1,017
  • 3
  • 13
  • 30
  • Why are you trying to use `--work-tree`? If you're not in the repo, you probably need to be adding `--git-dir` too... but this is an unusual way to use git. – John Szakmeister Jan 25 '13 at 22:58
  • where did you clone the repo to and where did you try to commit the file from. It needs to be from the same directory. i.e if you clone into git-repo, you need to go into git-repo first before trying to add/commit the file – drone.ah Jan 25 '13 at 22:59
  • Just copy the files into your cloned repo and use `git add` instead. `--work-tree` looks like a backwards way of doing things. – Martin Jan 25 '13 at 23:00
  • @ jszakmeister I am at the root I think(master) – OutOFTouch Jan 25 '13 at 23:07
  • @drone.ah cloned to the default location in windows Users\User – OutOFTouch Jan 25 '13 at 23:08

3 Answers3

24

My solution was to create a hard link to the file that is placed in another directory.

You can use ln command (mklink on Windows) to achieve this (assuming that you are currently in the repository directory):

# this will add a file with the same name to the current directory and the same inode
ln ../../filename . 

And then just add it to the git index:

git add filename

And commit it if you need:

git commit -m "add file from another directory"

See inode, ln.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
1

Simply move any file you wish to add into the repo directory, then add them to the git tracking.

Example: suppose you want to add a file named test.txt to your git repo.

  1. Move it into your repository directory
  2. run git add test.txt
  3. commit the file using git commit -m "Added test file to the repo"
  4. push your modification to the remote server git push
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
0

(Windows version) just add a mklink /j or /J for Directory Junction

at your git dir:

mklink /j DirName "C:\location\SourceDir\"
rudmac
  • 1