860
  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?

3
  • 9
    Maybe I just don't get it, but cannot you just run git init inside the local directory? Commented Jul 22, 2010 at 17:51
  • 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? Commented Jul 22, 2010 at 17:54
  • 8
    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. Commented Jun 21, 2019 at 16:58

11 Answers 11

1346

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
Sign up to request clarification or add additional context in comments.

18 Comments

abyx's instructions appears to work. Do I now run: git config branch.master.remote origin and git config branch.master.merge refs/heads/master and what I will end up with will be exactly the same as if I cloned the remote repository? ie git pull and git push will just work?
This worked for me also. I had to first create a project AppName in GitHub. It wasn't clear to me waht exactly the <url> means. So for those who the same question, we simple use GitHub.com, we're not running our repo, and then the <url> as used in the 5th line looked something like this: https://github.com/CompanyName/AppName
If you're setting up a repository that's not on GitHub, be sure to use 'git --bare init' to set up the empty remote repository, and not 'git init' (like I did) or the push will fail.
Just wanted to add, that I couldn't push until I did a: git pull --rebase
If git rejects because remote contains some minor changes (.README, .gitignore, etc.), try git pull origin master --allow-unrelated-histories to do a merge.
|
186

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

7 Comments

I have executed "git pull" like described in this post and obtained an error. I have continued with "git push" that has been accepted and when I go to Bonobo Git Server, I can now see the change. Thanks for this post with clear explanations on GIT command.
What does this mean: " and copy the URL of your project and copy URL of the project. " Is this a typing mistake, or are you trying to talk about two different URLs, or?
Thats a typography mistake, simply copy url of project as shown in screen shot
This is the correct answer and should be the accepted one. The accepted answer is missing your "Synchronize" step.
I ran into an error because github now initializes new repos with "main" as the default branch instead of "master."
|
44

Here's my solution:

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

3 Comments

Good solution when you setup remote repo with .gitignore & README.dm
what is the -t for?
-t / --track: When creating a new branch, set up "upstream" configuration. This just simplifies push later :)
17

The new official way to do this in 2021. Navigate to the 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.git
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

Comments

14

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!

1 Comment

The above commands wiped off all my existing builds :( Please exercise caution before doing the above steps
8

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

1 Comment

I needed to modify that last statement to be more like git push -u origin master --force-with-lease
4

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.

Comments

2

It took a time to go through all versions, and in the end this version did work

First of all: the original GitHub repository should NOT contain anything that your local repository already contains. Nothing. Period.

git init
git remote add origin URL
git branch -M main
git fetch
git pull origin main
git add .
git  commit -am "Initial commit"
git push -u origin main

And that's all folks.

Comments

1

Same issue, solution using GitHub Desktop. Functionality of git init, which is indeed what you want here, is not very self-explanatory in GitHub Desktop.

To summarise: you have a project in a single directory that's e.g. ready for deployment and you want to do that via GitHub. All the stuff inside the directory that you don't want in the repo (such as the virtual environment), is listed in a .gitignore.

  1. From the File menu, select New repository
  2. The name is mandatory. Fill in the name of the directory
  3. For Local path, remove the name of your project directory but leave the path to it. This is the important one. If you don't do this, you get a directory called inside you project directory.
  4. Leave Git ignore on None (otherwise it'll overwrite)
  5. License and Readme, if any, is up to you. GitHub Desktop will overwrite.
  6. Click Create Repository

Proceed as normal

Comments

0

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.

Comments

0

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.