720
votes
  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?

9
Maybe I just don't get it, but cannot you just run git init inside the local directory?Philipp
Do you mean that you have a repo somewhere else, and you want to add to that repo all the contents of this other directory which is not a repo? Or are you just trying to create a new repo in that directory?Cascabel
A number of the answers mention github, but the question itself is about git. github is not git, and it is not the center of the git universe.vfclists

9 Answers

1148
votes

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
110
votes

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

33
votes

Here's my solution:

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

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!

8
votes

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
4
votes

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.

2
votes

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
0
votes

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.

0
votes

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>