How to add an existing git remote repo to a new existing local project

When starting a project I tend to init local git repository before I create one on GitHub. However when I’m ready to link the newly created remote repo to my local project using
git remote add {name} {git url} I tend to run in issues when I want to push my changes ( committed before I add the origin ) which I mainly commit based.

How can I add remote origin without running into these issues?

I think I misunderstood your question in the workshop and it’s a bit vague at the moment. Therefore, I’m going to present two common situations related to the question with their solutions:

Situation 1 - Create a local Git repo, then remote Git repo and followed by pushing to the remote:

On your computer, you have a local project that is not tracked by git. You want to start track it, continue to work on it locally and eventually push it to a remote repo.

Here are the steps to do so:

  1. Initialize the git report by running the following commands:

    git init
    # Add the files you've created and updated.
    git add .
    git commit -am "Your Commit Message"
    
  2. Continue to work on the files you wish to update or add and use the git add and git commit commands to track the files in your repo.

  3. Once you’re satisfied with the changes, you share your code. You will need to go to Github or Gitlab and create a repository for your code.

    Please note, you will need to add your SSH key to Github and Gitlab, if you want to use the Git URL (e.g. [email protected]:<gorup-username>/<repo>.git).

    Otherwise, you will have to use the HTTPs URL (e.g. https://github.com/<group-username>/<repo>.git).

  4. Once your SSH key is setup. You can execute the following commands:

     git remote add {name} {git url}
     git push -u origin master
    

You should see the changes in your remote repository

Situation 2 - You have a local project that is untracked, but there’s a remote repository of the project that is tracked by git and you want to add your changes:

Simple Solution, but not the best

Honestly, the simplest solution is to clone the repo to a different folder, copy the files to the cloned repo, commit and push.
Example:
Lets assume you have a project in your home folder called html-sam and you’re currently in your html-
sam folder. Run the following commands:

cd ..
ls .

You should see your html-sam folder. Now clone the repo using git clone .
Lets call the new folder rs-html-sam

git clone <url-for-html-sam> rs-html-sam
ls . 

You would see html-sam and rs-html-sam folders. Next is to copy over the files and start tracking the untracked files.

cd rs-html-sam
git checkout -b add-untracked-files
cp -rf ../html-sam/ rs-html-sam
git status
git add .
git commit -am "Added new files."

You can push to production.

git push origin add-untracked-files

Better Solution:

Add the remote repository link to the local untracked project, track the files in a different branch, merge the files with the main branch then compare the changes between the main branch and the new branch.

We wrote a small utility that does this. You can download it from @b4oshany Github Gist repo.

cd /path-to-your-project
wget https://gist.githubusercontent.com/b4oshany/fef164455800d3bbf1002cf4012858d1/raw/e5cc2186406cd064fd8f5635a293a2451825343a/git-reinit.sh
mv git-reinit.sh ..
bash ../git-reinit.sh

Please note, git-reinit.sh bash script requires the following arguments:

Lets use the Git training repository as an example.

  1. Clone the repository:
git clone https://github.com/JamaicanDevelopers/GitTraining.git
  1. Remove the Git from the repository
cd GitTraining
rm -rf .git/
  1. Create some dummy files and git the contributors.md file:
echo "This is a dummy file" >> dummyfile.md
echo "- Another Test" >> CONTRIBUTORS.md
  1. If you run git status, it will state that the project isn’t tracked by git. However, we need to add our local changes that no longer have git to track it. Run the following command:
bash ../reinit.sh [email protected]:JamaicanDevelopers/GitTraining.git master origin
git diff master

The output should look file this:

You can read the code follow the code snippet to gain an understanding of what it is doing.

Nevertheless, here’s a modified simply version of the code snippet that was tailored for the Git training repository:

targetBranch=master
# Remove the .git config folder.
rm -rf .git/

# Re-init the Git repo and add repository
git init
git remote add origin [email protected]:JamaicanDevelopers/GitTraining.git
git fetch origin 

# Create a new branch with untracked files and then add it.
git checkout -b untrackedfiles && git add .
echo "- Oshane Test" >> CONTRIBUTORS.md
git commit -am "Untracked"
git tag v.untracked untrackedfiles


# Switch to the target branch
git checkout $targetBranch
git checkout -b merged-untracked

# Add the untracked files to the target file
git checkout untrackedfiles .
git commit -am "Update source code with untracked files"

# Clean up
git branch -D untrackedfiles


# Afterwards, run the follonng commands:
echo "Run the following command:"
echo "> git diff $targetBranch"

Hi @colin_campbell, the video recording for the workshop is now available on YouTube: https://www.youtube.com/watch?v=5t01nIeB98g