A Complete Guide to Installing and Using Git on Ubuntu: Setup, Commands, and Troubleshooting

1. What Is Git? Why Install Git on Ubuntu?

The Basics of Git

Git is a distributed version control system designed to enable multiple developers to work efficiently on software projects at the same time. It allows you to manage projects both remotely and locally while keeping a complete history of all changes.

Using Git on Ubuntu

Ubuntu is an excellent open-source development environment and is widely compatible with Git, which makes it a particularly developer-friendly platform.

2. Preparation Before Installing Git

Updating the System

Update the system to the latest state using the following commands:

sudo apt update
sudo apt upgrade

Checking sudo Privileges

sudo privileges are required. Use the whoami command to confirm that your user account has administrator rights.

3. Installing Git via Ubuntu’s Official Repository

sudo apt install git

You may also install the latest Git version using a PPA. Execute the following commands:

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

4. Initial Git Configuration

Configuring Username and Email Address

Before using Git, you must configure the username and email address that will appear in your commit history.

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

5. How to Clone a Git Repository

To clone a remote repository, use the following command:

git clone https://github.com/example/repo.git

6. Troubleshooting Authentication, Permission, and Conflict Issues

Authentication Errors When Using git push

Since 2021, services like GitHub have deprecated password-based authentication. Personal Access Tokens (PAT) or SSH authentication are now recommended. If you encounter authentication errors with git push, follow these steps:

  1. Create a PAT: Generate a Personal Access Token from your GitHub settings page and use it as the password during your next git push.
  2. Check SSH Keys: If using SSH authentication, confirm and add your SSH key with the following command:
ssh-add ~/.ssh/id_rsa

Permission Errors During git push

If you encounter a “Permission denied” error during git push, you may not have push permissions for the remote repository. To resolve this issue, perform the following steps:

  1. Check the remote configuration:
git remote -v
git remote show origin
  1. Verify push permissions: Confirm that you have push rights for the remote repository. If not, request push access from the repository owner.
  2. Update the remote URL: Configure the correct remote URL:
git remote set-url origin <new-URL>

Remote Branch Errors During git pull

If you see an error stating that the remote branch cannot be found when running git pull, address it as follows:

  1. Check remote branches:
git branch -r
  1. Set tracking for the branch:
git branch --set-upstream-to=origin/<branch> <branch>
  1. Fetch remote changes: Use git fetch to retrieve remote updates. If a remote branch has been deleted, errors may occur during git pull.

7. How to Create a Pull Request on GitHub

The steps to create a pull request on GitHub are: create a branch, commit your changes, push the branch to the remote repository, and then create a pull request from GitHub’s interface.

8. Summary and Next Steps

Once you are familiar with basic Git commands and troubleshooting techniques, explore more advanced features such as git rebase and Git Flow to further enhance your development workflow.

侍エンジニア塾