There is a great deal of documentation and many posts on Git out there, so this is more of a note to self as I keep forgetting the steps needed to set up a remote repository and doing an initial “push”.
So, firstly setup the remote repository:
ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare
git update-server-info # If planning to serve via HTTP
exit
On local machine:
cd my_project
git init
git add *
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master
Done!
Team members can now clone and track the remote repository using the following:
git clone git@example.com:my_project.git
cd my_project
Bonus
To have your terminal prompt display what branch you are currently on in green, add the following to your ~/.bash_profile (I have my current directory displayed in cyan):
function git-branch-name {
git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
local branch=`git-branch-name`
if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "
Pingback: Showing the current Git branch in your bash prompt « duesenklipper
That is a great bonus tip! That little ~3 liner will save me countless (50, 60, who knows?!) gba (git branch all) calls in a day. I’ve actually trained myself that to ‘gba’ whenever I enter into a git directory to know which of the several branches I left myself in.
I think you just made my evening…. and probably even my week. Thank you, thank you, thank you. 🙂
I liked your tips on how to have your terminal prompt display any Git branch name. Added it to my bashrc.
Nice tip.. I am new bee.. and couldn’t understand the so many commands. 🙂
Thanks for nice post . keep posting
Thanks! I’ve added the bit about how to create the empty remote repository, to the erratum for the relevant section of the book “Pragmatic Version Control Using Git” (http://pragprog.com/titles/tsgit/errata)
Your info was exactly what I was looking for. Cheers!
Pingback: 6/12/2010 « Holy Sinster
Thanks so much for this. Been looking around for how to setup a bare repo and do that initial push.
thx! Great tip!
Thanks for the good tip! I’ve been a Subversion guy for a long time and decided to give git a try for my latest project. I’m still not completely sold, but the info you’ve given has made the initial setup a bit easier.
@Jonathan No problem at all, glad it helped. I struggled with this basic stuff when I switched to Git too, it’s a whole other mindset to Subversion.
I beg your parden but can you clarify whether I need to create a user account “git” and makdir my_project.git in its home directory?
(I presume that you mean that but it helps to state it.)
@Yoichi Yes that is correct, sorry I should have been more explicit.
YES! Finally it works! Thank you for the great tipps! I love the colored branch name in my command line!
Pingback: GitHub ile SSH kullanarak Haberleşmek ve ilk Push işlemini Gerçekleştirmek | Taha Yavuz Bodur weblogging..
Instead of requiring the git-track function that you have created, you can just do this instead:
git push -u origin master
The remote branch is then automatically tracked. Thanks for the help creating my remote repo though :o)
Thanks @frak
Pingback: Git remote repository 생성 방법 « 살군의 보조기억 장치
Some good ideas. I’m looking forward to adding them to my GIT toolkit.
Pingback: Git remotes « Bassed
Pingback: Git – setting up a remote repository and doing an initial ‘push’ « The Lucid | OpCode1300's
This works fine with bitbucket.org
Thanks 🙂
In
ssh git@example.com
mkdir my_project.git
cd my_project.git
git init –bare
git-update-server-info # If planning to serve via HTTP
exit
The server does not recognize “git-update-server-info” but does appear to accept “git update-server-info”
When I try the second paragraph,
“git push origin master”
I get:
! [remote rejected] master -> master (branch is currently checked out)
Any ideas? Why is it trying to push “master” when I’m telling it “origin”?
I even logged back into my server and try to create a branch called “origin”, it returns
$git branch origin
fatal: Not a valid object name: ‘master’.
Also tried to commit (since it said “branch is currently checked out”) and I got this
$git commit -a
fatal: This operation must be run in a work tree
Thanks
One Word…Godsend! Thank you!
Fantastic!! thank you, it’s been so hard to find a writeup this concise… all i needed were these steps!
No problem, glad you found it useful.
The only problem is after the “Done!” part 🙂 Where are my files on the remote machine???
I google this page every single time I set up a new repository. Thank you!
Thanks for the “push -u” pointer @frak! I had figured out how to do:
“git push –all origin” and then
“git branch –set-upstream master origin/master”
— I didn’t realize you could combine the two as
“git push -u –all origin”
@Jason Your content will be in the master branch on the remote machine.
@frak I have updated the article with your “git push -u” suggestion, many thanks.
Wonderful article on github. really useful. Thanks for sharing it
Thanks for the tips
My situation: local branch in my laptop, cloned from origin git repo. On same git repo cloned on an other host. I want to see the git log on this host from my latopop:
> git remote add myTag ssh://user@host:/path/to/git/repo
> # Log of origin ok
> git log -n 5 origin
> But not on remote host
> git log -n 5 myTag
> fatal: ambiguous argument ‘myTag’: unknown revision or path not in the working tree.
> Use ‘–‘ to separate paths from revisions
How do I undo a commit to the remote repo?
I would be inclined to to a gig log to show recent commits, then git checkout [the hash of the commit to go back to]. Then commit these changes to master and push.
Pingback: Git Resources and Tutorials | luiscberrocal
Pingback: Come avere un proprio server Git senza usare GitHub | 10tank blog
Thanks for the post. I’m just trying to work with git after an Subversion.
So I did everything as you said, but when I push something to my remote repository from my local machine, there’s nothing in the remote repository although the push itself seems to be a success. Master-branch is the only branch on the remote repo and there’s nothing I pushed there. What am I doing wrong?
@Damnit The instructions are to setup a bare repository on the remote machine, this means that there will be no working copy and everything is stored within the ‘.git’ repository directory. If you try cloning the remote repo with ‘git clone user@host:repo.git’, you will most likely find it works as expected.
If you want the remote machine to have a working copy, just leave off the ‘–bare’ option when initialising the repo. Hope this helps.
Thank you very much for the instructions. It’s the only quick tutorial I’ve found that really works for me.
Thanks for the tip! I’ve been looking for something just as concise and to the point as this!
It’s a pleasure… hope to do a few more Git related articles in the near future.
Pingback: Git – New Remote Repository with Initial “Push” | Pivot Solutions
It shoud be git update-server-info not git-update-server-info
Otherwise, thanks for help 🙂
Good catch Mike, thanks. Updated article appropriately.
Hi,
I new to git now i can push to the local machine but how to push to the another machine. and
what is example.com in
ssh git@example.com
mkdir my_project.git
cd my_project.git
git init –bare
git update-server-info # If planning to serve via HTTP
exit
In the example, example.com is a domain name pointing to the server you wish to push to. This could just as easily be an IP address.
The example also assumes a ‘git’ user, so using an IP address you’d end up with something like: ‘ssh git@1.2.3.4’.
Thanks for the tip! It is very clear and succinct.
Thank you! I’m looking for this 🙂
great! thanks!
does not work for me !
fatal: ‘me@myremote/xxx…/’ does not appear to be a git repository
fatal: The remote end hung up unexpectedly
Are you 100% that ‘me@myremote/xxx…/’ has had ‘git init –bare’ run on it.
Pingback: my git reminder
Make sure the git user and group owns the repository directory on the remote. If it doesn’t, use the chown command to make the proper changes.
Ah, you have to create the remote project first. I skipped that step thinking it would be created automatically with the first push. Of course, it took a few more dead ends to figure out how to get everything in sync again since the remote repository was created after the local one.
It’s not difficult, just a little confusing figuring it out.
It would be helpful to update the post for anyone who might make the same mistake. 😉
Thanks for the tip!
I love this site!
Pingback: Getting Started with GIT for Designers
Thank you! This is really useful!
I receive the following error in the shell after adding the bonus function to my .bash_profile:
sed: -e expression #2, char 4: unterminated `s’ command
Pingback: Setting up GIT on a Windows computer | Diary of a Web Dev
Pingback: Git branch in prompt | jh.net
Pingback: How to push to git on EC2 - Tech Forum Network
Great, straightforward instructions. Thanks!
This short post helped me do in 10 minutes what I struggled with for hours. Thanks!
Pingback: 让 shell 显示当前 git 的分支名称 » Gopher beyond Eliphants
Hi,
I am trying the following and getting the request to enter password. Which password is this ? I am not able to log in with the password which I used for the server at bootup.
sudo git push -u origin master
git@192.168.1.81‘s password:
Syed
@Syed This will be your ‘git’ user’s password. I’m assuming you have added a ‘git’ user.
Hi,
I am gettting the following error . The only difference in steps is that I had to use “git add .” instead of “git add *”.
mohsin@KubuntuVM:~/my_project$ sudo git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to ‘git@192.168.1.81:/opt/my_project.git’
Mohsin
@Mohsin It looks like it can’t find the repository, are you certain that you are using the correct path?
Hi Jamie,
I think the error comes when you have no files in my_project. Once I added a file to my_project/ and repeated the procedure it all worked.
One final question on terminal prompt displaying what branch you are currently on.
I am using Kubuntu and could not find ~/.bash_profile file. I only have ~/.profile. Do we have to add the code to the ~/.profile ?
Also, are any changes required or just copy the exact code ? I am asking because, I did add the lines to my .profile and could not login the next time.
Thanks,
Mohsin
@Mohsin Adding to the ~/.profile file should do the trick… I will post my updated prompt among other things in a few days.
This was really great thank you, first time getting into git today and I found this very helpful! Great resource for a learner and well explained, consider yourself bookmarked!
Thanks 😀
No problem, glad it helped.
Pingback: git remote cluster & laptop setup for one developer — why a "bare" repository?QueryBy | QueryBy, ejjuit, query, query by, queryby.com, android doubt, ios question, sql query, sqlite query, nodejsquery, dns query, update query, insert q
Pingback: How to push to git on EC2 | Ask Programming & Technology
Thanks Jamie, for the post. It helped!!
Pingback: 让 shell 显示当前 git 的分支名称multiprocess | multiprocess
Pingback: 让 shell 显示当前 git 的分支名称 | Multiprocess
Your post almost 4 years old and still very usable. Thank you!! 😀
You’re welcome.
Pingback: GIT server 설정 | CODE Paint
Pingback: Private Remote Git Repository Debian Wheezy and other stories from Hell » Number ONE
Fantastic, really quick to pick up. Just one thing – I had to put a ‘/’ in front of the path to the *.git folder on the server e.g.
git remote add origin git@example.com:/y_project.git
Cheers!
Thanks for putting this up. I find myself searching every time I need to do this, and the last couple times have ended up here. Cheers.
No problem, glad you found it useful.
Asking questions are actually fastidious thing
if you are not understanding something entirely, except this
post offers good understanding even.
And yet, this was the first google entry for the search “git push to new remote repository”. Congrats.
Pingback: Add git status to your bash prompt | Code Snippets
Thank you for the write-up — it put me on the right track. Please consider updating your write-up to include an explanation of the post-receive hook and how to implement this and where to save it — something like this:
Create a file post-receive in the hooks directory of your repository:
#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f
Make sure the target directory exists, as Git won’t create it for you. Finally, set permissions on the file:
$ chmod +x hooks/post-receive
Pingback: git distant bunch & notebook setup for just one creator — why a “simple” archive? | CodersDiscuss.com
What I like about this article is that it is simple, to the point, and has none of the the cruft a lot of other tuts on the same subject have. Brilliant!
Thanks.
More git tips here:
http://wiki.glitchdata.com/index.php?title=Git
Dear Lucid,
do not assume anything. If something can go wrong it will definitely go wrong and it always goes wrong.
You could add a few lines when explaining how to set up the initial repository on the remote server, stating with CAPITALS that:
YOU GUYS NEED FIRST TO CREATE A git USER give it a password which you are going to share with your friends and use the following command for permissions or whatever.
Otherwise people are confused.
Thank you! I forget to add git remote add origin … 😉
Pingback: Using Git’s post-receive hook to publish WordPress themes | Proper Design
Hi, first thank you for your instructions, Git is going difficult to me.
I follow your steps carefully but when finally I push my local files to the remote bare repository it looks like run well, I meen I took a bit time but the files are not sent to the remote server project folder???
It is necesary to push there by using FTP??
Thanks in advance!!
Rubén
Pingback: How to initialize a project with git | Jianglin's Blog
Thanks alot man !!!!!
Pingback: Setting up a svn server on FreeBSD. | the explorator
Thx, but this will not work on gitorious because gitorious doesn’t provide shell acces
Pingback: Derry Birkett › Getting Started with GIT for Designers
great tutorial! thanks
Thanks a lot.
Also, you should run
git init --bare
with--shared
parameter if you are using central git repository with team:$ git init --bare --shared foo.git
Initialized empty shared Git repository in /git/foo.git/
$ chgrp -R devteam foo.git
How to change the default git editor to be emacs or vim or something else?
@Simeon
To setup editor follow this steps: http://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#Basic-Client-Configuration
Great tutorial, thanks!
Thanks Saeid Zebardast, without –shared I was getting “error: failed to push some refs”.
I’m used to doing to contribute to repositories but for some reason is missed out in the bitbucket instructions for how to push your first commit – thought perhaps the ‘git remote add origin git@bitbucket.
Pingback: Common Git commands | Netgloo Blog
Pingback: Git: setting up a remote repository and doing an initial push – Davide’s blog