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) which is available for MacOS, MS Windows and Linux/Unix platforms. This lab only covers using Git on Oracle Linux, but the steps covered in this lab 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 a large feature set that assists development teams in tracking those changes. Git is most efficient when used at the command line - this may appear to be a barrier for some users. The command line can appear confusing at times, hopefully, this lab helps by providing an introduction showing how to install, configure and then start using Git on Oracle Linux.

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

Objectives

In this lab, you'll learn:

  • What Git is
  • How to install Git
  • How to create a Git repository
  • The basic commands to use with Git

What Do You Need?

  • An Oracle Linux system.

Setup Lab Environment

Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.

  1. Open a terminal and connect via SSH to the ol-node-01 instance if not already connected.

    ssh oracle@<ip_address_of_instance>

    Important: ALL steps are completed the ol-node-01 terminal.

Install Git on Oracle Linux

  1. Install Git.

    sudo dnf install git -y
  2. Confirm that Git is installed.

    git --version

    Example Output:

    [oracle@ol-node-01 ~]$ git --version
    git version 2.39.3

Initial Setup

Now Git is installed, the first step to complete before using Git is to configure Git 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 lab use the fictional name provided.

  1. Setup a User Name.

    git config --global user.name "Jane Doe"
  2. Set up an Email Address.

    git config --global user.email "jane.doe@acmecorp.com"
  3. Confirm the Username and Email are set.

    git config --list

    Example Output:

    [oracle@ol-node-01 ~]$ git config --list
    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 the specific information, for example, your user.email details is to run git config user.email.

  4. Configure the initial branch name to be used 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. Ensure you are in your $HOME directory.

    Note: You can create a Git repository in any directory if using your system.

    cd ~
  2. Create the new Git repository.

    mkdir start-git
  3. Change into the directory.

    cd start-git
  4. Check if this is a Git repository now.

    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 being a repository? Because any new Git repository needs initializing. Next, you will initialize a new Git repository.

  5. Initialize the 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 creates a hidden directory called .git 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 lab, and highly recommended that you do not alter anything in the .git directory. Doing so will most likely result in breaking your newly created repository.

  6. Confirm the Git status.

    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)

    Note: Notice that 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).

Your first Git 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/first-commit.txt
    Your first file is managed by Git.
    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)
    	first-commit.txt
    
    nothing added to commit but untracked files present (use "git add" to track)

    This 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 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. The command used to do this is git add.

  1. Add the new file to the Git repository.

    git add first-commit.txt

    Note: There are several ways to add a file, or files, to a Git repository. If you only want to add a single file at a time, you need to reference each file separately as shown above. However, if several files need to be added to your repository, you could also use git add . to include all new files created since the last git add was used. There is no 'right', or 'wrong' way to do this, instead the choice of 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:   first-commit.txt
    

    At this point, Git has confirmed it is aware of the file and is tracking any changes made. However, it has not yet committed (stored) the history/timeline associated with the file in the repository.

    Information: This stage is known as Staging a file, or files. There is more nuance to using a Staging area in Git but covering it is beyond the remit of this lab. For more information, please look here

  3. Commit the new 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 a total of 72 characters. In practice, most teams prefer less than 50 characters to be used. While it may be tempting to enter a very brief commit message, 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 first-commit.txt

    Explanation: 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, this is the first commit to a brand new repository.
    • The 00a7084 is a unique transaction SHA1 ID which Git uses internally to identify what was committed. Every future commit will have a unique SHA1 ID.

    The last line (create mode 100644 first-commit.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. Does git status command show anything?

    git status

    Example Output:

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

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

Review The Logs

It is often helpful to review the transaction history of a repository. This section explains how to view the update history for a Git repository, using the git log command.

  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. It's 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 paginate flag like this: git log -p. The output returned uses the Linux less command and should be navigated in the same way as when using the less command (use the spacebar to advance one page at a time, and the up and down arrow keys to move up/down one line at a time).

    However the details of what actual file, or files that were updated are missing. The next command shows how to return that detail.

  2. Check what files were included in a commit.

    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
    
     first-commit.txt | 1 +
     1 file changed, 1 insertion(+)

    This shows the file, or files, modified as part of a 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 is no guarantee that Git is also tracking any changes made to them, Git provides a way to confirm the tracked files - the git ls-files command.

    git ls-files

    Example Output:

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

    Only one file returns, as expected. However, there has only been one commit, which is not realistic. So let's add another couple of commits to the repository to register some changes and then look at the git log output just covered again.

Add More Content to the Repository

Add a New File

  1. Create a new file and add some text.

    cat << 'EOF' > ~/start-git/second-commit.txt
    The second file is managed by Git.
    EOF
  2. Sometimes you may be unsure exactly what file or files have been modified. To overcome this, Git provides the --dry-run flag that will return details of what needs to be added to the staging area.

    git add . --dry-run

    Example Output:

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

    Nothing unexpected is listed.

  3. Confirm the new file needs to be added 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)
    	second-commit.txt

    Notice the output confirms that the new file (second-commit.txt) is still untracked.

  4. Track the new file.

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

    git commit -m "Added 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 first-commit.txt file.

    echo "New text added" >> first-commit.txt
  2. Confirm the file was updated.

    cat first-commit.txt

    Example Output:

    [oracle@ol-node-01 start-git]$ cat first-commit.txt 
    Your first file is managed by Git.
    New text added
  3. Does Git notice the update?

    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:   first-commit.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    This confirms Git knows about the updated file. What happens if you can't remember what was changed? The git diff command will remind you of all changes since the last commit.

  4. Confirm what changed in the first-commit.txt file.

    git diff

    Example Output:

    [oracle@ol-node-01 start-git]$ git diff
    diff --git a/first-commit.txt b/first-commit.txt
    index 68115de..2fca927 100644
    --- a/first-commit.txt
    +++ b/first-commit.txt
    @@ -1 +1,2 @@
     Your first file is managed by Git.
    +New text added

    Changes are prefixed with a + symbol. In this example, a new line was added to the file by this output: +New text added

  5. Next add the change and then commit it to the repository. Because you know what changed, you can use a shortcut that combines the -a and -m flags.

    git commit -a -m "Add a new line to the first-commit.txt file"

    Example Output:

    [oracle@ol-node-01 start-git]$ git commit -a -m "Add a new line to the first-commit.txt file"
    [main b37385a] Add a new line to the first-commit.txt file
     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

Review the Git Log Again

  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 the first-commit.txt file
    
    commit 3f7975a29652a602036ac3b5e0cf6745573c61d8
    Author: Jane Doe <jane.doe@acmecorp.com>
    Date:   Tue Oct 10 14:04:05 2023 +0000
    
        Added 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

    Note: The most recent update is listed at the top, and older updates are listed below. The (HEAD -> main) tag also confirms that we are currently on the main branch.

  2. An alternative way to display the git log output more concisely.

    git log --oneline

    Example Output:

    [oracle@ol-node-01 start-git]$ git log --oneline
    b37385a (HEAD -> main) Add a new line to the first-commit.txt file
    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.

How to Rename or Delete a File added in Error

What happens if you make an error? The following steps show how to remove a file added in error:

  • Create a new file, then add and commit it to the repository.
  • Next, rename the file.
  • Delete the file.

So let's get started.

  1. Create a new file.

    cat << 'EOF' > ~/start-git/test-file.txt
    A test file.
    EOF
  2. Add the file to the repository.

    git add .
  3. Commit the change to the repository.

    git commit -m "Add the test file"
  4. Confirm the new file (test-file.txt) is part of the repository.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    first-commit.txt
    second-commit.txt
    test-file.txt

How to Rename a File

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

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

    ls

    Example Output:

    [oracle@ol-node-01 start-git]$ ls
    first-commit.txt  second-commit.txt  test-file.txt
  2. Rename the File.

    mv test-file.txt changed-feature.txt
  3. Check the Git 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:    test-file.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
         	 changed-feature.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    Notice that Git is aware that something has happened. In this instance, it thinks that the test-file.txt was deleted and a new file (changed-feature.txt) added to the directory. Which, if you think about it is what happened. The Git output actually tells you how to rectify the issue.

  4. Remove the test-file.txt file from the Git repository.

    git rm test-file.txt

    Example Output:

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

    git add changed-feature.txt
  6. 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:    test-file.txt -> changed-feature.txt

    This confirms Git recognizes the file has changed. Git provides a shorter way to rename a file. The next step shows how to accomplish this in a single command. But first you need to commit the change.

  7. Commit the change.

    git commit -m "Add changed-feature.txt"
  8. Check the working tree is 'clean'.

    git status

    Example Output:

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

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

  9. Rename the file again.

    git mv changed-feature.txt test-file.txt
  10. List the files in the repository.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    first-commit.txt
    second-commit.txt
    test-file.txt
  11. Commit the change.

    git commit -m "Rename changed-feature.txt to test-file.txt"
  12. Check the working tree is 'clean'.

    git status

    Example Output:

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

    So you see that you need to use the git mv command instead of the Linux mv command to rename a file.

How to 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 as shown below:

  • rm testfile.txt (removes the file from the staging area)
  • git rm test-file.txt (removes the file from the Git repository)

Instead, use the git rm command.

  1. Use a single command to delete a file from the staging area and repository..

    git rm test-file.txt

    Example Output:

    [oracle@ol-node-01 start-git]$ git rm test-file.txt
    rm 'test-file.txt'
  2. 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)
    	deleted:    test-file.txt
  3. Remove the file from the repository using git commit.

    git commit -m "Delete test-file.txt"

    Example Output:

    [oracle@ol-node-01 start-git]$ git commit -m "Delete the test-file.txt"
    [main 508a7dd] Delete the test-file.txt
     1 file changed, 1 deletion(-)
     delete mode 100644 test-file.txt
    ``
  4. Check the working tree is 'clean'.

    git status

    Example Output:

    [oracle@ol-node-01 start-git]$ git status
    On branch main
    nothing to commit, working tree clean
  5. Confirm the file is deleted from the repository.

    git ls-files

    Example Output:

    [oracle@ol-node-01 start-git]$ git ls-files
    first-commit.txt
    second-commit.txt
  6. Confirm that the file has been deleted from project's directory on Oracle Linux.

    ls

    Example Output:

    [oracle@ol-node-01 start-git]$ ls
    first-commit.txt  second-commit.txt

    This confirms using the git rm command removes the file from both the Git repository and also in the Oracle Linux filesystem.

Summary

This concludes the walkthrough demonstrating how to install, configure and start using Git on Oracle Linux.

For More Information

See other related resources:

SSR