[git] Push existing project into Github

I have a folder with my project sources. How I can push this project into Github's repository?

I tried using this steps:

  1. I created empty repository on GitHub.
  2. I run git-bash and typed git init, so inside project root appeared .git folder.
  3. I added some files to version control using git add sourcesFolderName
  4. I committed files added in previous step using git commit -m "initial commit"
  5. I specified remote repository using git remote add MyProject <url>
  6. Finally git push, but nothing is pushed to remote repo... (no authorization failure)

So how I can push existing sources into newly created github repo?

This question is related to git github

The answer is


First, make a new repository on Github with your project name.Then follow the below steps..

1)git init
2)git add *
3)git commit -m "first commit"
4)git remote add origin https://github.com/yuvraj777/GDriveDemo.git
5)git push -u origin master

Create a new repository

git clone <url>
cd "repositoryName"
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Existing folder

cd existing_folder
git init
git remote add origin <url>
git add .
git commit -m "Initial commit"
git push -u origin master

Existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin <url>
git push -u origin --all
git push -u origin --tags

I know, this is an old question but I'm trying to explain every step, so it may help others. This is how I add an existing source to git:

  1. Create the repo on the git, so you'll have the ssh || https where you're gonna remote add you source code.
  2. In your terminal go to the path of your project.
  3. Run git init (here you initiate the project as a git one).
  4. Run git add * (here you add all the files and folders from you project).
  5. Run git commit -m "Initial Commit." (here you commit your files and folders added in step #4; keep in mention that you can't push your changes without committing them).
  6. Run git remote add origin https://[email protected]/your_username/project-name.git (here you add a remote project where your source it's gonna be pushed; replace my link with your ssh || https from the step #1).
  7. Run git push -u origin master (here you push your source into the git repository).

Note: Those are simple steps for pushing your source into the master branch.


In less technical terms

My answer is not different but I am adding more information because those that are new could benefit from filling in the gaps in information.

After you create the repo on github they have instructions. You can follow those. But here are some additional tips because I know how frustrating it is to get started with git.

Let's say that you have already started your project locally. How much you have does not matter. But let's pretend that you have a php project. Let's say that you have the index.php, contact.php and an assets folder with images, css, and fonts. You can do it this way (easy), but there are many options:

Option 1

Login to your github account and create the repo.

enter image description here

In the following screen you can copy it down where you need it if you click the button (right side of screen) to "clone in desktop".

enter image description here

You can (or do it another way) then copy the contents from your existing project into your new repo. Using the github app, you can just commit from there using their GUI (that means that you just click the buttons in the application). Of course you enter your notes for the commit.

Option 2

  • Create your repo on github as mentioned above.
  • On your computer, go to your directory using the terminal. using the linux command line you would cd into the directory. From here you run the following commands to "connect" your existing project to your repo on github. (This is assuming that you created your repo on github and it is currently empty)

first do this to initialize git (version control).

git init

then do this to add all your files to be "monitored." If you have files that you want ignored, you need to add a .gitignore but for the sake of simplicity, just use this example to learn.

git add .

Then you commit and add a note in between the "" like "first commit" etc.

 git commit -m "Initial Commit"

Now, here is where you add your existing repo

git remote add github <project url>

But do not literally type <project url>, but your own project URL. How do you get that? Go to the link where your repo is on github, then copy the link. In my case, one of my repos is https://github.com/JGallardo/urbanhistorical so my resulting url for this command would just add .git after that. So here it would be

git remote add github https://github.com/JGallardo/urbanhistorical.git

Test to see that it worked by doing

git remote -v

You should see what your repo is linked to.

enter image description here

Then you can push your changes to github

git push github master

or

git push origin master

If you still get an error, you can force it with -f. But if you are working in a team environment, be careful not to force or you could create more problems.

git push -f origin master

This one worked for me (just keep it for reference when in need)

# Go into your existing directory and run below commands
cd docker-spring-boot
echo "# docker-spring-boot" >> README.md
git init
git add -A
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/devopsmaster/docker-spring-boot.git
git push -u origin master
                


Another option if you want to get away from the command line is to use SourceTree.

Here are some additional resources on how to get set up:


git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master

The -f option on git push forces the push. If you don't use it, you'll see an error like this:

To [email protected]:roseperrone/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:roseperrone/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I will follow the previous comment from Rose P. It took me a long time to find the solution so I'm reposting (hopefully in plain English) what worked for me...

step 1: Create your new repository on Github.com (skip if you already have one)

step 2: Close XCode...not needed

step 3: Open a new Terminal window (yes, you have to use terminal...I tried all other ways...nothing worked)

step 4: Using the command cd to find your folder location to your project (the project you want to add to your existing or new repository)

step 5: type git init you'll get something like this. Reinitialized existing Git repository in /{current directory}

step 6: type git add . nothing happens after this step, but type it and go on to the next step.

step 7: type git commit -m "Initial commit" you'll get the following: # On branch master nothing to commit, working directory clean

or

some explanation about configuration and then a list of files that have changed.

step 8: type git remote add origin {project url} The project url can be found in Github.com. Its the HTTPS clone URL...you should be able to just copy and paste to the terminal window. If the system tell you that origin already exists, create a different name or use your project name (something different)

step 9: go to your GitHub application on your mac and click the "Sync Branch" button (even if there are no pending changes). It takes awhile I think for it to actually commit, but if you go back to your local repository folder you should see your new project. I had to re-create the parent folder, but it's just a matter of moving your files around. Go to GitHub.com and refresh your browser and your new files should also be there.

I hope that helps.


In summary;

git init
git status
git add "*"
git commit -m "Comment you want"
git remote add origin  https://link
git push  -u origin master

I would like to share a source with you so that you learn about Git more easily.

https://try.github.io/levels/1/challenges/1


Git has been the version-control system of choice since its inception in 2005. About 87% of the developers use Git as their version-control system.

But if you have a project that is already existing and you want to push to Git in the remote server, follow along the below steps:

  1. Go to the terminal of your project directory

  2. You need to initialize your project git using git init

  3. Create a .gitignore file and it is actually a text file that tells Git which files or folders to ignore in a project.

  4. Stage your files using git add .

  5. Commit your changes to your local repository with an appropriate commit message: git commit -m "my first commit"

  6. In this step, you just need to create a repository in any one of the distributed version control systems like GitHub or Bitbucket

  7. Use this Git command to link your local repository with that of the remote: git remote add <your-remote-name> <your-remote-url>

So, if your GitHub repo-url is https://github.com/your-github-username/new-repository.git, then the Git command becomes:

git remote add origin https://github.com/<your-github-username>/new-repository.git
  1. Push your code to remote GitHub repository

    git push origin master

Note: The git push command requires two parameters: the name of the remote repository (origin) and the branch to push to (here master is the default branch for every repository).

Refer this blog for detailed information.


I found that stimulating an update in "natural" sequence was an easier route than force-pushing things.

Supposing that the repo is already created on github and you may have also put some stuff into the README.md .

  1. On your computer, open terminal and git clone [repo URL]

  2. You'll see a new folder will have been created bearing your repo's name. Feel free to rename it - doesn't matter.

  3. Move your code, files etc into this folder. Edit the README.md if you have to.

  4. Now open Terminal / command prompt, get inside that folder and do things as if you are making the next update to the repo:

git add .
git commit -m "v2"
git push origin master

Note: at the commit command git may reject, asking to configure the user email and password first. Follow the steps as given on screen, then run the commit command again.

  1. And those three commands is what you do now onwards for every time you want to push another update.

Hate to add yet another answer, but my particular scenario isn't quite covered here. I had a local repo with a history of changes I wanted to preserve, and a non-empty repo created for me on Github (that is, with the default README.md). Yes, you can always re-create the Github repo as an empty repo, but in my case someone else has the permissions to create this particular repo, and I didn't want to trouble him, if there was an easy workaround.

In this scenario, you will encounter this error when you attempt to git push after setting the remote origin:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:<my repo>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

As the error indicates, I needed to do a git pull after setting the remote origin, but I needed to specify the --allow-unrelated-histories option. Without this option, git pull complains warning: no common commits.

So here is the exact sequence of commands that worked for me:

git remote add origin <github repo url>
cp README.md README.md-save
git pull origin master --allow-unrelated-histories
mv README.md-save README.md
git commit -a
git push

If you're on a Mac (and this probably works the same on a PC), here's a very easy way to do this. Strangely enough I've looked high and low for this simple process and never found it.

  • Do not do anything on Github (other than having an account, and not having used up all your available repos).
  • Download GitHub for Mac and install. Go through the account setup, etc. Do NOT create any repositories for your existing project.
  • "Add New Local Repository" in repositories.
  • Select your existing folder. It'll ask if you want to do that, say yes.
  • Once done, you'll see a list of all your files, etc. Commit them.
  • Go to Repositories and Publish (this will create the new repo on GitHub for you, if you set up your account properly).
  • Go to Repositories and Push (you'll either see the "nothing to push" thing, or it'll push your files/changes to the newly-auto-made repo).
    • Wonder why you could not find this simple process anywhere else.

I know it is not recommended to use the project folder as the repo folder. I do it all the time, it always works, it makes it simple, and I never have any trouble with it.


As of 7/29/2019, Github presents users with the instructions for accomplishing this task when a repo is created, offering several options:

create a new repository on the command line

git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/user/repo.git
git push -u origin master

push an existing repository from the command line

git remote add origin https://github.com/user/repo.git
git push -u origin master

import code from another repository

press import button to initialize process.

For the visual learners out there:

enter image description here


you will need to specify which branch and which remote when pushing:

? git init ./
? git add Readme.md
? git commit -m "Initial Commit"
? git remote add github <project url>
? git push github master

Will work as expected.

You can set this up by default by doing:

? git branch -u github/master master

which will allow you to do a git push from master without specifying the remote or branch.


git init

Add the files in your new local repository. This stages them for the first commit.

git add .

Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.

Commit the files that you've staged in your local repository.

git commit -m "First commit"
# Commits the tracked changes and prepares them to be pushed to a remote

repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again. Copy remote repository URL fieldAt the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL.

In the Command prompt, add the URL for the remote repository where your local repository will be pushed.

git remote add origin remote repository URL
# Sets the new remote
git remote -v
# Verifies the new remote URL

Push the changes in your local repository to GitHub.

git push origin master
# Pushes the changes in your local repository up to the remote repository you 

specified as the origin


  1. From command line, navigate to your local repository directory.
  2. Create new repository in GitHub, it will provide you with a link ends with .git.
  3. in cmd run : git remote add origin [your_GitHub_Repository_link] (remember link should end with .git )
  4. then run : git push -u origin master

Hope this was useful.


Follow below gitbash commands to push the folder files on github repository :-
1.) $ git init
2.) $ git cd D:\FileFolderName
3.) $ git status
4.) If needed to switch the git branch, use this command : 
    $ git checkout -b DesiredBranch
5.) $ git add .
6.) $ git commit -m "added a new folder"
7.) $ git push -f https://github.com/username/MyTestApp.git TestBranch
    (i.e git push origin branch)

Just follow the steps in this URl: CLICK HERE