This how I took an existing directory of files on my desktop and put it into a new git repository on a remote server. I found a number of how-tos online but none worked for me – certainly this was because I’m such a newbie with git that I wasn’t sufficiently understanding what I was being told to do. The following works for me; clearly this isn’t the only way or the best way to accomplish the task. This is mostly a note to self.
Create an empty directory on the remote server to hold the repository
$ mkdir -p /var/local/git/repos/CrashTesting
Intialize the empty repository
$ git --bare init
Initialized empty Git repository in /var/local/git/repos/CrashTesting/
Now I have the necessary repository components.
$ ls
branches config description HEAD hooks info objects refs
On my desktop, in the existing directory of files, init the directory
$ git init
Initialized empty Git repository in /Users/crashing/Desktop/testws/.git/
This created a .git
directory with git control files.
Next, I tell my local desktop repository about the remote repo. The remote repo is given the short name origin
.
$ git remote add origin ssh://server.crashingdaily.com/var/local/git/repos/CrashTesting
Then, I place my local files under version control.
$ git add .
Now I can commit the local files to the local repository.
$ git commit -a -m 'initialize repo'
[master (root-commit) 7871087] initialize repo
23 files changed, 500 insertions(+), 0 deletions(-)
create mode 100755 build.properties
create mode 100755 build.xml
Finally, push the master
branch to the remote repository named origin
.
$ git push origin master
Counting objects: 44, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (31/31), done.
Writing objects: 100% (44/44), 1.65 MiB, done.
Total 44 (delta 1), reused 0 (delta 0)
To ssh://server.crashingdaily.com/var/local/git/repos/CrashTesting
* [new branch] master -> master
19 comments
Comments feed for this article
May 29, 2011 at 12:18 pm
gunna35
I found this very helpful, thanks. I’d been stuffing up and missing the –bare and many other mistakes. You’ve allowed me to keep a few patches of hair that I haven’t yet pulled out.
August 22, 2011 at 6:05 pm
Graham MacDonald (@grimondo)
Thanks for that – nice and clear instructions.
October 4, 2011 at 3:21 pm
Michał
Thanks, I needed that :)
December 8, 2011 at 6:34 pm
Committing and Pushing to GitHub using JGit – Bare Repo?
[…] https://crashingdaily.wordpress.com/2009/09/02/initing-a-new-remote-git-repository-with-existing-file… […]
March 6, 2012 at 9:52 pm
Заметки по работе с git | Andrew's Home Site
[…] Initing a new remote git repository with existing files; […]
July 12, 2012 at 9:44 pm
Neerav
Finally a concise and the point instructions!!
August 4, 2012 at 2:50 pm
Hi
Thanks, this is super helpful and easy to follow!
August 14, 2012 at 9:56 pm
LL
Thanks dude. I was referring to http://git-scm.com/book/en/Git-on-the-Server-Getting-Git-on-a-Server and trying “git clone –bare my_project my_project.git”. but getting “error: refs/heads/master does not point to a valid object!”
Your tutorial worked for me :-)
December 20, 2012 at 10:56 am
Helmingstay
I’m pretty sure this is the “Wrong Way” to do this…
December 20, 2012 at 11:09 am
Helmingstay
Nevermind, my bad.
January 13, 2013 at 6:56 pm
Jason
Exactly what i was looking for, thanks!
September 12, 2013 at 2:47 pm
Sathya
Awesome !! Thank you so much. I was struggling to get this done all day and lo! your step by step instructions worked magic. Thanks!!
September 18, 2013 at 2:35 am
downforce
Thanks for the easy to follow guide! Was my first search results and exactly what I needed. Thanks
January 30, 2014 at 11:21 am
Sergio
It really works, but you should add how to clone the repo in other machine.
Anyway, it’s very good explained!
January 31, 2018 at 9:57 am
¿Comprometerse y empujar a GitHub usando JGit – Bare Repo? Git & Github
[…] Initing a new remote git repository with existing files […]
February 6, 2018 at 11:02 am
Committing and Pushing to GitHub using JGit – Bare Repo? Git Baby
[…] Initing a new remote git repository with existing files […]
February 8, 2018 at 7:55 am
Commettre et pousser à GitHub en utilisant JGit – Bare Repo? FR Git
[…] Initing a new remote git repository with existing files […]
June 16, 2018 at 3:43 pm
Commit und Pushing zu GitHub mit JGit – Bare Repo? DE Git
[…] Initing a new remote git repository with existing files […]
June 19, 2018 at 1:42 am
Согласование и нажатие на GitHub с использованC
[…] Initing a new remote git repository with existing files […]