Git is a popular version control system that allows developers to manage their code repositories efficiently. If you are a developer using FreeBSD, you will need to set up Git and an authentication method to start collaborating on projects with other developers. In this post, we will walk you through the process of installing Git, creating SSH keys, and cloning a project from GitHub on FreeBSD.
Does this work in Linux or Mac? Yes, it does. You just need to install git following your operating system’s instructions, if it’s not already installed.
Switch to the root user and install git:
su - pkg install git
===== Message from git-2.39.2: -- If you installed the GITWEB option please follow these instructions: In the directory /usr/local/share/examples/git/gitweb you can find all files to make gitweb work as a public repository on the web. All you have to do to make gitweb work is: 1) Please be sure you're able to execute CGI scripts in /usr/local/share/examples/git/gitweb. 2) Set the GITWEB_CONFIG variable in your webserver's config to /usr/local/etc/git/gitweb.conf. This variable is passed to gitweb.cgi. 3) Restart server. If you installed the CONTRIB option please note that the scripts are installed in /usr/local/share/git-core/contrib. Some of them require other ports to be installed (perl, python, etc), which you may need to install manually.
The installation is complete, and we are ready to create a key-pair to use to authenticate against GitHub.
End your root user session and go back to your regular user prompt.
Creating a key-pair is a simple process and is accomplished by typing the following command:
ssh-keygen -t ed25519 -C "firstname.lastname@example.org"
Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_ed25519 Your public key has been saved in /home/user/.ssh/id_ed25519.pub
It is ok to leave the passphrase empty, but it is important that you understand the implications.
The key-pair process creates a set of files, a public key, and a private key. They are stored in your
~/.ssh directory and named
id_ed25519 for the private one, and
id_ed25519.pub for the public one. Be very careful with these files and treat them like you treat passwords.
GitHub allows you to use SSH keys to authenticate. They can be added on the settings/keys page. You can navigate to it by going to Settings, and then selecting SSH and GPG keys. To add a new ssh key:
- Press the New SSH key button, then
- Enter a description in the Title field, then
- Copy the contents of the generated public key (the
id_ed25519.pubfile you just created)
And paste it into the Key field in GitHub:
Press the Add SSH key button to finish.
You should now see your SSH key added to GitHub:
Once you are done setting up the keys, we are ready to clone our first project. On GitHub, go to your repositories and select a private project, then click on Code and select SSH. Copy the url that shows up.
Type the following command using the URL you just copied:
git clone email@example.com:user/project.git
Cloning into 'project'... remote: Enumerating objects: 115, done. remote: Counting objects: 100% (115/115), done. remote: Compressing objects: 100% (57/57), done. remote: Total 115 (delta 29), reused 111 (delta 25), pack-reused 0 Receiving objects: 100% (115/115), 22.68 KiB | 7.56 MiB/s, done. Resolving deltas: 100% (29/29), done.
Voila! The project should be successfully cloned using the keys that we created previously to authenticate against GitHub.
Git is an essential tool for any developer who wants to collaborate on code repositories with other developers. By following the steps outlined in this post, you’ll be able to install Git, create SSH keys, and clone a project from GitHub on FreeBSD with ease. With these steps, you’ll be able to join a project and start collaborating with other developers in no time.
Skip this step if you are not using a Mac.
If you are using a Mac, you have to add the generated key to the ssh-agent.
Testing your connection
If the connection is failing, you can try the following:
Try to connect to github:
ssh -T firstname.lastname@example.org
yes to the following prompt:
> The authenticity of host 'github.com (IP ADDRESS)' can't be established. > ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. > Are you sure you want to continue connecting (yes/no)?
Verify that the following message shows up:
> Hi USERNAME! You've successfully authenticated, but GitHub does not > provide shell access.
If you are trying to
git pull an existing project and git insists on using your username and password, then:
- Open the file
.git/configin the project’s directory.
- Look for the
[remote "origin"]section and verify that the
urlis set to use ssh. For example:
url = email@example.com:some-account/some-project.git
git will not work if the url is using the
https protocol. If this is the case, get the ssh url from github and replace the value in this file.
- GitHub. (n.d.). “Generating a new SSH key and adding it to the ssh-agent.” Retrieved March 18, 2023, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- GitHub. (n.d.). “Testing your SSH connection.” Retrieved May 1, 2023, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection
- GitHub. (n.d.). “Adding your SSH key to the ssh-agent.” Retrieved May 1, 2023, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent
- Microsoft - Azure DevOps Services. (2022, October 4). “SSH passphrases.” https://learn.microsoft.com/en-us/azure/devops/repos/git/gcm-ssh-passphrase?view=azure-devops
- static_rtti. (2020, January 28). “Git keeps prompting me for a password.” Stack Overflow. https://stackoverflow.com/a/7773605