Using Git version control on Ubuntu

Version control systems (VCS) track and manage code changes, enabling teams to collaborate efficiently, revert to previous versions, and maintain a history of modifications. Git is the most widely used version control system; however, others including Concurrent Version System (CVS), Apache Subversion (SVN), Mercurial, and Bazaar are also available for those who require them for specific use cases.

This article focuses on Git – the de facto standard in software development.

Git

Git is a decentralized source-code management system that provides for efficient branch use, ability to perform complex merges, and archive integrity. To read more about the system, see its official website at Git SCM.

Tip

Git documentation can also be installed for local viewing in a web browser.

To get it:

sudo apt install -y git-doc

To view it using Firefox:

firefox /usr/share/doc/git-doc/index.html

Getting Git

To install Git on Ubuntu Desktop, use the regular package management system (package git). If your specific use case requires it, there is also a snap package, git-scm.

To install the DEB package, run:

sudo apt install -y git

To install the snap package, run:

sudo snap install git-scm

Configuring Git

Initial Git configuration is described in detail in official documentation at First-Time Git Setup.

When using Git for both personal and professional development, consider conditioning some settings based on repository properties. For example, to configure multiple email addresses for commit logs depending a given repository, use one or more keywords with the includeIf variable.

Example: conditional include based on a local directory

  1. Add the following variable definition to the ~/.gitconfig configuration file:

    ~/.gitconfig
    ; configuration file to use for repositories under $HOME/git/example-com/
    [includeIf "gitdir:~/git/example-com/"]
        path = ~/.gitconfig_example
    
  2. Provide specific configuration in the file included above:

    ~/.gitconfig_example
    ; configuration differing from global settings
    [user]
        email = [email protected]
    

See the reference documentation for the git config command (including examples of configuration files) at git-config.

Git and hosting providers

To collaborate with others using Git, connect your local Git instance to a remote Git-hosting provider. This can be a Git server instance provided by your organization, or one of the publicly available Git-hosting services, such as GitLab, GitHub, or Launchpad.

A commonly used method of facilitating secure repository authentication is based on SSH keys. See SSH Key Authentication for instructions on how to work with SSH keys on Ubuntu.

To generate and use an SSH key pair:

  1. Install the openssh-client package:

    sudo apt install -y openssh-client
    
  2. Run the ssh-keygen command:

    ssh-keygen -t ed25519 -C "[email protected]"
    
  3. Add the generated public key to your Git provider.

    For information on how to add an SSH key to various Git-hosting providers, see:

Launchpad considerations

Launchpad is suite of tools for collaborating on software projects. Among other functions, it provides:

  • Backend services for processes critical to the development of the Ubuntu Linux distribution.

  • Git-hosting service for teams and individuals.

To participate in the development and maintenance of the Ubuntu Linux distribution, Launchpad is essential. See the official documentation at Launchpad Help.

Suggestions for using Git

Below are some best practices you can consider when using Git:

Writing meaningful commit messages

Ensure that your commit messages are meaningful and consistent to provide context to the changes your commit introduces. Consider adopting a common standard, such as Conventional Commits.

Using Git aliases

Git aliases allow you to configure shorthand alternatives to longer Git commands. For example, if you set ci as the alias for the commit command, running git ci would perform the same function as running git commit. For more information on Git aliases, see Git documentation: Git aliases.

Using .gitignore files

Not all files need to be tracked by version control. You can use a .gitignore file to specify files you want Git to ignore. Find a list of standard .gitignore templates in GitHub gitignore repository.

Pulling before pushing

Always pull the latest changes from a remote repository before pushing to avoid conflicts and keep your local branch up to date. For an overview of recommended workflows to use with Git, see the gitworkflows(7) manual page, and especially its DISTRIBUTED WORKFLOWS section (available locally through git help workflows).

Useful tooling for Git

For a more productive development experience with Git, consider these tools:

Git shell helpers

Using a Git shell helper, such as the bundled git-prompt.sh, can improve your Git workflow with features including auto-completion and branch names with repository status displayed as part of the command prompt in the terminal. To set it up, read the instructions in the /usr/lib/git-core/git-sh-prompt file. See also Git in Other Environments - Git in Bash.

Text user interfaces (TUI)

Git TUIs, such as Tig, provide a visual way to interact with Git in the terminal, making it easier to navigate commits and branches. Install with sudo apt install -y tig.

Graphical user interfaces (GUI)

Git GUIs, such as the basic gitk or GNOME-native gitg, provide a graphical interface to manage repositories instead of using the command line. Install with sudo apt install -y gitk gitg. See GUI Clients for an overview of available applications (includes TUIs).

Git hooks

Git hooks are scripts that trigger actions at specific Git events (e.g., before a commit, after a push). This enables you to enforce coding standards, prevent commits with errors or unformatted code, and automate tasks such as linting, formatting, and commit-message validation. See Customizing Git - Git Hooks.

Additional resources