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

gokul_uf
  • 740
  • 1
  • 8
  • 21
HMW
  • 7,973
  • 3
  • 16
  • 4
  • 9
    Maybe I just don't get it, but cannot you just run `git init` inside the local directory? – Philipp Jul 22 '10 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? – Cascabel Jul 22 '10 at 17:54
  • 4
    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 Jun 21 '19 at 16:58

10 Answers10

1268

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 main
JesusIniesta
  • 10,412
  • 1
  • 35
  • 28
abyx
  • 69,862
  • 18
  • 95
  • 117
  • 13
    **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*? – HMW Jul 22 '10 at 18:53
  • 41
    This worked for me also. I had to first create a project `AppName` in GitHub. It wasn't clear to me waht exactly the `` means. So for those who the same question, we simple use GitHub.com, we're not running our repo, and then the `` as used in the 5th line looked something like this: `https://github.com/CompanyName/AppName` – Bart Oct 23 '13 at 11:56
  • 15
    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. – jdusbabek Jun 25 '14 at 16:47
  • What happens if you don't have a git daemon? – icc97 Jun 30 '14 at 11:11
  • 1
    @icc97: Then you can use a hosting service like GitHub and BitBucket to do it for you – abyx Jun 30 '14 at 11:38
  • 3
    Just wanted to add, that I couldn't push until I did a: git pull --rebase – Artemix Nov 25 '14 at 15:28
  • 2
    @jdusbabek. running 'git --bare init' does not work because you cannot do 'git add .' afterwards. see [link](http://stackoverflow.com/questions/1456923/why-am-i-getting-the-message-fatal-this-operation-must-be-run-in-a-work-tree) – nsof Jan 11 '15 at 12:26
  • I sometimes create a local-only git repository for short-term versioning. For this usage you don't need the git remote / git push steps. – yoyo Mar 21 '15 at 22:47
  • 1
    Unfortunately, responses in other threads who point out the need for a .gitignore file, have been marked as duplicate. This is the most upvoted answer, so I add it as a comment here. You should add a file called .gitignore inside , with the following contents: https://github.com/github/gitignore/blob/master/Swift.gitignore Do this before running "git add ." This prevents git from tracking intermediate build files and UI updates that there's no need to track. – Balthazar Sep 06 '15 at 13:15
  • doesn't work... as there are already some files in remote repo and git doesn't know what to do with that. – Flash Thunder Jun 28 '16 at 08:05
  • If you want a message with multiple words, use: "a message here" – Th3B0Y Aug 11 '16 at 02:19
  • 13
    If git rejects because remote contains some minor changes (.README, .gitignore, etc.), try `git pull origin master --allow-unrelated-histories` to do a merge. – karlisup Dec 28 '16 at 12:11
  • Assuming you have a void GitHub repository created, you have at least a README.md file, so you have to run `git pull master` before `git push -u origin master` to make it work properly. – CeuxDruman Sep 29 '17 at 17:02
  • I had to use double quotes for the message: git commit -m "message" – Patrick Koorevaar Apr 14 '20 at 12:13
  • 3
    Just a note, you should change "master" to "main" from now on – aheze Nov 13 '20 at 03:29
  • As aheze noted, I had to write `git push -u origin main` instead of `git push -u origin master` – raphaelrk Apr 16 '21 at 17:23
  • Password authentication will not be allowed now in git. Please follow this also for generating token and perform git operations https://stackoverflow.com/questions/68775869/support-for-password-authentication-was-removed-please-use-a-personal-access-to – Jyothish Bhaskaran Nov 12 '21 at 18:02
  • cannot tell you how many times I have come back to this thread just to read the same answer over and over – James Oct 04 '22 at 22:29
159

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

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • 1
    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. – schlebe Mar 27 '18 at 09:43
  • 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? – gwideman Jan 23 '19 at 01:49
  • Thats a typography mistake, simply copy url of project as shown in screen shot – Hitesh Sahu Jan 23 '19 at 07:48
  • 5
    This is the correct answer and should be the accepted one. The accepted answer is missing your "Synchronize" step. – Ilan Mar 28 '19 at 22:37
  • 1
    I ran into an error because github now initializes new repos with "main" as the default branch instead of "master." – Jon Apr 15 '21 at 20:48
  • Noticed that currently creating a new repo in github sets the default branch to "main" and not "master" – NotSoShabby Dec 27 '21 at 15:00
43

Here's my solution:

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master
csomakk
  • 5,369
  • 1
  • 29
  • 34
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!

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
10

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
polka
  • 1,383
  • 2
  • 25
  • 40
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
    I needed to modify that last statement to be more like `git push -u origin master --force-with-lease` – cfont May 08 '20 at 22:44
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.

Deepam Gupta
  • 2,374
  • 1
  • 30
  • 33
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.

tpage
  • 123
  • 2
  • 8
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> 
Harish Karthick
  • 710
  • 2
  • 12
  • 26
0

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

RolfBly
  • 3,612
  • 5
  • 32
  • 46