Get Started with Git on Oracle Linux

5
0
Send lab feedback

Get Started with Git on Oracle Linux

Introduction

Git is a free and open-source Distributed Version Control System (DVCS) available for MacOS, MS Windows, and Linux/Unix platforms. This tutorial covers using Git on Oracle Linux, but the steps covered in this tutorial work for any platform.

How does Git work? Git does not need a centrally located Git server to be fully functional. Instead, a simple project consists of a local folder on your computer containing files. Git then tracks any changes made to the directories and files contained within the project over time. It achieves this by storing the history and details of branches and commits locally without requiring a network connection.

The ability to manage your code history and track these changes over time is why it is called a version control system. Notice the absence of distributed? Being distributed requires a remotely hosted server to provide some of the more advanced features of a remotely hosted server such as GitHub.

Git is very powerful, with an extensive feature set that assists development teams in tracking changes. It is most efficient when used at the command line, which may appear to be a barrier for some users. The command line can appear confusing at times. Hopefully, this tutorial will help by providing an introduction showing how to install and configure Git and then start using it on Oracle Linux.

Note: This tutorial shows how to install and use Git locally. It does not cover using a remote repository, such as GitHub.

Objectives

In this tutorial, you'll learn how to:

  • Install Git
  • Create a Git repository
  • Use Git

Prerequisites

  • Minimum of a single Oracle Linux system

  • Each system should have Oracle Linux installed and configured with:

    • A non-root user account with sudo access
    • Access to the internet

Deploy Oracle Linux

Note: If running in your own tenancy, read the linux-virt-labs GitHub project README.md and complete the prerequisites before deploying the lab environment.

  1. Open a terminal on the Luna Desktop.

  2. Clone the linux-virt-labs GitHub project.

    git clone https://github.com/oracle-devrel/linux-virt-labs.git
  3. Change into the working directory.

    cd linux-virt-labs/ol
  4. Install the required collections.

    ansible-galaxy collection install -r requirements.yml
  5. Deploy the lab environment.

    ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6" -e update_all=true

    The free lab environment requires the extra variable local_python_interpreter, which sets ansible_python_interpreter for plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.

    The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add -e instance_shape="VM.Standard3.Flex" or -e os_version="9" to the deployment command.

    Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Linux is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.

Install Git on Oracle Linux

  1. Install Git.

    sudo dnf install git -y
  2. Confirm the installation of Git.

    git --version

Initial Setup

Now that you have Git installed, the first step to complete before using Git is to configure it with your name and email details. These details identify the author of any transaction.

Note: You should use your real name and email address. However, in the tutorial, use the fictional name provided.

  1. Set your username.

    git config --global user.name "Jane Doe"
  2. Set your email address.

    git config --global user.email "jane.doe@acmecorp.com"
  3. Confirm the settings for the username and email.

    git config --list

    Example Output:

    user.name=Jane Doe
    user.email=jane.doe@acmecorp.com

    Note: Using git config --list will show all of the settings that you have set. Currently, the user.name and user.email are the only values defined. An alternative way to show specific information, for example, your user.email details, is to run git config user.email.

  4. Configure an initial branch name for all new Git repositories you create.

    git config --global init.defaultBranch main

    Note: If you forget to set a global default name to use for the initial branch name, a reminder message will be displayed similar to this:

    [oracle@ol-node-01 ~]$ git init test-git
    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>

    At this point, Git is set up and ready for you to use. The next step is to create a Git repository on this machine.

Create a Local Git Repository

You can create a new local Git repository in any subdirectory you choose. Next, you will create a new subdirectory and initialize it as a Git repository.

  1. Create the new Git repository.

    mkdir ~/start-git
  2. Change into the directory.

    cd ~/start-git
  3. Check if this directory is currently a Git repository.

    git status

    Example Output

    [oracle@ol-node-01 start-git]$ git status
    fatal: not a git repository (or any of the parent directories): .git

    What happened? Why is this not recognized as a repository? Any new Git repository needs initializing. Next, you will initialize a new Git repository.

  4. Initialize the directory as a new Git Repository.

    git init

    Example Output:

    [oracle@ol-node-01 start-git]$ git init
    Initialized empty Git repository in /home/oracle/start-git/.git/

    Interesting Information: It is important to note that this newly created Git repository did not require any server either locally or remotely. Instead, the git init command makes a hidden .git directory in the directory where git init was executed. You can look at the contents of the hidden directory using the command: ls -lsa .git. It is beyond the scope of this tutorial, and Git best practices recommend that you do not manually alter anything in the .git directory. Doing so will most likely result in breaking your newly created repository.

  5. Confirm the status of the Git repository.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    
    No commits yet
    
    nothing to commit (create/copy files and use "git add" to track)

    The first line confirms that the global branch name previously set worked. The default branch used by the new Git repository is main (see: On branch main on the first line of output).

Create Your First Commit

The following steps demonstrate how to create a new file in a Git repository and then manage it.

  1. Create a simple text file.

    cat << 'EOF' > ~/start-git/file1.txt
    Hello World!!
    EOF
  2. Confirm the Git status again.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    
    No commits yet
    
    Untracked files:
     (use "git add <file>..." to include in what will be committed)
     file1.txt
    
    nothing added to commit but untracked files present (use "git add" to track)

    The status output confirms Git is aware of the new file. However, more steps are required to begin tracking changes to it.

Add a file to Your Git Repository

The last command's output suggested using gid add to allow Git to track the file. This section explains how to add the new file to your Git repository and to track it.

  1. Add the file to the Git repository.

    git add file1.txt

    There are several ways to add a file, or files, to a Git repository. If you only want to add a single file and not other untracked files in the same directory, you need to reference each file separately, as shown above. However, if you need to add several files to your repository, you could also use git add . to include all untracked, newly created, and modified files since the last time using git add. There is no right or wrong way to do this. Instead, choosing which to use is up to you.

  2. Check that Git is now tracking the file.

    git status

    Example Output

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
     new file:   file1.txt
    

    At this point, Git has confirmed it is aware of the file and is tracking any changes made. This step is known as staging. There is more nuance to using a staging area in Git, but covering it is beyond the scope of this tutorial. For more information, please look here

    Even after the staging is complete, the file's history and changes must still be committed or stored in the repository.

  3. Commit the file to the Git repository.

    git commit -m "This is my first commit"

    Where -m tells Git what message to store with the file(s) added to the repository. You should be concise because the maximum title length permitted is 72 characters. In practice, most teams prefer using less than 50 characters. While entering a very brief commit message may be tempting, as you will see later, it is also important to be as clear as possible.

    Example Output:

    [oracle@ol-node-01 start-git]$ git commit -m "This is my first commit"
    [main (root-commit) 00a7084] This is my first commit
     1 file changed, 1 insertion(+)
     create mode 100644 file1.txt

    Take a minute to review the Git message text.

    The first part of the first line - [main (root-commit) 00a7084]

    • The (root-commit) only occurs when, like this, it is the first commit to a brand new repository.
    • The 00a7084 is a unique transaction SHA1 ID that Git uses internally to identify the committed files. Every future commit will have a unique SHA1 ID.

    The last line - create mode 100644 file1.txt shows that Git has stored the file's permissions setting.

    At this point, you have completed your first Git commit. The workflow of using git add followed by the git commit -m command will become very familiar as you become used to using Git day-to-day. But what happened to the commit?

  4. Run a status check.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    nothing to commit, working tree clean

    Everything in the repository is clean, but there is no further detail. To see that another command is needed.

Review The Logs

Reviewing a repository's transaction history is often helpful. This section explains how to view these logs.

  1. Show the log information.

    git log

    Example output:

    [oracle@ol-node-01 start-git]$ git log
    commit 00a70845b2e686e820d8f28c7e1f91bee57d5b5e (HEAD -> main)
    Author: Jane Doe <jane.doe@acmecorp.com>
    Date:   Wed Oct 4 15:07:46 2023 +0000
    
        This is my first commit

    The most important information returned is the commit ID reference returned on the first line. The commit ID is a unique reference for every commit you create in a repository. Its importance will become more relevant as your knowledge of Git develops. The rest of the information returned describes what changes occurred and who did it. Notice that the Author field returns the user.name and user.email values you entered when setting up the repository. Likewise, the last line returned matches the commit message you entered after the -m flag in the git commit command.

    The git log command returns every commit made in the repository and, therefore, can be very long. If this happens to your repository, you can paginate the output by adding the -p paginate flag to the command. The output returned uses the Linux less command, which you can navigate using the spacebar to advance one page at a time and the up and down arrow keys to move up or down one line at a time).

    However, the default output does not show which files we modified as part of the commit.

  2. Check which files the commit includes.

    git log --stat

    Example Output:

    [oracle@ol-node-01 start-git]$ git log --stat
    commit 00a70845b2e686e820d8f28c7e1f91bee57d5b5e (HEAD -> main)
    Author: Jane Doe <jane.doe@acmecorp.com>
    Date:   Wed Oct 4 15:07:46 2023 +0000
    
     This is my first commit
    
     file1.txt | 1 +
     1 file changed, 1 insertion(+)

    The output indicates that we added and modified one file during the commit transaction.

  3. List all of the files in a Git repository.

    Because you now know that simply listing all of the files in an Oracle Linux directory does not guarantee that Git is also tracking any changes made to them, Git also provides a way to confirm which files it is tracking.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    file1.txt

    The output shows only one file, which we expect. However, a repository with only a single commit is not realistic. So, let's add another couple of commits to the repository to register some changes and then look at the logs again.

Add More Content to the Repository

Add a New File

  1. Create a new file containing some text.

    cat << 'EOF' > ~/start-git/file2.txt
    Hello again!!
    EOF
  2. Test staging the new file.

    Sometimes, you may need clarification from Git regarding what file or files you recently modified but have not committed to the repository. To overcome this, Git provides the --dry-run flag, which will return details of what you need to add to the staging area.

    git add . --dry-run

    Example Output:

    [oracle@ol-node-01 start-git]$ git add --dry-run .
    add 'file2.txt'

    Nothing unexpected is listed.

  3. Confirm the need to add the new file to the staging area.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
     file2.txt

    Notice the output confirms that the new file is still untracked.

  4. Start tracking the new file.

    git add .
  5. Commit the new file to the repository.

    git commit -m "Add a second text file"

    Example Output

    [oracle@ol-node-01 start-git]$ git commit -m "Added a second text file"
    [main 3f7975a] Added a second text file
     1 file changed, 1 insertion(+)
     create mode 100644 second-commit.txt

Update an Existing File

  1. Update the the first file.

    echo "Welcome to Git." >> file1.txt
  2. Confirm the new text within the first file.

    cat file1.txt

    Example Output:

    [oracle@ol-node-01 start-git]$ cat file1.txt 
    Hello World!!
    Welcome to Git.
  3. Check if Git notices the change to the file.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
     modified:   file1.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    The output confirms that Git knows about the file updates. What happens if you need help remembering what was changed?

  4. Get a list of the last changes made to the file.

    git diff

    Example Output:

    [oracle@ol-node-01 start-git]$ git diff
    diff --git a/file1.txt b/file1.txt
    index 68115de..2fca927 100644
    --- a/file1.txt
    +++ b/file1.txt
    @@ -1 +1,2 @@
     Hello World!!
    +Welcome to Git.

    Git uses a + to indicate lines added and a - for lines removed.

  5. Add and commit the change to the repository.

    Because you know what changed and the file already exists in the repository, you can use a shortcut that combines these steps into a single command using the -a and -m flags.

    git commit -a -m "Add a new line to file1.txt"

    Example Output:

    [oracle@ol-node-01 start-git]$ git commit -a -m "Add a new line to file1.txt"
    [main b37385a] Add a new line to the file1.txt
     1 file changed, 1 insertion(+)
  6. Confirm no further changes are outstanding.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    nothing to commit, working tree clean

Recheck the Commit History

  1. Review the logs again.

    git log

    Example Output:

    [oracle@ol-node-01 start-git]$ git log
    commit b37385a9804684bab9bc45adab1f149b062180a9 (HEAD -> main)
    Author: Jane Doe <jane.doe@acmecorp.com>
    Date:   Tue Oct 10 14:53:31 2023 +0000
    
        Add a new line to file1.txt
    
    commit 3f7975a29652a602036ac3b5e0cf6745573c61d8
    Author: Jane Doe <jane.doe@acmecorp.com>
    Date:   Tue Oct 10 14:04:05 2023 +0000
    
        Add a second text file
    
    commit 9fc5789ba9596c33d025c37bbb0224a9b55be104
    Author: Jane Doe <jane.doe@acmecorp.com>
    Date:   Tue Oct 10 11:01:16 2023 +0000
    
        This is my first commit

    The most recent update is at the top of the list. The (HEAD -> main) tag points to the latest commit of the currently checked-out branch.

    The --oneline flag can show a consolidated output.

    Example Output:

    [oracle@ol-node-01 start-git]$ git log --oneline
    b37385a (HEAD -> main) Add a new line to file1.txt
    3f7975a Added a second text file
    9fc5789 This is my first commit

    The listing shows each unique Git commit ID and its associated commit message. It also highlights why it is important to use meaningful commit messages - they make identifying a specific transaction easier.

Rename and Delete Files

What happens if you need to rename or delete files? The steps are the same, whether this is intentional or due to an error.

  1. Create a new file.

    cat << 'EOF' > ~/start-git/feather.txt
    This file contains a new feature.
    EOF
  2. Add and commit the file to the repository.

    git add .
    git commit -m "Add a new test file for rename and delete"
  3. Confirm the new file exists as part of the repository.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    feather.txt
    file1.txt
    file2.txt

    You notice the last file added has the wrong name, and you need to rename it.

  4. Confirm which files are in the project's directory.

    ls

    Example Output:

    [oracle@ol-node-01 start-git]$ ls
    feather.txt  file1.txt  file2.txt
  5. Rename the File.

    mv feather.txt feature.txt
  6. Check the repository status.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            deleted:    feather.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            feature.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    Git is aware that something has happened. In this instance, it thinks that you removed the feather.txt file and then created the file feature.txt, which, if you think about it, is what happened. The Git output actually tells you how to rectify the issue.

  7. Remove the incorrectly named file from the Git repository.

    git rm feather.txt

    Example Output:

    [oracle@ol-node-01 start-git]$ git rm feather.txt
    rm 'feather.txt'
  8. Add the renamed file to the repository.

    git add feature.txt
  9. Check the status.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    Changes to be committed:
     (use "git restore --staged <file>..." to unstage)
     renamed:    feather.txt -> feature.txt

    The output confirms that Git recognizes the file change. Git provides a shorter way to rename a file using a single command. But first, you need to commit your changes to the repository.

  10. Commit the change.

    git commit -m "Add feature.txt"
  11. Confirm the working tree is clean.

    git status

    Now, you will rename a file, but this time using a single command.

  12. Rename the file again.

    git mv feature.txt deleteme.txt
  13. List the files in the repository.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    deleteme.txt
    file1.txt
    file2.txt
  14. Commit the change.

    git commit -m "Rename feature.txt to deleteme.txt"
  15. Confirm again the working tree is clean.

    git status

    When renaming files in a Git repository, it's best practice to use the Git commands rather than the equivalent Linux commands.

Delete a File

The same principle applies when deleting a file. You could use the Linux rm command to delete a file. However, that would also entail the same lengthy Git workflow you encountered when renaming a file using the Linux mv command.

  1. Remove the file you no longer need.

    git rm deleteme.txt
  2. Check the status.

    git status

    The output shows the file removed in staging.

  3. Commit the change to the repository.

    git commit -m "Remove deleteme.txt"

    The output shows the commit ID and the file removed.

  4. Confirm again the working tree is clean.

    git status
  5. Ensure the repository no longer includes the file.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    file1.txt
    file2.txt

    You can also confirm that the file no longer exists in the local project directory using the Linux' ls' command.

    Completing these actions confirms that using the git rm command removes the file from both the Git repository and the Oracle Linux filesystem.

Next Steps

Thank you for completing this tutorial. Hopefully, you better understand Git and how to manage and track source code changes using a local Git repository.

SSR