So installieren und konfigurieren Sie Git auf Ubuntu 22.04


Git auf Ubuntu 22.04 installieren und konfigurieren

Ziel

Git ist eines der bekanntesten Versionsverwaltungssysteme (VCS) der Welt. Aufgrund seiner weiten Verbreitung in den letzten Jahren ist es heute ein unverzichtbares Tool in der Welt der Softwareentwicklung.

In diesem Tutorial erfahren Sie, wie Sie Git auf der Linux-Distribution Ubuntu 22.04 installieren und konfigurieren.

 

Voraussetzungen

Dieses Tutorial setzt voraus, dass Sie über einen VPS, einen Bare-Metal-Server oder wie im vorliegenden Beispiel über eine OVHcloud Compute Instanz mit Ubuntu 22.04 verfügen. Außerdem sollten Sie Grundkenntnisse im Umgang mit der Kommandozeile haben. Sie brauchen Hilfe beim Einrichten einer Public Cloud Instanz mit Ubuntu 22.04? Dann befolgen Sie die Anleitung Erste Public Cloud Instanz erstellen und auf dieser einloggen.

 

Anleitung

In diesem Tutorial installieren und konfigurieren Sie Git und lernen, wie Sie Ihre Commits signieren.

Zum Zeitpunkt der Verfassung dieses Tutorials war Git 2.36.1 die aktuelle LTS-Version.

 

Installation von Git LTS

Um Git auf Ubuntu zu installieren, verwenden Sie den apt-get-Befehl.
Um die aktuellste LTS-Version zu erhalten, muss ein Repository hinzugefügt werden.

sudo add-apt-repository ppa:git-core/ppa

Output:

$ sudo add-apt-repository ppa:git-core/ppa

PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb https://ppa.launchpadcontent.net/git-core/ppa/ubuntu/ jammy main'
Description:
The most current stable version of Git for Ubuntu.

For release candidates, go to https://launchpad.net/~git-core/+archive/candidate .
More info: https://launchpad.net/~git-core/+archive/ubuntu/ppa
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Found existing deb entry in /etc/apt/sources.list.d/git-core-ubuntu-ppa-jammy.list
Adding deb entry to /etc/apt/sources.list.d/git-core-ubuntu-ppa-jammy.list
Found existing deb-src entry in /etc/apt/sources.list.d/git-core-ubuntu-ppa-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/git-core-ubuntu-ppa-jammy.list
Adding key to /etc/apt/trusted.gpg.d/git-core-ubuntu-ppa.gpg with fingerprint E1DD270288B4E6030699E45FA1715D88E1DF1F24
Hit:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease                    
Get:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]                              
Ign:4 https://pkg.jenkins.io/debian-stable binary/ InRelease                                                                                                 
Hit:5 https://pkg.jenkins.io/debian-stable binary/ Release                                                                                     
Hit:6 https://ppa.launchpadcontent.net/git-core/ppa/ubuntu jammy InRelease       
Hit:7 http://security.ubuntu.com/ubuntu jammy-security InRelease                 
Fetched 99.8 kB in 1s (112 kB/s)
Reading package lists... Done

Installieren Sie Git mit dem apt-get-Befehl:

sudo apt-get update && sudo apt-get -y install git

Output:

$ sudo apt-get update && sudo apt-get -y install git

Hit:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease                                       
Hit:3 http://security.ubuntu.com/ubuntu jammy-security InRelease                                                 
Get:4 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]                           
Hit:5 https://ppa.launchpadcontent.net/git-core/ppa/ubuntu jammy InRelease                                       
Ign:6 https://pkg.jenkins.io/debian-stable binary/ InRelease                                                     
Hit:7 https://pkg.jenkins.io/debian-stable binary/ Release
Fetched 99.8 kB in 1s (113 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  git-man
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
The following packages will be upgraded:
  git git-man
2 upgraded, 0 newly installed, 0 to remove and 27 not upgraded.
Need to get 7700 kB of archives.
After this operation, 8810 kB of additional disk space will be used.
Get:1 https://ppa.launchpadcontent.net/git-core/ppa/ubuntu jammy/main amd64 git amd64 1:2.36.1-0ppa1~ubuntu22.04.1 [5768 kB]
Get:2 https://ppa.launchpadcontent.net/git-core/ppa/ubuntu jammy/main amd64 git-man all 1:2.36.1-0ppa1~ubuntu22.04.1 [1933 kB]
Fetched 7700 kB in 0s (22.9 MB/s)
(Reading database ... 131441 files and directories currently installed.)
Preparing to unpack .../git_1%3a2.36.1-0ppa1~ubuntu22.04.1_amd64.deb ...
Unpacking git (1:2.36.1-0ppa1~ubuntu22.04.1) over (1:2.34.1-1ubuntu1.2) ...
Preparing to unpack .../git-man_1%3a2.36.1-0ppa1~ubuntu22.04.1_all.deb ...
Unpacking git-man (1:2.36.1-0ppa1~ubuntu22.04.1) over (1:2.34.1-1ubuntu1.2) ...
Setting up git-man (1:2.36.1-0ppa1~ubuntu22.04.1) ...
Setting up git (1:2.36.1-0ppa1~ubuntu22.04.1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...                                                                                             
Scanning candidates...                                                                                            
Scanning linux images...                                                                                          

Restarting services...
 ystemctl restart irqbalance.service jenkins.service packagekit.service polkit.service postgresql@14-main.service qemu-guest-agent.service ssh.service udisks2.service
Service restarts being deferred:
 systemctl restart ModemManager.service
 systemctl restart networkd-dispatcher.service
 systemctl restart systemd-logind.service
 systemctl restart unattended-upgrades.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

Überprüfen Sie die Installation und lassen Sie sich die Git-Version anzeigen:

git --version

Output:

$ git --version

git version 2.36.1

 

Basiskonfiguration: Name und E-Mail

Sobald Git installiert ist, ist es ratsam, einige Konfigurationen vorzunehmen. Das gilt insbesondere für die mit Ihren Commits verbundene Identifizierung. Anstatt diese Konfiguration für jedes Repository einzeln vorzunehmen, können Sie sie auch global mithilfe einer .gitconfig-Datei durchführen, die sich im Stammverzeichnis des home-Benutzers befindet.

Diese Datei kann manuell oder mithilfe der config-Option und dem git-Befehl erstellt werden:

git config --global user.name "Firstname Lastname" git config --global user.email "firstname.lastname@mail.com"

Zeigen Sie Ihre .gitconfig-Datei in Ihrem home-Verzeichnis an:

cat .gitconfig

Output:

$ cat.gitconfig
 
[user]
	name = Firstname Lastname
	email = firstname.lastname@mail.com

 

Erweiterte Konfiguration: signierte Commits

Es wird ausdrücklich empfohlen, dass Sie Ihre Commits signieren. Der Grund dafür ist, zu gewährleisten, dass Sie der Eigentümer des Commits sind und dass sich nicht einfach nur jemand als Sie ausgibt. Wie Sie bereits gesehen haben, sind der Name und die E-Mail in Remote-Repositorys wie GitHub oder GitLab für jeden einsehbar. Jeder kann einfach die gleichen Daten eingeben und behaupten, Sie zu sein. Um dies zu verhindern, wird das gpg-Tool zur Erstellung eines öffentlichen kryptografischen Schlüssels verwendet, mit dem die Echtheit der Herkunft eines Commits überprüft werden kann.

Alle Befehle verwenden das Dienstprogramm gpg, das bereits auf Ubuntu vorinstalliert ist.

Generieren Sie den Schlüssel:

gpg --generate-key

Output:

$ gpg --generate-key

gpg (GnuPG) 2.2.27; Copyright (C) 2021 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Note: Use "gpg --full-generate-key" for a full featured key generation dialog.

GnuPG needs to construct a user ID to identify your key.

Real name: Firstname Lastname
Email address: firstname.lastname@email.com
You selected this USER-ID:
    "Firstname Lastname "

Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key BECE6D85FDA1B6C8 marked as ultimately trusted
gpg: directory '/home/ubuntu/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/ubuntu/.gnupg/openpgp-revocs.d/18E345A3F27325A8B4816875BECE6D85FDA1B6C8.rev'
public and secret key created and signed.

pub   rsa3072 2022-06-17 [SC] 
      18E345A3F27325A8B4816875BECE6D85FDA1B6C8
uid                      Firstname Lastname 
sub   rsa3072 2022-06-17 [E] 

Überprüfen Sie den generierten Schlüssel:

gpg --list-secret-keys --keyid-format=long

Output:

$ gpg --list-secret-keys --keyid-format=long

gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: next trustdb check due at 2024-06-16
/home/ubuntu/.gnupg/pubring.kbx
-------------------------------
sec   rsa3072/ABCDEFGHIJKLMNOP 2022-06-17 [SC] 
      SSSSSSSSSSSSSSSSSSSSSSSSABCDEFGHIJKLMNOP
uid                 [ultimate] Firstname Lastname 
ssb   rsa3072/ZZZZZZZZZZZZZZZZ 2022-06-17 [E] 

 Fügen Sie den Schlüssel zu Ihrer Git-Konfiguration in der .gitconfig-Datei in Ihrem home-Repository hinzu:

git config --global commit.gpgsign true git config --global gpg.program gpg git config --global user.signingkey ABCDEFGHIJKLMNOP

Extrahieren Sie Ihren Schlüssel und kopieren Sie ihn in Ihren Remote-Git-Provider (GitLab, GitHub, BitBucket, ...):

gpg --armor --export ABCDEFGHIJKLMNOP

Output:

$ gpg --armor --export ABCDEFGHIJKLMNOP

-----BEGIN PGP PUBLIC KEY BLOCK-----

TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4g
SW4gYmliZW5kdW0gaGVuZHJlcml0IG5pYmggaWQgZ3JhdmlkYS4gUGhhc2VsbHVzIG5lYyBhbGlx
dWFtIG1hc3NhLCBmcmluZ2lsbGEgc29sbGljaXR1ZGluIG1ldHVzLiBQaGFzZWxsdXMgc2l0IGFt
ZXQgdGluY2lkdW50IG9yY2kuIERvbmVjIHF1aXMgdml2ZXJyYSBsaWd1bGEuIE5hbSBwaGFyZXRy
YSBlbGl0IGV4LCBzaXQgYW1ldCB0aW5jaWR1bnQgbmliaCB0ZW1wb3IgdmVsLiBOdWxsYSBzZW1w
ZXIgcGxhY2VyYXQgZGlhbSBzaXQgYW1ldCB2YXJpdXMuIFN1c3BlbmRpc3NlIHRyaXN0aXF1ZSBl
Z2V0IGxvcmVtIGV1IHBsYWNlcmF0LiBOdW5jIGlhY3VsaXMgbnVuYyBldCBuZXF1ZSBlZmZpY2l0
dXIgZnJpbmdpbGxhLiBWZXN0aWJ1bHVtIG1vbGxpcyBldWlzbW9kIG1hZ25hIHNpdCBhbWV0IGlu
dGVyZHVtLiBQZWxsZW50ZXNxdWUgaW1wZXJkaWV0IGxvcmVtIG51bGxhbS4g

-----END PGP PUBLIC KEY BLOCK-----

Geschafft! Sie haben erfolgreich Git auf Ubuntu 22.04 installiert und konfiguriert.