Home > Software engineering >  How can I push git to directory in repository directly?
How can I push git to directory in repository directly?

Time:01-16

I made directory called testDir in repository like the picture below and I also made a test project. I want to push test project to testDir directly. For this, I typed command below.

enter image description here

git init

git add README.md

git commit -m "first commit"

git branch -M main

git remote add origin https://github.com/MontaKr/test/tree/main/testDir

but it says fatal: repository 'https://github.com/MontaKr/test/tree/main/testDir' not found

or sometimes it says error: failed to push some refs to ~~~

I don't know what is wrong and how can I push project to directory?

CodePudding user response:

You should:

  • clone your GitHub repository locally, using the repository URL https://github.com/MontaKr/test (and not a subtree)
  • copy your local testDir folder in it
  • add, commit and push

CodePudding user response:

I’m currently reading Pro Git, so while I’m not completely knowledgeable, I’ll take a swing with what I know

@VonC ‘s answer is the way to do it, but why?

Git uses the .git directory to build the repo. You made a new repo (using ‘git init’), which by consequence has a different .git directory.

So although the link was off, as VonC pointed out, the original way still wouldn’t have been legal - I think.

Usual flow would be:

  1. Clone your repo. Use the link GitHub gives you - find it by clicking on the green code button
  2. Make changes via commits
  3. Push it back to the GitHub repo

This way the original .git directory is still intact, and no foul no harm.

  •  Tags:  
  • git
  • Related