Installing Git on Ubuntu 24.04

Installing Git on Ubuntu 24.04

在 Ubuntu 24.04 上安装 Git

Git is an open-source distributed version control system used to track changes in projects and during software development. Git allows multiple contributors to simultaneously work on a single project, maintain and track the history of all changes. It enables efficient collaboration and allows developers to synchronize changes using Git-compatible platforms such as GitHub, GitLab, and Bitbucket.

Git 是一个开源的分布式版本控制系统,用于跟踪项目和软件开发过程中的更改。Git 允许多个贡献者同时处理同一个项目,并维护和跟踪所有更改的历史记录。它实现了高效的协作,并允许开发人员使用 GitHub、GitLab 和 Bitbucket 等兼容 Git 的平台来同步更改。

This guide walks through installing and configuring Git on Ubuntu 24.04. By the end, you’ll have the latest Git version installed and configured to work with local and remote repositories in your project environment. Before you begin, you need access to an Ubuntu 24.04 instance to install Git as a non-root sudo user.

本指南将介绍如何在 Ubuntu 24.04 上安装和配置 Git。阅读完本指南后,您将安装好最新版本的 Git,并配置好在项目环境中与本地和远程仓库协同工作。在开始之前,您需要访问一个 Ubuntu 24.04 实例,并以非 root 的 sudo 用户身份安装 Git。

1. Verify the Default Git Version

1. 验证默认 Git 版本

Git is available by default on Ubuntu 24.04, but the default version may be outdated and not the latest.

  1. View the default installed Git version: $ git --version

Ubuntu 24.04 默认提供 Git,但默认版本可能已过时,并非最新版本。

  1. 查看默认安装的 Git 版本: $ git --version

Your output should be similar to the one below. git version 2.43.0

您的输出应类似于以下内容。 git version 2.43.0

  1. Run the Git command and verify that the default help page displays: $ git

  2. 运行 Git 命令并验证是否显示默认帮助页面: $ git

If Git is unavailable or outdated on your workstation, follow the steps below to install the latest Git version on your instance.

如果您的工作站上没有 Git 或版本过旧,请按照以下步骤在您的实例上安装最新版本的 Git。

2. Install Git Using APT

2. 使用 APT 安装 Git

Git is available in the default APT package repositories on Ubuntu 24.04.

  1. Add the Git PPA to the APT package repository sources: $ sudo add-apt-repository ppa:git-core/ppa

Ubuntu 24.04 的默认 APT 软件包仓库中提供了 Git。

  1. 将 Git PPA 添加到 APT 软件包仓库源中: $ sudo add-apt-repository ppa:git-core/ppa

  2. Update the APT package index: $ sudo apt update

  3. 更新 APT 软件包索引: $ sudo apt update

  4. Install Git: $ sudo apt install git -y

  5. 安装 Git: $ sudo apt install git -y

This command installs the latest Git version and replaces the default version installed on your system. To install Git and all extra tools, install the git-all package instead. $ sudo apt install git-all

此命令将安装最新版本的 Git 并替换系统上安装的默认版本。若要安装 Git 及所有额外工具,请安装 git-all 软件包。 $ sudo apt install git-all

  1. Verify the installed Git version: $ git --version

  2. 验证已安装的 Git 版本: $ git --version

Your output should be similar to the one below. git version 2.48.1

您的输出应类似于以下内容。 git version 2.48.1

3. Install a Specific Git Version From Source Code

3. 从源代码安装特定版本的 Git

Specific Git versions offer enhanced features and compatibility options you can use with applications or platforms like GitHub.

  1. Install all required dependency packages for compiling Git: $ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc -y

特定的 Git 版本提供了增强的功能和兼容性选项,您可以将其与 GitHub 等应用程序或平台配合使用。

  1. 安装编译 Git 所需的所有依赖包: $ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc -y

  2. Visit the Git releases page and use wget to download your target .tar.gz Git source code archive depending on the version. For example, git-2.48.1.tar.gz. $ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.48.1.tar.gz

  3. 访问 Git 发布页面,并根据版本使用 wget 下载目标 .tar.gz Git 源代码压缩包。例如:git-2.48.1.tar.gz。 $ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.48.1.tar.gz

  4. Extract files from the archive: $ tar -zxvf git-2.48.1.tar.gz

  5. 从压缩包中提取文件: $ tar -zxvf git-2.48.1.tar.gz

  6. Navigate to the Git directory depending on the downloaded version: $ cd git-2.48.1/

  7. 根据下载的版本进入 Git 目录: $ cd git-2.48.1/

  8. Compile the source code to the /usr/local directory using Make: $ sudo make prefix=/usr/local all

  9. 使用 Make 将源代码编译到 /usr/local 目录: $ sudo make prefix=/usr/local all

  10. Install the compiled Git package: $ sudo make prefix=/usr/local install

  11. 安装编译好的 Git 软件包: $ sudo make prefix=/usr/local install

  12. Test the installed Git version: $ git --version

  13. 测试已安装的 Git 版本: $ git --version

4. Configure Git

4. 配置 Git

Git requires a valid username and email to include on commit messages in your project.

  1. Add a global username to use with Git: $ git config --global user.name "Your Full Name"

Git 需要有效的用户名和电子邮件地址,以便将其包含在项目的提交信息中。

  1. 添加用于 Git 的全局用户名: $ git config --global user.name "Your Full Name"

  2. Add a global email to use with Git: $ git config --global user.email "email@example.com"

  3. 添加用于 Git 的全局电子邮件地址: $ git config --global user.email "email@example.com"

  4. Test the Git configuration and verify that your account information is available: $ git config --list

  5. 测试 Git 配置并验证您的账户信息是否可用: $ git config --list

Open the .gitconfig file in your user’s home directory to modify the Git configuration directly without using commands. $ nano ~/.gitconfig

打开用户主目录中的 .gitconfig 文件,无需使用命令即可直接修改 Git 配置。 $ nano ~/.gitconfig

5. Access and Use Git on Ubuntu 24.04

5. 在 Ubuntu 24.04 上访问和使用 Git

  1. Create a new project directory such as demo-project: $ mkdir demo-project

  2. 创建一个新的项目目录,例如 demo-project$ mkdir demo-project

  3. Navigate to the demo-project directory: $ cd demo-project

  4. 进入 demo-project 目录: $ cd demo-project

  5. Create a new sample file such as file.txt: $ echo "Hello, Git Works!!" > file.txt

  6. 创建一个新的示例文件,例如 file.txt$ echo "Hello, Git Works!!" > file.txt

  7. Initialize a new repository in the directory using Git: $ git init

  8. 使用 Git 在目录中初始化一个新的仓库: $ git init

  9. Add all files in the directory to staging using Git: $ git add . Or specify a specific file to stage using Git. $ git add file.txt

  10. 使用 Git 将目录中的所有文件添加到暂存区: $ git add . 或者使用 Git 指定特定文件进行暂存。 $ git add file.txt

  11. Create a new commit using Git with all new or updated files: $ git commit -m "First Commit" -a

  12. 使用 Git 对所有新文件或更新后的文件进行提交: $ git commit -m "First Commit" -a

  13. Use the following command to commit specific files using Git: $ git commit -m "Initial Commit" file.txt

  14. 使用以下命令通过 Git 提交特定文件: $ git commit -m "Initial Commit" file.txt

  15. View the Git commit history: $ git log

  16. 查看 Git 提交历史: $ git log

  17. Check the repository status to test all changes, and untracked files in the current branch: $ git status

  18. 检查仓库状态,以测试当前分支中的所有更改和未跟踪的文件: $ git status

  19. Add a new remote repository and save it as origin to use with Git: $ git remote add origin <remote-repository-url>

  20. 添加一个新的远程仓库并将其保存为 origin 以供 Git 使用: $ git remote add origin <remote-repository-url>

  21. Verify all available remotes: $ git remote -v

  22. 验证所有可用的远程仓库: $ git remote -v

  23. Push all local changes to the remote repository: $ git push origin main

  24. 将所有本地更改推送到远程仓库: $ git push origin main

  25. Pull the latest changes from the remote repository: $ git pull origin main

  26. 从远程仓库拉取最新更改: $ git pull origin main

  27. View all branches in the active project directory: $ git branch -a

  28. 查看活动项目目录中的所有分支: $ git branch -a

  29. Create a new branch such as demo-branch using Git: $ git checkout -b demo-branch

  30. 使用 Git 创建一个新分支,例如 demo-branch$ git checkout -b demo-branch

  31. Switch to the branch: $ git checkout demo-branch

  32. 切换到该分支: $ git checkout demo-branch

  33. Check the active branch and verify that it’s demo-branch: $ git branch

  34. 检查活动分支并验证其是否为 demo-branch$ git branch

  35. Switch back to the master branch: $ git checkout master

  36. 切换回 master 分支: $ git checkout master