August 7, 2012

Beginner

How To Install Git on Ubuntu 12.04

Tagged In: Ubuntu, Miscellaneous, Git

What the Red Means

The lines that the user needs to enter or customize will be in red in this tutorial!

The rest should mostly be copy-and-pastable.

About Git


Git is a distributed version control system released to the public in 2005. The program allows for non-linear development of projects, and can handle large amounts of data effectively by storing it on the local server.

This tutorial will cover two ways to install Git.

How to Install Git with Apt-Get


Installing Git with apt-get is a quick and easy process. The program installs on the virtual private server with one command:
sudo apt-get install git-core

After it finishes downloading, you will have Git installed and ready to use.

How to Install Git from Source


If you are eager to download the most recent version of Git, it is generally a good idea to install it from the source.

Quickly run apt-get update to make sure that you download the most recent packages to your VPS.
sudo apt-get update

Prior to installing Git itself, download all of the required dependancies:
sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev build-essential

Once they are installed, you can download the latest version of Git from the google code page.
wget https://git-core.googlecode.com/files/git-1.8.1.2.tar.gz

After it downloads, untar the file and switch into that directory:
tar -zxf git-1.8.1.2.tar.gz
cd git-1.8.1.2

If you want to do a global install, install it once as yourself and once as root, using the sudo prefix:
make prefix=/usr/local all
sudo make prefix=/usr/local install

If you need to update Git in the future, you can use Git itself to do it.
git clone git://git.kernel.org/pub/scm/git/git.git

How to Setup Git


After Git is installed, whether from apt-get or from the source, you need to copy your username and email in the gitconfig file. You can access this file at ~/.gitconfig.

Opening it following a fresh Git install would reveal a completely blank page:
sudo nano ~/.gitconfig

You can use the follow commands to add in the required information.
git config --global user.name "NewUser"
git config --global user.email [email protected]

You can see all of your settings with this command:
git config --list

If you avoid putting in your username and email, git will later attempt to fill it in for you, and you may end up with a message like this:
[master 0d9d21d] initial project version
 Committer: root 
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

See More


This tutorial covered how to install Git on your virtual private server. Stay tuned for a second tutorial on Git Basics.



By Etel Sverdlov

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

45 Comments

Write Tutorial
  • Gravatar itommyyang over 1 year

    i just realized how resourceful you guys are!!! :)

  • Gravatar Etel Sverdlov over 1 year

    Thank you! Please let me know if there are any other topics you would like to see covered. =]

  • Gravatar Alexandre Baggott Ayres over 1 year

    awesome tutorial. I'm waiting it basic... :)

  • Gravatar richardkightley over 1 year

    Hi! this only works if you are running certain versions and set ups. The command root@nova-controller:~# apt-get install git -y Reading package lists... Done Building dependency tree Reading state information... Done Package git is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'git' has no installation candidate

  • Gravatar Moisey over 1 year

    That would depend on what repos you are contacting to install packages but it should be supported, have you made any repo changes for apt?

  • Gravatar pranshugupta18 over 1 year

    git config --list does not work,it shows an error fatal: bad config file line 2 in /root/.gitconfig what to do now..?

  • Gravatar Moisey over 1 year

    In a user's home directory the .gitconfig file is the user's specific config variables. You should review that file and see if you have any errors in it as whenever you run git as root that file will be referenced. Also depending on how your application is setup you may want to consider running all of your git repos under another user who is not root for security. In that case the /root/.gitconfig would not be referenced.

  • Gravatar ja.delcallar over 1 year

    How about a post commit hook, so when after I push a commit code to git repo, it will auto upload php files to the lamp/web server which is also hosted by digital ocean. Hope you will have a tutorial for this. I am a newbie.

  • Gravatar Moisey over 1 year

    You can set something up like that, but it kind of breaks the convention behind git, which is meant to commit changes to the repo and then delpoy the code separately. You can use something like capistrano to automate the deployment of your git code after you have updated. This way you can do multiple commits and work with other collaborators without having to worry that each most recent commit automatically gets deployed as this can be error prone, by deploying code that hasn't yet been thoroughly tested.

  • Gravatar it.ravindrasingh about 1 year

    Super and Easy steps given here!!!!

  • Gravatar shrestha.anit about 1 year

    thanks.

  • Gravatar tom about 1 year

    Can this work when you are using putty? I keep getting an error saying bad config file line 1

  • Gravatar yeosamuelh about 1 year

    After the following instructions above, "Opening it following a fresh Git install would reveal a completely blank page: sudo nano ~/.gitconfig" I misunderstood and tried editing in the next commands directly into the that file, that is, when that file was open, I type in this: git config --global user.name "NewUser" git config --global user.email [email protected] This was not the right thing to do. Then, when I later tried trying the same things at the command prompt, it gave me an error, because I already had bad syntax in the file. I had to delete the bad config file lines before the commands would work at the command prompt: git config --global user.name "NewUser" git config --global user.email [email protected] I believe this may answer one of the queries/problems asked above too.

  • Gravatar yeosamuelh about 1 year

    Also, should the quotes around "NewUser" in the same example be there or not, or do they matter? They are not shown around the email example of "[email protected]"

  • Gravatar rdarling about 1 year

    Thanks!

  • Gravatar arthurnoroes about 1 year

    Thank you for the great tutorial! I'm using ubuntu 11.04 and it worked just fine.

  • Gravatar Ben Bishop about 1 year

    Part 2 and Part 3 of this excellent series. https://www.digitalocean.com/community/articles/how-to-use-git-effectively https://www.digitalocean.com/community/articles/how-to-use-git-branches

  • Gravatar kunjalpopat about 1 year

    Usefull Tut :)

  • Gravatar [email protected] 12 months

    Can you please guide on how to install gerrit

  • Gravatar hellowangit 11 months

    thanks before read

  • Gravatar reshmiraji1612 10 months

    THANK YOU..........................

  • Gravatar Kamal Nasser 10 months

    @anil.virtuali: https://www.digitalocean.com/community/articles/how-to-install-gerrit-on-an-ubuntu-cloud-server

  • Gravatar edgardcunha 10 months

    Thank you! \o/

  • Gravatar gautama 9 months

    Thank you, nice tutorial :D

  • Gravatar Marshall Moore 9 months

    I was having problems with these instructions on Ubuntu 12.04. This seems to have worked better for me: sudo apt-get -y install git-core Hope this helps if anyone is having issues.

  • Gravatar paul.logston 9 months

    For me, this post seemed to be a bit outdated. I'm running Ubuntu 12.04LTS. To update the .gitconfig file, I had to write the file in the format shown at this link: https://gist.github.com/bvsatyaram/1500350

  • Gravatar Pablo of vDevices.com 9 months

    @yeosamuelh: Yes, you should wrap the username w/quotes; and the e-mail address should not be wrapped w/quotes.

  • Gravatar egemenozkan 9 months

    I think that somebody understands from this tutorial git config --global user.name "NewUser" git config --global user.email [email protected] adds to file "sudo nano ~/.gitconfig" You should enter git command into prompt window

  • Gravatar la 9 months

    This doesn't work. I just created a new droplet running Ubuntu 12.04 and the command: apt-get install git-core fails because it can't fetch some archives: [beta:~] root$ apt-get install git-core Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: fakeroot libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libc-dev-bin libdpkg-perl libfile-fcntllock-perl libitm1 libquadmath0 manpages-dev Use 'apt-get autoremove' to remove them. The following extra packages will be installed: git git-man libcurl3-gnutls Suggested packages: git-daemon-run git-daemon-sysvinit git-doc git-el git-arch git-cvs git-svn git-email git-gui gitk gitweb The following NEW packages will be installed: git git-core git-man libcurl3-gnutls 0 upgraded, 4 newly installed, 0 to remove and 1 not upgraded. Need to get 241 kB/7,042 kB of archives. After this operation, 15.8 MB of additional disk space will be used. Do you want to continue [Y/n]? y Err http://archive.ubuntu.com/ubuntu/ quantal-updates/main libcurl3-gnutls amd64 7.27.0-1ubuntu1.2 404 Not Found [IP: 91.189.92.200 80] Err http://security.ubuntu.com/ubuntu/ quantal-security/main libcurl3-gnutls amd64 7.27.0-1ubuntu1.2 404 Not Found [IP: 91.189.91.14 80] Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/c/curl/libcurl3-gnutls_7.27.0-1ubuntu1.2_amd64.deb 404 Not Found [IP: 91.189.91.14 80] E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? I tried the --fix-missing with no luck. Any ideas?

  • Gravatar Kamal Nasser 9 months

    @la: As the error says: > E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? Run

    sudo apt-get update
    first.

  • Gravatar la 8 months

    @KamalNasser Thank you! I completely missed that it. I tried --fix-missing with no luck. But apt-get update did the trick. Best.

  • Gravatar felix.johnson 8 months

    bad config file line 1 in /root/.gitconfig. how can I fix that? Any help?

  • Gravatar felix.johnson 8 months

    it`s all blank in cofig page. what should I do?

  • Gravatar Kamal Nasser 8 months

    @felix.johnson: What's the output of

    cat /root/.gitconfig
    ?

  • Gravatar szbzhao 8 months

    Under Ubuntu 12.04, git version is: 1.7.9.5 is very enough to use.

  • Gravatar juancvalenciah 8 months

    thanks guys! your explanations are awesome!

  • Gravatar vskylabv 7 months

    For those that are getting errors, make sure you type it like this: git config --global user.email "[email protected]" see the quotes. See this guide if you're using GitHub: https://help.github.com/articles/set-up-git#platform-linux

  • Gravatar erelsgl 7 months

    Your excellent tutorial appears second in Google for "ubuntu git" :)

  • Gravatar sreenatha.setty 5 months

    Very helpful tutorial... Thank you so much guys....

  • Gravatar amrsubedi 4 months

    Thank you, nice tutorial :D

  • Gravatar tips 3 months

    I had a lot of trouble getting this going, until I realized that your fonts suck. Distinguishing between l and 1 are very difficult unless you closely inspect it. For anyone having trouble pay close attention to the l's and 1's when installing the dependancies.

  • Gravatar Kamal Nasser 3 months

    @tips: What lines are you talking about?

  • Gravatar amithsingh009 3 months

    Thank you so much.

  • Gravatar stockford229 3 months

    I used the first method at the top, it seemed the most idiot(me) proof and all the commands worked (I think) they threw no errors anyway but afterwards in putty all three it has "user name" and it has "email" both those lines fully typed out then under both of them but when I type "git config --list" nothing happens I just get another new line to enter stuff on no errors being thrown, what am I dong wrong, please say someone watches these comments everything has gone perfect till here, love your guys' beginner tutorials, I couldn't even start my first droplet without the tuts lol...

  • Gravatar minamarcos about 1 month

    used to get this msg: The program 'git' is currently not installed. To run 'git' please ask your administrator to install the package 'git' here is how you fix it [code] sudo dpkg --configure -a [/code] if you still having proble installing git

Leave a Comment

Create an account or login:
Ajax-loader