Setting Up and Example Git Command on FreeBSD

If you're used to using FreeBSD, chances are you've heard of git, or you may be wondering, what exactly is git and how do I use it? . Git is the brainchild of Linus Torvalds, who developed it as a source code management system while working on the Linux kernel.

It has since been adopted by many projects and software developers due to its track record of speed and efficiency along with its ease of use. Git is also gaining popularity among writers of all types, as it can be used to track changes to any set of files, not just code.

In general, git is a free, open source distributed version control system tool designed to handle small to very large projects with maximum speed and efficiency. Git continues to evolve from being a preferred skill to a must-have skill for many of today's job roles. Nowadays Git has become an important part of the daily application development process. In the tutorial we will briefly explain how to use git commands on a FreeBSD 13.2 machine.


List of contents:
1. Installing git on FreeBSD
2. Configure git
3. Create a New Project with Git
4. Git Log Files
5. Examples of git Commands



1. Installing git on FreeBSD

Installing git on a FreeBSD machine is very easy. The installation process can use the ports system, pkg package or directly from the git repository on Github.

a. Git installation with system ports

root@ns1:~ # cd /usr/ports/devel/git
root@ns1:/usr/ports/devel/git # make install clean BATCH="yes"

b. Git Installation With pkg Package

root@ns1:~ # pkg update
root@ns1:~ # pkg upgrade
root@ns1:~ # pkg install git



2. Configure git on FreeBSD

Once git is installed on our FreeBSD system, continue with configuring git. To use git, we just need to get some basic configuration. The first thing you have to do is configure your email and username in git. Note that this is not used to log into any services, git is only used to document what changes will be made when recording commits.

The main requirement for configuring email and username, you must already have an account on Github, if not, create a Github account now. This article will not explain how to create a GitHub account. You can read other articles that explain how to create a GitHub account.

OK, now let's just go ahead and configure email and username in Git. In the example below I already have an account on Github with User: iwanse1977 and Email: datainchi@gmail.com. Type the script below to configure users and emails.

root@ns1:~ # git config --global user.email "datainchi@gmail.com"
root@ns1:~ # git config --global user.name "iwanse1977"

To see your username and email verified, enter the following:

root@ns1:~ # git config -l
user.email=datainchi@gmail.com
user.name=iwanse1977


3. Creating a New Project with Git

To set up a new project with git, it must be initialized first using the following command:

root@ns1:~ # git init templateblog
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /root/templateblog/.git/

The script above will create a project with the name "templateblog" and save it in the root directory, you can change the directory, for example you place the new project in the /usr/local/etc folder.

Use the ls command to view the project that was created above.

root@ns1:~ # ls
-s		.gitconfig	.local		.shrc		templateblog
.bash_history	.history	.login		.ssh
.cshrc		.k5login	.profile	.wget-hsts

Try looking at the contents of the root folder above, a Git project has been formed, namely the .gitconfig file (hidden) and the templateblog folder. When working with git, you should make the newly created project folder the working directory. Now we will create a helloworld.c file in the templateblog working directory.

root@ns1:~ # cd templateblog
root@ns1:~/templateblog # touch helloworld.c
root@ns1:~/templateblog # git add .
root@ns1:~/templateblog # git commit -m "First commit of project, just an empty file"

The script above will create a file called helloworld.c. After creating the helloworld.c file, enter the following script into the file.

root@ns1:~/templateblog # ee helloworld.c
#include
int main(void)
{
    printf("Hello, World!\n");
    return 0;
}

Continue with the following script.

root@ns1:~/templateblog # git add helloworld.c
root@ns1:~/templateblog # git commit -m "added source code to helloworld.c"


4. Git Log Files

After the git project is created, you can view the log file of the project, use the following script to view the project log file.

root@ns1:~/templateblog # git log
commit 5256dbb6f0ab83de90c8756a2fdceb1afc84243d (HEAD -> master)
Author: iwanse1977 <datainchi@gmail.com>
Date:   Tue Aug 1 14:37:49 2023 +0700

    First commit of project, just an empty file

commit b69becc771d9a48c6ab1dfeea4479ca7fa4f0d0c
Author: iwanse1977 <datainchi@gmail.com>
Date:   Tue Aug 1 14:37:08 2023 +0700

    First commit of project, just an empty file

From the script above you can see that each commit is regulated by a SHA-1 hash ID, Author's name and email as well as date and comments. We will also see that the latest commits are referred to as HEAD in the output. HEAD is our current position in the Git project. Now let's look at the contents of the SHA-1 hash ID, use the following script to open the contents of the SHA-1 hash ID.

root@ns1:~/templateblog # git show 5256dbb6f0ab83de90c8756a2fdceb1afc84243d
commit 5256dbb6f0ab83de90c8756a2fdceb1afc84243d (HEAD -> master)
Author: iwanse1977 <datainchi@gmail.com>
Date:   Tue Aug 1 14:37:49 2023 +0700

    First commit of project, just an empty file

diff --git a/helloworld.c b/helloworld.c
index e69de29..32870e4 100644
--- a/helloworld.c
+++ b/helloworld.c
@@ -0,0 +1,6 @@
+#include 
+int main(void)
+{
+    printf("Hello, World!\n");
+    return 0;
+}



5. GIT for FreeBSD Ports Update

The git command can also be used to update FreeBSD ports. You can download FreeBSD ports on the website "https://cgit.freebsd.org/ports". Use the git command below to download and update FreeBSD ports.

root@ns1:~ # git clone -b release/13.2.0 --depth 1 https://git.FreeBSD.org/ports.git /usr/ports

Next, you run the commands git pull, git branch and git config.

root@ns1:~ # cd /usr/ports
root@ns1:/usr/ports # git pull && git branch --all
root@ns1:/usr/ports # git config --global --add safe.directory /usr/ports

In this tutorial we will not discuss how to update ports. To carry out the ports update process, you can read our previous article.




6. Examples of git Commands

Below we will provide several examples of basic Git commands.

a. git status
This command is used to display the modification status of existing files and the status of adding new files.

root@ns1:~/templateblog # git status
On branch master
nothing to commit, working tree clean

b. git remote
This command is used to connect your local repository to a remote server.

root@ns1:~/templateblog # git remote add origin https://github.com/git/git.git

c. git push
This command is to push changes made from the master branch to the remote repository.

root@ns1:~/templateblog # git push origin master
Username for 'https://github.com': 
Password for 'https://iwanse1977@github.com': 
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.


d. git clone
This command is used to create a copy of a repository from an existing URL. So if you want to copy the repository from the remote server to the local computer, use this command.

root@ns1:~ # git clone https://github.com/git/git.git
Cloning into 'git'...
remote: Enumerating objects: 352259, done.
remote: Counting objects: 100% (924/924), done.
remote: Compressing objects: 100% (442/442), done. 

e. git branch
This command lists all available branches in the repository.

root@ns1:~/templateblog # git branch -a 
* master

f. git checkout
This command is used to move from one branch to another.

root@ns1:~/templateblog # git checkout master
Already on 'master'

g. git stash
This command is to temporarily save all modified tracked files.

root@ns1:~/templateblog # git stash save

h. git revert
The git revert command is used to offer a safe way to revert changes in Git.

root@ns1:~/templateblog # git revert --continue

i. git diff
The git diff command compares changes made to files in the working directory with those in the staging area. If you have updated a file on your computer but are unsure of the changes you made, use this command to highlight the changes to the file, as shown below.

root@ns1:~/templateblog # git diff helloworld.c

j. git merge
This command merges the specified branch history into the current branch.

root@ns1:~/templateblog # git merge

k. git rebase
The git rebase command applies changes from one branch to another.

root@ns1:~/templateblog # git rebase master
Current branch master is up to date.

Now you have basic knowledge of how to install git, configure it, and use it to work with local repositories and remote servers. You can expand your git knowledge by joining the growing git community that leverages the power and efficiency of git as a distributed revision control system.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post