96

I'm very new to Git. I've been searching for an answer, but I couldn't find one.

In my computer I have a project folder like this:

project_a
--some_folder
--another_folder
--.git

And I have a repository on GitHub, let’s say https://github.com/company/our_repo.git. Under this repository I have some folders. So my goal is to put my project_a under trunk/bin. How do I achieve this? (Again, I'm very very very new.)

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Vor
  • 33,215
  • 43
  • 135
  • 193

11 Answers11

114

Open your Terminal, access to this folder and write:

git init
git add .
git commit -m "my commit"
git remote set-url origin git@github.com:username/repo.git
git push origin master
Ziyaddin Sadigov
  • 8,893
  • 13
  • 34
  • 41
  • 2
    But my goal is to put my `project_a` under existing `repo` in such a way that on github it would look like this `repo/trunk/bin/project_a` – Vor Jun 03 '13 at 13:29
  • 9
    then first clone existing repo to folder in your local machine. Then add project_a to trunk/bin folder. And then push to github. – Ziyaddin Sadigov Jun 03 '13 at 13:31
  • 31
    One note: If you're doing this for the first time for your folder then you should use `git remote add` instead of `git remote set-url` . – Purvik Rana Nov 06 '19 at 14:01
  • won't `git clone` blow out the code in my local machine? i have code that i want to add to an existing repo – b_dubb Jul 29 '21 at 17:04
  • 1
    @b_dubb let's say you have "workspace" folder and inside this folder you have "new" folder which contains the code you want to add to an existing repo. Now, you go inside "workspace" folder and git clone the existing repo there with the folder name "existing". Now, inside "workspace" folder you have two folders: "existing" which contains the code from the existing repo and "new" which contains the code you want to add to an existing repo. Now, copy the code from "new" folder to "existing" folder, go inside "existing" folder, and commit & push the changes to your remote server. – Ziyaddin Sadigov Jul 29 '21 at 17:26
  • 1
    @ZiyaddinSadigov thx for reply. I wound up doing something like this yesterday. But for anyone who is curious the short answer to my question is yes. If you `git clone` info a directory that has files in it you will lose that code. – b_dubb Jul 30 '21 at 14:41
  • git remote add origin git@github.com:repo/spring-mongodb.git – Kasthuri Shravankumar Nov 23 '21 at 10:28
  • Or you need to use git remote add origin to connect the local repo with the remote repo – Qays Dwekat Feb 06 '22 at 22:58
  • If you get *error: src refspec master does not match any*, you may be running on the _main_ branch – Oded Ben Dov Nov 24 '22 at 08:41
  • for me `git remote set-url origin https://github.com/username/repo.git` had worked – sidharth vijayakumar Jun 30 '23 at 10:32
77

I had more luck with navigating in my terminal to the directory I wanted to add to the repository, then (assuming you're working on a branch called master):

    git init
    git add .
    git commit -m "my commit"
    git remote add origin <remote repository URL>
    git push origin master

Here's a link to an article explaining how to do it in more detail: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

Note that you won't be able to run the "git add ." line if the directory in question is open.

Adam
  • 8,752
  • 12
  • 54
  • 96
31

All the answers above seems to guide about creating a new repository in git but the question is about adding a folder to existing repo. To do that following steps can be followed.

  • Clone your existing repo using following command: git clone https://github.com/company/our_repo.git
  • Manually take your project folder to the desired location i.e. trunk/bin
  • Now commit and then push in the repo using the commands: git commit -m "message" and git push origin master
Ajaya Mandal
  • 537
  • 4
  • 10
18
1. first create a git repostry.
2. second open git bash in  existing or uploading project.
3. perform git init 
4. git add .
5. git commit -m "print message"
6. git remote add github<repostry url>
7. git remote -v
8. git push github master

OR

git push origin master

if you get any error, you may use it

git push -f origin master
sop
  • 3,445
  • 8
  • 41
  • 84
Vikram Shekhawat
  • 627
  • 2
  • 8
  • 12
8

You have to use -f when you are going to push on already existing repo.

git init
git add *
git commit -m "Initial commit"
git branch -M main
git remote add origin <repo url>
git push -f origin main
ZygD
  • 22,092
  • 39
  • 79
  • 102
Akash Mane
  • 81
  • 1
  • 2
  • Warning: using `git push -f` is only safe if you don't want to keep what's already on that repo! Many answers here have assumed you don't, but the original question specified there were existing files there. In fact, I find it strange that https://stackoverflow.com/a/51018115/3216427 is not the accepted answer, since the accepted answer also assumes you don't have pre-existing commits on the remote. – joanis Oct 29 '21 at 12:59
3

I think it is very preferable if you first pull the existing Github repo on the local and then add the new files to the Github repo

This link will help: https://stackoverflow.com/a/61285076/5840973

Darshan Jain
  • 781
  • 9
  • 19
2

Go to the directory where you code is,

git init
git add .
git commit -m "Your message"

Now add your address go to your git hub copy the clone address,

git remote add origin <remote repository URL>

Now add push your code with,

git push -u -f origin master

And you are done.

2
  1. git init
  2. git add .
  3. git commit -m "initial commit"
  4. git remote add origin [URL]
  5. git push origin master

    or

  6. git push -f origin master

git push --force.

It does force the update


Suresh B
  • 379
  • 4
  • 3
2

Lets say you have remote repo with files and local repo with the same files. And you want to add Git to local files, and dont want to push. Then you can do such commands on local repo:

git init
git remote add origin <url>
git fetch --all
git reset --hard origin/master

After that your repository is synced with remote repo. You can change files, git add, push and so on.

Denis Borisov
  • 166
  • 1
  • 3
1

Assume that I would like to add FreeRTOS repository, which URL is https://github.com/FreeRTOS/FreeRTOS-Kernel.git, into my repository, example URL is https://github.com/username/example as a submodule

git submodule add https://github.com/FreeRTOS/FreeRTOS-Kernel.git
git add .
git commit -m 'add a submodule'
git push

To clone using HTTPS:

git clone https://github.com/username/example.git --recurse-submodules

Using SSH:

git clone git@github.com:username/example.git --recurse-submodules

If you have downloaded the repo without using the --recurse-submodules argument, you need to run:

git submodule update --init --recursive
0

So i had this project that wasnt under source control i made some changes to and wanted to keep stuff i changed.

git init
git remote add origin <url>
git fetch
git branch master origin/master
git restore --staged .
  • This doesn't answer the question, which is: "...my goal is to put my project_a under trunk/bin. How do I achieve this?" Also, your answer appears very similar to existing answers. I would recommend looking through all existing answers before proposing a new answer. You contributions are nonetheless appreicated! – tjheslin1 Mar 30 '22 at 16:32