Last updated 5 July 2013.

Newer Git versions offer the extraordinarily convenient syntax:
Anonymous:

# Example git clone --branch [branch_name] [anonymous project url]
git clone --branch master http://git.drupal.org/project/devel.git

- - -
If you receive an error related to branch, you might try to clone the repository first, without specifying the branch

git clone http://git.drupal.org/project/drupal.git

rather than

git clone --branch 8.x http://git.drupal.org/project/drupal.git

- - -
Maintainer:

# Example of git clone --branch [branch_name] [maintainer project url] 
git clone --branch master [git_username]@git.drupal.org:project/devel.git

This clones, creates a tracking branch, and checks out the branch all in one tidy and readable command.

Older Git clients

Less recent versions of Git do not support this, in which case, the most succinct syntax is:

git clone [project_url] [directory]
cd [directory]
git checkout --track origin/[existing_branch_name] -b [tracking_branch_name]

So to check out a branch named "devel_feature_branch" of the devel module into a directory named "devel_feature":

git clone http://git.drupal.org/project/devel.git devel_feature
cd devel_feature
git checkout --track origin/devel_feature_branch -b devel_feature_branch

Install newest git version from the Ubuntu Git Maintainers team

# add PPA to your system (Ubuntu 9.10 (Karmic) and later)
sudo add-apt-repository ppa:git-core/ppa

If this returns

add-apt-repository: command not found

...then you need to install the python software properties tool.

sudo apt-get install python-software-properties

...then run the add-apt-repository command, above. Now continue.

# update your archive list
sudo apt-get update

# upgrade git (if you installed it before)
sudo apt-get upgrade
# OR install git
sudo apt-get install git

Install new git version from source on ubuntu

# remove git if it is installed
sudo apt-get remove git git-core

# Install prerequisites
sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev bzip2

# Download latest git source
# http://git-scm.com/download 
wget http://kernel.org/pub/software/scm/git/git-1.7.4.4.tar.bz2 
tar xvjf git-1.7.4.4.tar.bz2

# Compile time!
cd git-1.7.4.4/
./configure
make
sudo make install

#alias git command
alias git="/usr/local/bin/git"

"Remote HEAD refers to nonexistent ref" error

If you do not specify a branch in your clone command, you may receive the following error at the end:

warning: remote HEAD refers to nonexistent ref, unable to checkout.

This will happen if the repository you're trying to clone does not have a "master" branch.

To resolve, simply git checkout to a valid branch.

Comments

lelizondo’s picture

it seems that

git clone --branch [branch_name] [project url] [your_branch_name]

works like:

git clone --branch [branch_name] [project url] [directory_to_save_the_project]

and [branch_name] will be both the source and target branch (remote and local)

(I don't want to hack this node with this without 100% confirmation of this)

Luis

webchick’s picture

Edit away. :)

Kristen Pol’s picture

I'm running an old version of git and cannot update it at this time. So I used the instructions for cloning the "old way", e.g.

git clone http://git-testing.drupal.org/project/seo_friend.git  seo_friend
cd seo_friend
git checkout --track origin/6.x-1.x -b 6.x-1.x

The clone and checkout worked so I made a trivial change (added a space in a file) then did "git commit" which worked. When I try to do "git push", I get an error though. First time it was:

error: Cannot access URL http://git.drupal.org/project/seo_friend.git/, return code 22
error: failed to push some refs to 'http://git.drupal.org/project/seo_friend.git'

So I assume there is some permission thing going on since I never specified a username when cloning/checking out the code. I then updated drupal.org with my public ssh key, wiped out the local directory and tried again like:

git clone http://kepol@git-testing.drupal.org/project/seo_friend.git  seo_friend
cd seo_friend
git checkout --track origin/6.x-1.x -b 6.x-1.x

but I got a similar error:

error: Cannot access URL http://kepol@git.drupal.org/project/seo_friend.git/, return code 22
error: failed to push some refs to 'http://kepol@git.drupal.org/project/seo_friend.git'

I tried one more time using "Kristen Pol" instead of "kepol" because I had changed my username awhile back. That gave the same error.

Unfortunately I cannot update git at this time but I would like to be able to do "git push" so I can update my projects. Any help is greatly appreciated!

Thanks,
Kristen

-Kristen
Blog: http://kristen.org
Drupal 7 Multilingual Sites: http://kristen.org/book

eliza411’s picture

Best to file things like this as an issue in http://drupal.org/project/git_dev for immediate help. You are correct: you can't push to http:/anonymous urls. You'll need the format [git_username]@git.drupal.org:project/seo_friend.git

I'll update the docs accordingly.

Kristen Pol’s picture

I forgot to respond to this months ago. I did, in the end, get 'git push' to work by editing the .git/config file to make sure the url looks like:

url = ssh://kepol@git.drupal.org/project/featured_content.git

Kristen

-Kristen
Blog: http://kristen.org
Drupal 7 Multilingual Sites: http://kristen.org/book

bartl’s picture

It looks like the directory http://git.drupal.org/project/ simply doesn't exist, let alone any files under it.

Beanjammin’s picture

You want http://drupalcode.org/project/, I have updated the URLs above to match. Thanks for pointing this out.

Beanjammin’s picture

Just to clarify - If you want to view the files via browser user the URL http://drupalcode.org/project/project_name, if you want to clone the repository use the URL http://git.drupal.org/project/project_name.

NBZ4live’s picture

Every time I try to clone a drupal repository I becoma an error.

Here an example:
git clone --branch 7.x http://git.drupal.org/project/drupal.git
Initialized empty Git repository in /home/radmin/te/git/drupal/.git/
error: RPC failed; result=22, HTTP code = 417

I'm using "git version 1.7.0.4" on Ubuntu.

I have no problems with our corporate git repositories or github. Only with Drupal repositories.

Beanjammin’s picture

I think you ran into the same issue with the URL as the poster above. Try again with http://drupalcode.org/project/drupal.git as the URL.

Beanjammin’s picture

Sorry, I confused things... There are two URLs for git:

http://git.drupal.org/project/project_name - URL for cloning git repositories
http://drupalcode.org/project/project_name - URL for viewing the repositories with a browser

sylvaticus’s picture

I was used to manage a d6 installation with CVS, and I'd like to to the same now with GIT (by the way.. it tokked me hours to find about the CVS->GIT migration as lots of old CVS related pages are simply displaying an "Access denied" error!).

All I need for now is to checkout the D7 with git.
When 7.1 will be released I'll want of course to update to 7.1
How to perform that ?

vijaycs85’s picture

I'm trying to clone someone's sandbox project (D6 webform_tokens) and getting "authetication failed" (both for anonymous/anonymous and d.o username/password).

Any idea/reference how to do it?

-VJ

lelizondo’s picture

You don't need any kind of authentication, just go to the Version control tab in the project you want to clone and follow instructions.

I'll give you an example, if you want to anonymously clone the latest Drupal project, you'll do:

git clone --branch 8.x http://git.drupal.org/project/drupal.git

Luis

Kristen Pol’s picture

After some troubles, I managed to create a process for creating a new branch based on another one so I thought I'd put my steps here in case it benefits someone. In my case, I wanted to create a 6.x-2.x branch based on the 6.x-1.x branch.

Example:

1) clone repo, e.g.
git clone kepol@git.drupal.org:project/featured_content.git fc

2) go into new dir, e.g.
cd fc

3) grab the branch to copy from remote, e.g.
git checkout --track origin/6.x-1.x -b 6.x-1.x 

4) create a new branch based on the branch above, e.g. 
git checkout -b 6.x-2.x

5) edit .git/config and adjust the url if necessary, e.g. 
url = ssh://kepol@git.drupal.org/project/featured_content.git

6) may also need to adjust the file so it knows about the new branch (I imagine there is a better way to do this with a git command so feel free to comment and I can fix this step), e.g. 
[branch "6.x-2.x"]
  remote = origin
  merge = refs/heads/6.x-2.x

7) push the branch out to remote, e.g.
git push origin 6.x-2.x 

8) start using this branch, e.g.
git checkout 6.x-2.x 

9) now working with the new branch!

Kristen

-Kristen
Blog: http://kristen.org
Drupal 7 Multilingual Sites: http://kristen.org/book

Diogenes’s picture

Yesterday I visited the version control page of a sandbox project of mine. Much to my surprise, the working directory name had changed from members to members_page.

Almost 3 weeks ago I changed the name of the module from 'notbot' to 'members', and made all the attendant changes to the documentation on drupal.org to reflect this. I continued to push to and fetch from this sandbox many times, as I have been developing/testing D6,7,8 versions.

Since it is a module, the working directory from the git sandbox no longer matches any of the code that was written for it. I have to do a module code name change across 3 versions to remedy this now.

Can someone explain why this happened?

Update: Quickly tested the D7 module called members that installed in a directory
htdocs/D7/sites/<site>/modules/members_page
and everything seems to work fine. So no code change is needed? Cool

NBZ4live’s picture

Looks like there is already a full project called Members ( http://drupal.org/project/members ).

Diogenes’s picture

You are right. I had also discovered before this but had forgotten about it.

I need an embarrassed emoticon here. WTF? - NO EMOTICONS with this editor? ;-)

True story - I wanted to change the name of my module. I was logged into Drupal.org at the time so I did a "contributed module" search.

Drupal.org was down - timeout. The first thing to do in a situation like this is to see if you have an internet connection.

Ping? Ha-ha. How quaint. First, you try Google.

Whoa, Google works! Try "drupal member module" as a Google search. Nothing on the first page.

After changing all 3 versions (6-8) to the new name - 'members' and committing with git, I discover I can't push to origin. so I try a different name. I believe that worked (now that I have had time to think about this). So I left it at that.

In the meantime, a whole bunch of edit/add/commit/push/fetch cycles occurred over the last 3 weeks. I never changed my working directory to members_page, always worked with 'members'. There were no problems.

Now I realize that this may be the first time in 3 weeks that I actually did a git clone command. When I saw the directory name generated, I was surprised and thought "that won't work". But it does. It works fine.

On a closing note, the 'members' project you link to is an abandoned mine. And, as I post this, www.php.net site is down.

Don't ya' hate it when that happens?

P.S. BTW - I really love git & Drupal.

NBZ4live’s picture

I didn't search it.
I simply used the URL http://drupal.org/project/<module_alias> and there was a page=)

Diogenes’s picture

Right again. But when I went to look at the time, the whole drupal.org site was down. So I Googled it very quickly and decided to use members. Made the name change to all 3 versions and quickly tested them.

I discovered later, when Drupal.org was up and running again, that Members was taken. So I guess I tried Members Page right after that. It was late at night. It seemed to work and I forgot all about it.

I carried on using my own members working directory over many cycles but I don't ever think I did a git clone command in that time. When I finally did, I was horrified to see the Drupal.org repository showing a working directory of members_page instead of members. I thought that would break the code.

Turns out it doesn't make a whiff of difference. BIG SMILE.

fehin’s picture

subscribing

jlyon’s picture

Not sure if this is the best place to look for support, but I'm having a really weird issue on my localhost. When I try to git clone a project, it just hangs:

$ git clone -v --recursive --branch master http://git.drupal.org/project/webform_pay.git
Cloning into webform_pay...
<hangs>
error: The requested URL returned error: 503 while accessing http://git.drupal.org/project/webform_pay.git/info/refs

fatal: HTTP request failed

I am running git version 1.7.4.1 in Ubuntu 11.04.

On any of my servers (running Ubuntu 10.04, git version 1.7.9), it works fine. This happens when I try to git clone any project. Any ideas?

LonitaD’s picture

Whenever I run git clone with a url copied from a Drupal project, I have to change the http:// to git:// and then it works. I use msysgit version 1.7.4 on Windows XP. I'm not sure if you are having the same problem, but this fixes the HTTP request failed error for me.

NexusStar’s picture

I am using Ubuntu 14.04 and when I run

git clone --branch 7.x http://git.drupal.org/project/drupal.git .

It results in

Cloning into '.'...
remote: Counting objects: 444171, done.
remote: Compressing objects: 100% (87624/87624), done.
fatal: The remote end hung up unexpectedly.08 MiB | 84.00 KiB/s    
fatal: early EOF
fatal: index-pack failed

But with

git clone --branch 7.x git://git.drupal.org/project/drupal.git .

Project is cloned

Could someone explain this?

Entering The Dip and trying to win over it.

BigEd’s picture

Try a using a different terminal, I have seen some that have SSH issues and do not respond during the authentication process and hang.

Aaron23’s picture

When im trying to clone in another system. Im getting enter password . I need my code to be cloned to all. Please, help me

My git url : git clone --branch 7.x-1.x Aaron23@git.drupal.org:project/hide_node_field.git