How to convert existing non-empty directory into a Git working directory and push files to a remote repository

Git

Git Problem Overview


  1. I have a non-empty directory (eg /etc/something) with files that cannot be renamed, moved, or deleted.

  2. I want to check this directory into git in place.

  3. I want to be able to push the state of this repository to a remote repository (on another machine) using "git push" or something similar.

This is trivial using Subversion (currently we do it using Subversion) using:

svn mkdir <url> -m <msg>
cd <localdir>
svn co <url> .
svn add <files etc>
svn commit -m <msg>

What is the git equivalent?

Can I "git clone" into an empty directory and simply move the .git directory and have everything work?

Git Solutions


Solution 1 - Git

Given you've set up a git daemon on <url> and an empty repository:

cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master

Solution 2 - Git

This is how I do. I have added an explanation to understand what the heck is going on.

Initialize Local Repository

  • first, initialize Git with > git init

  • Add all Files for version control with

    > git add .

  • Create a commit with a message of your choice

> git commit -m 'AddingBaseCode'

Initialize Remote Repository

  • Create a project on GitHub and copy the URL of your project. as shown below:

enter image description here

Link Remote repo with Local repo

  • Now use copied URL to link your local repo with the remote GitHub repo. When you clone a repository with git clone, it automatically creates a remote connection called origin pointing back to the cloned repository. The command remote is used to manage a set of tracked repositories.

    > git remote add origin https://github.com/hiteshsahu/Hassium-Word.git

Synchronize

  • Now we need to merge local code with remote code. This step is critical otherwise we won't be able to push code on GitHub. You must call 'git pull' before pushing your code.

    > git pull origin master --allow-unrelated-histories

Commit your code

  • Finally, push all changes on GitHub

    >git push -u origin master

Note: Now Github uses "main" as the default branch. If your project use "main" instead of "master simply replace "master" with "main" from the above commands

Solution 3 - Git

Here's my solution:

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master

Solution 4 - Git

In case the remote repository is not empty (this is the case if you are using IBM DevOps on hub.jazz.net) then you need to use the following sequence:

cd <localDir>
git init
git add -A .
git pull <url> master
git commit -m "message"
git remote add origin <url>
git push

EDIT 30th Jan 17: Please see comments below, make sure you are on the correct repo!

Solution 5 - Git

When is a github repository not empty, like .gitignore and license

Use pull --allow-unrelated-histories and push --force-with-lease

Use commands

git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/...
git pull origin master --allow-unrelated-histories
git push --force-with-lease

Solution 6 - Git

The new official way to do this in 2021. Navigate to directory containing files. This assumes there are no files already in the repository.

git init
git add .
git commit -m "initial commit"   
git remote add origin https://<git-userName>@github.com/xyz.gi
git branch -M main    # New
git push -u origin main # New

Sometimes I have to set the upstream by using this command.

git branch --set-upstream-to=origin/main main

And then force the push with this command.

git push -u origin main --force

Solution 7 - Git

The simplest way of doing this which I find useful is the below.

This might not be the official way but it works well.

Suppose you have a project named "XYZ" on your PC. Now you want to make a git repo of this project on github and use its functionalities.

Step 1: go to "www.github.com"

Step 2: create a repository with a "README.md" file (name it as you like it)

Step 3: clone the repository to your PC.

Step 4: In the cloned folder you will get two things : ".git" folder and a "README.md" file. Copy these two in your project folder "XYZ".

Step 5: Delete the cloned repo.

Step 6: add, commit and push all the files and folders of your project.

Now, you can just use your project "XYZ" as a git repository.

Solution 8 - Git

I had a similar problem. I created a new repository, NOT IN THE DIRECTORY THAT I WANTED TO MAKE A REPOSITORY. I then copied the files created to the directory I wanted to make a repository. Then open an existing repository using the directory I just copied the files to.

NOTE: I did use github desktop to make and open exiting repository.

Solution 9 - Git

Here's my solution if you created the repository with some default readme file or license

git init
git add -A
git commit -m "initial commit"   
git remote add origin https://<git-userName>@github.com/xyz.git //Add your username so it will avoid asking username each time before you push your code
git fetch
git pull https://github.com/xyz.git <branch>
git push origin <branch> 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHMWView Question on Stackoverflow
Solution 1 - GitabyxView Answer on Stackoverflow
Solution 2 - GitHitesh SahuView Answer on Stackoverflow
Solution 3 - GitcsomakkView Answer on Stackoverflow
Solution 4 - GitRomeo KienzlerView Answer on Stackoverflow
Solution 5 - GitSebastian Oscar LopezView Answer on Stackoverflow
Solution 6 - GitpolkaView Answer on Stackoverflow
Solution 7 - GitDeepam GuptaView Answer on Stackoverflow
Solution 8 - GittpageView Answer on Stackoverflow
Solution 9 - GitHarish KarthickView Answer on Stackoverflow