Tutorial

How To Install Git on CentOS 7

Updated on January 24, 2022
English
How To Install Git on CentOS 7
Not using CentOS 7?Choose a different version or distribution.
CentOS 7

Introduction

Version control has become an indispensable tool in modern software development. Version control systems allow you to keep track of your software at the source level. You can track changes, revert to previous stages, and branch off from the base code to create alternative versions of files and directories.

One of the most popular version control systems is git. Many projects maintain their files in a Git repository, and sites like GitHub, GitLab, and Bitbucket have made sharing and contributing to code with Git easier than ever.

In this guide, we will demonstrate how to install Git on a CentOS 7 server. We will cover how to install the software in a couple of different ways, each with their own benefits, along with how to set up Git so that you can begin collaborating right away.

Prerequisites

Before you begin with this guide, there are a few steps that need to be completed first.

You will need a CentOS 7 server installed and configured with a non-root user that has sudo privileges. If you haven’t done this yet, you can run through steps 1–4 in the CentOS 7 initial server setup guide to create this account.

Once you have your non-root user, you can use it to SSH into your CentOS server and continue with the installation of Git.

Step 1 — Installing Git

The easiest way to install Git is from CentOS’s default software repositories. This is the fastest method, but the Git version that is installed this way may be older than the newest version available. If you need the latest release, consider compiling git from source.

Use yum, CentOS’s native package manager, to search for and install the latest git package available in CentOS’s repositories:

  1. sudo yum install git

If the command completes without error, you will have git downloaded and installed. To double-check that it is working correctly, try running Git’s built-in version check:

  1. git --version

If that check produced a Git version number, then you can now move on to setting up Git.

Step 2 — Setting Up Git

Now that you have git installed, you will need to configure some information about yourself so that commit messages will be generated with the correct information attached. To do this, use the git config command to provide the name and email address that you would like to have embedded into your commits:

  1. git config --global user.name "Your Name"
  2. git config --global user.email "you@example.com"

To confirm that these configurations were added successfully, we can see all of the configuration items that have been set by typing:

  1. git config --list
Output
user.name=Your Name user.email=you@example.com

This configuration will save you the trouble of seeing an error message and having to revise commits after you submit them.

Conclusion

You should now have git installed and ready to use on your system. To learn more about how to use Git, check out these more in-depth articles:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
7 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This article is missing a key ingredient: cURL.

This enables the HTTPS remote helper when cloning repositories.

This is how you can enable the HTTPS remote helper:

  1. Install the libcurl-devel package in addition to the packages mentioned

sudo yum install curl-devel

  1. Add --with-curl flag to configure

./configure --prefix=/usr --with-curl

  1. Run make as normal

Now you can git clone https://.... Enjoy!

Instructions like these are hugely helpful. Thanks.

I don’t know what is best, but I used

./configure --prefix=/usr

to overwrite the older yum installed version.

how to install in docker in centos-9server,
my email== rritsoftwaresolutions@gmail.com

india , + 91 9505782251, watsup

WHat about uninstalling it? There’s no instructions on that…

Ironically enough, using the group install “Development Tools” actually installs the git package from yum, which is what I’m trying to avoid. To that end, I instead installed all the build prerequisites specifically, as described on https://git-scm.com/book/en/v2/Getting-Started-Installing-Git The only difference is that perl wasn’t specifically mentioned there.

    - gcc
    - automake
    - autoconf
    - perl-CPAN
    - perl-devel
    - libcurl-devel
    - expat-devel
    - gettext
    - zlib-devel
    - openssl-devel

Here’s an ansible playbook to do the above process…

  • name: configure test01 hosts: test01 vars: git_version: “2.14.2” # https://github.com/git/git/releases git_user_name: “{{ ansible_hostname }}” git_user_email: “{{ ansible_hostname }}@company.com” tasks:

    https://www.digitalocean.com/community/tutorials/how-to-install-git-on-centos-7

    • name: check git version shell: “git --version 2>&1 | grep {{ git_version }}” failed_when: false changed_when: false register: git_versions_match

    https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

    • name: install build prerequisites become: yes yum: name: “{{ item }}” state: latest with_items:

      • gcc
      • automake
      • autoconf
      • perl-CPAN
      • perl-devel
      • libcurl-devel
      • expat-devel
      • gettext
      • zlib-devel
      • openssl-devel when: git_versions_match.rc != 0
    • name: download git {{ git_version }} get_url: url: https://github.com/git/git/archive/v{{ git_version }}.tar.gz dest: /tmp/git.tar.gz when: git_versions_match.rc != 0

    • name: extract git.tar.gz unarchive: src: /tmp/git.tar.gz dest: /tmp remote_src: yes when: git_versions_match.rc != 0

    • name: make configure make: chdir: /tmp/git-{{ git_version }} target: configure when: git_versions_match.rc != 0

    • name: configure command: ./configure --prefix=/usr/local args: chdir: /tmp/git-{{ git_version }} when: git_versions_match.rc != 0

    • name: make install remote_user: root make: chdir: /tmp/git-{{ git_version }} target: install when: git_versions_match.rc != 0

    • name: git config user name git_config: name: user.name value: “{{ git_user_name }}” scope: global

    • name: git config user email git_config: name: user.email value: “{{ git_user_email }}” scope: global

    • name: git config rebase on pull git_config: name: pull.rebase value: true scope: global

    • name: git config branch.autosetuprebase always git_config: name: branch.autosetuprebase value: always scope: global

After installing git from source following this tutorial, I can’t run git command elsewhere other than /usr/local/bin. I need to create a symbol file for it. ln -s /usr/local/bin/git /usr/bin/git

A very useful tutorial, thanks!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel