Add Existing Project to Already created Remote Git Repository

Sample .gitignore command

Some times you may want to create a .gitignore before to create git

echo projectBase > .gitignore

if there are multiple files/folders

echo projectBase > .gitignore & echo otherFolder >> .gitignore

Add Existing Project to Already created Remote Git Repository

Prerequisites

  1. Install Git https://git-scm.com/downloads
  2. Install TortoiseGit(Optional) https://tortoisegit.org/download/
  3. Create an account in Remote Git Repository . I suggest https://bitbucket.org/ OR https://github.com/

Steps

  1. Create a Remote Git Repository . Set Default branch name as 'master'
  2. Create 'addExistingPjt2Git.bat' in the folder where you want to make the git and copy the following code into it.
    @echo off
    setlocal
    :PROMPT
    rem Git URL eg:  https://userid@bitbucket.org/userid/repository-name.git
    set /p remoteRepositoryURL="Enter remote repository URL: or 'q' to quit "
    echo "Entered remote repository URL was " %remoteRepositoryURL%
    set quitbat=true
    IF not "%remoteRepositoryURL%" == "q" IF not "%remoteRepositoryURL%" ==  "Q" set quitbat=false
    if "%quitbat%" == "true" goto END
    :NOTEND
    rem echo "Inside :NOTEND Entered Value was " %ANYKEY%
    git init
    git add .
    git commit -m "First commit"
    git remote add origin %remoteRepositoryURL%
    echo # list of files and folders to ignore in git  >> .gitignore
    for %%f IN (.gitignore,addExistingPjt2Git.bat)do echo %%f >> .gitignore
    git remote -v
    git pull --progress -v --no-rebase --allow-unrelated-histories "origin" master
    git push origin master
    set /p ANYKEY="Successfully added project to GIT, Press any key to continue: "
    :END
    endlocal

The above .bat worked for bitbucket

But for github a slightly different version was required
The main reason was

  1. Default branch of github is main and not master as in bitbucket
  2. Default brach created locally with git init is master
  3. so a rename of branch is needed for properly pushing it to remote
    @echo off
    setlocal
    :PROMPT
    rem Git URL eg:  https://userid@bitbucket.org/userid/repository-name.git
    rem Git URL eg:  https://github.com/osolsolutions/simple-contact-us.git
    rem Git URL eg:  ssh://git@github.com/osolsolutions/simple-contact-us.git
    set GIT_REMOTE_BRANCH_NAME=main
    set /p remoteRepositoryURL="Enter remote repository URL: or 'q' to quit "
    echo "Entered remote repository URL was " %remoteRepositoryURL%
    set quitbat=true
    IF not "%remoteRepositoryURL%" == "q" IF not "%remoteRepositoryURL%" ==  "Q" set quitbat=false
    if "%quitbat%" == "true" goto END
    :NOTEND
    rem echo "Inside :NOTEND Entered Value was " %ANYKEY%
    git init
    rem rename branch to match the rmote repository branch name
    git branch -m %GIT_REMOTE_BRANCH_NAME%
    git add .
    git remote add origin %remoteRepositoryURL%
    git commit -m "First commit"
    rem ------ the below commands may be uncommented if you are using a different github account than your gobally set account
    rem git config --local user.name <github username>
    rem git config --local user.email <github email>
    rem git config --local user.signingkey ""
    rem git config --local credential.helper wincred
    rem git config --local credential.username <github username>
    rem ------- loca user settings complete
    rem use below commented commands to check if the local setting worked
    rem git config user.name
    rem git config user.email
    git remote -v
    git.exe pull --progress -v --no-rebase --allow-unrelated-histories "origin" %GIT_REMOTE_BRANCH_NAME%
    git push origin %GIT_REMOTE_BRANCH_NAME%
    set /p ANYKEY="Successfully added project to GIT, Press any key to continue: "
    :END
    endlocal

    Shortest form for github

    git init
    rem git init creates branch 'master', but for git hub default branch is 'main'
    rem so rename branch to match the remote repository branch name.
    git checkout -b main
    git remote add origin https://github.com/osolgithub/phpdoxygen.git
    git pull origin main
    git add . && git commit -m "First commit to upload"
    git push origin main

PS:if branch main is not created, you may upload to main branch from default master branch with

rem git push origin HEAD:main
  1. PS: Keep only required files in the folder where you want to create local repository(.gitignore is not incorporated in the .bat file)
  2. Double Click the .bat file. It will prompt for the remote repository URL
  3. Copy the URL of the repository you made in step 1 and paste in command prompt.
  4. Press Return
  5. The Command Prompt will display a series of messages and finally come to halt with the message Successfully added project to GIT, Press any key to continue:
  6. That is it. Refresh the remote repository in browser and you see the local files is merged

Suggested Steps after merging repository

  1. Copy files and folder to ignore to the repository folder
  2. Add gitignore on those files