Skip to content

ClassroomCode/git-fundamentals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Git Fundamentals

This file includes the steps for the hands-on lab exercises for each chapter.

Introduction

To check if Git is currently installed on your machine:

$ git -v

This should output a version string like the following:

git version 2.49.0

If the command instead produces an error, you should install Git using the instructions for your specific platform.

Exercise 1 - Initial Setup

  1. Execute a command to view all of the Git's configuration parameters and where they are located:

    $ git config --list --show-origin
  2. Make sure your name and e-mail are set correctly for your enterprise GitHub environemnt and modify them if necessary:

    $ git config --global user.name "Your Name"
    $ git config --global user.email "your.email@example.com"
  3. Change the default text editor used by Git (if you want to use something other than vim):

    Emacs:

    $ git config --global core.editor "emacs -nw"

    Pico:

    $ git config --global core.editor "pico"

    Sublime Text:

    $ git config --global core.editor "subl -n -w"

    Atom

    $ git config --global core.editor "atom --wait"

    Visual Studio Code

    $ git config --global core.editor "code --wait"

    [!NOTE] If you don't see your preferred editor here, perform a web search for how to use it with Git.

    [!TIP] One way that you can test to see if your editor is configured properly is by trying to edit the global config file with git config --global -e

  4. Check for existing SSH keys:

    $ ls -al ~/.ssh

    [!NOTE] The filenames of supported public keys for GitHub include:

    • id_rsa.pub
    • id_ecdsa.pub
    • id_ed25519.pub
  5. Generate a new SSH key pair (if necessary):

    $ ssh-keygen -t ed25519 -C "your_email@example.com"
  6. Add the SSH private key to the ssh-agent:

    $ eval "$(ssh-agent -s)"
    $ ssh-add ~/.ssh/id_ed25519

    [NOTE!] Depending on your environment, you may need to use a different command. For example, you may need to use root access by running sudo -s -H before starting the ssh-agent, or you may need to use exec ssh-agent bash or exec ssh-agent zsh to run the ssh-agent.

  7. Copy the SSH public key to your clipboard. If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace.

    $ cat ~/.ssh/id_ed25519.pub
    # Then select and copy the contents of the id_ed25519.pub file
    # displayed in the terminal to your clipboard
  8. In the upper-right corner of any page on GitHub, click your profile photo, then click Settings.

  9. In the "Access" section of the sidebar, click SSH and GPG keys.

  10. Click New SSH key or Add SSH key.

  11. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal laptop, you might call this key "Personal laptop".

  12. Select a key type of Authentication Key.

  13. In the "Key" field, paste your public key.

  14. Click Add SSH key.

  15. If prompted, confirm access to your account on GitHub.

  16. Repeat steps 4-9 again but select Signing Key as the key type.

  17. Clone the training repository by using the SSH URL from the repository page.

    SSH URL

    $ git clone ssh_url_from_repository_page

    [!NOTE] This will create a sub-directory in the current working directory with the same name as the repository ("git-fundamentals").

Exercise 2 - Git Basics

  1. Ensure you are in the root directory of the repository:

    $ cd git-fundamentals
  2. Add two new files:

    $ echo "This is file a" > a.txt
    $ echo "This is file b" > b.txt
  3. Use the status command to confirm that the files are untracked:

    $ git status
  4. Add both files to the staging area and view the status:

    $ git add -A
    $ git status
  5. Remove b.txt from the staging area and view the status:

    $ git restore --staged b.txt
    $ git status
  6. Add b.txt back to the staging area:

    $ git add b.txt
  7. Modify the contents of b.txt and view the status.

  8. Stage the change made to b.txt and view the status:

    $ git add -A
  9. Create a new commit for the addition of the two files and check the status:

    $ git commit -m "Added a and b"
  10. Use the log command and the status command to examine the effect of the commit:

    $ git log --oneline
    $ git status
  11. Undo the last commit by using the reset command:

    $ git reset HEAD~1
  12. View the effect of the reset by using the status and log commands:

    $ git status
    $ git log --oneline

Exercise 3 - Branching

  1. Use the branch command to confirm that you are on the main branch:

    $ git branch
  2. Use the status command to confirm that you have two untracked files (a.txt and b.txt):

    $ git status
  3. Create a new branch named feature-a:

    $ git checkout -b feature-a
  4. Add the two untracked files to the staging area for the new branch and create a new commit:

    $ git add -A
    $ git commit "Added a and b"
  5. Use the log command to view the new commit:

    $ git log --oneline
  6. Switch back to the main branch:

    $ git switch main
  7. Confirm that a.txt and b.txt are not present on the main branch and then use the merge command to perform a fast-forward merge:

    $ ls
    $ git merge feature-a
  8. Confirm that a.txt and b.txt are now present on the main branch and use the status command to see that main branch is now ahead of origin/main by one commit:

    $ ls
    $ git status
  9. Switch back to the feature-a branch and add a new file:

    $ git switch feature-a
    $ echo "This is file c" > c.txt
  10. Stage the change and create a commit:

    $ git add -A
    $ git commit -m "Added c"
  11. Switch back to the main branch and create a new file there as well:

    $ git switch main
    $ echo "This is file d" > d.txt
    $ git add -A
    $ git commit -m "Added d"
  12. Use the merge command to perform a 3-way merge:

    $ git merge feature-a
  13. Now that the merge has been completed, delete the feature-a branch:

    $ git branch -D feature-a
  14. To introduce a merge conflict, start by creating a new branch with another new file:

    $ git checkout -b feature-b
    $ echo "This is file e" > e.txt
    $ git add -A
    $ git commit -m "Added e"
  15. Create a file with the same name on the main branch:

    $ git switch main
    $ echo "This is some text" > e.txt
    $ git add -A
    $ git commit -m "Added e"
  16. Try to merge feature-b into the main branch and take note of the conflict message:

    $ git merge feature-b
  17. Use the status command to get more information about the merge conflict:

    $ git status
  18. Edit e.txt to resolve the conflict.

  19. Use the merge command to stage the conflict resolution and create a merge commit:

    $ git merge --continue
  20. With the merge complete, delete the feature-b branch:

    $ git branch -D feature-b
  21. To practice creating a squashed commit, create a new branch with three individual commits:

    $ git checkout -b feature-c
    [ Modify a.txt ]
    $ git add -A
    $ git commit -m "Modified a"
    [ Modify c.txt ]
    $ git add -A
    $ git commit -m "Modified c"
    [ Modify d.txt ]
    $ git add -A
    $ git commit -m "Modified d"
  22. To ensure a 3-way merge will be performed, add a commit back on the main branch:

    $ git switch main
    [ Modify b.txt ]
    $ git add -A
    $ git commit -m "Modified b"
  23. Start the process of performing a squashed merge:

    $ git merge --squash feature-c
  24. Use the commit command to complete the merge and review the commit message:

    $ git commit
  25. Delete branch feature-c and then use the log command to review the results of the squashed merge:

    $ git branch -D feature-c
    $ git log --oneline
  26. To practice rebasing a branch prior to a merge, create a new branch with two individual commits:

    $ git checkout -b feature-d
    [ Modify a.txt ]
    $ git add -A
    $ git commit -m "Another change to a"
    [ Modify b.txt ]
    $ git add -A
    $ git commit -m "Another change to b"
  27. Create a separate commit back on the main branch:

    $ git switch main
    [ Modify c.txt ]
    $ git add -A
    $ git commit -m "Another change to c"
  28. Switch back to feature-d and rebase the branch onto the top of main:

    $ git switch feature-d
    $ git rebase main
  29. Switch back to the main branch and merge feature-d (notice that the merge is performed as a fast-forward):

    $ git switch main
    $ git merge feature-d
  30. To prepare for the next exercise, use the fetch and reset commands to discard all local changes and get back in sync with the origin:

    $ git fetch origin
    $ git reset --hard origin/main

Exercise 4 - GitHub

  1. Use the GitHub web interface to add a new file. The file should be named file.txt and it should be located in a folder using your name (e.g., firstname-lastname) inside of the student-files folder. The contents of the files does not matter.

  2. Execute the git status command and notice that it still shows the main branch as being up to date (event though a file has been added):

    $ git status
  3. Execute the git fetch command followed by git status and notice that the state is now accurately reflected:

    $ git fetch
    $ git status
  4. Perform a pull to bring get the local main branch in sync with the origin:

    $ git pull
  5. Create a new branch and use your name as part of the branch name:

    $ git checkout -b firstname-lastname-feature-a
  6. Switch to the directory under student-files that you created earlier:

    $ cd student-files/firstname-lastname
  7. Create and commit a two new files:

    $ echo "This is part 1" > part1.txt
    $ git add -A
    $ git commit -m "Added part 1"
    $ echo "This is part 2" > part2.txt
    $ git add -A
    $ git commit -m "Added part 2"
  8. Push the feature branch to the origin:

    $ git push -u origin firstname-lastname-feature-a
  9. Use the GitHub web interface to create a new pull request for the branch (the title and description can be anything you like):

  10. Use the "Pull Requests" tab to navigate to the new pull request and explore the commits and files changed.

  11. Add a comment to a line in part1.txt and click "Start a review".

  12. Add a suggestion for a line in part2.txt and click "Add review comment".

  13. Click "Finish your review" in the upper-right, provide a general comment, and click "Submit review".

  14. Navigate back to the conversation view for the pull request.

  15. Resolve the conversation for the comment about part1.txt.

  16. Commit the suggestion for that was provided for part2.txt.

  17. Use the GitHub web interface to manually add another part1.txt file in the directory with your name in in the main branch.

  18. Navigate back to the pull request and notice that the pull request now has conflicts that must be resolved.

  19. Back in your local repository, perform a pull for both branches:

    $ git switch main
    $ git pull
    $ git switch firstname-lastname-feature-a
    $ git pull
  20. Obtain the commit ID for the last commit on the main branch:

    $ git merge-base firstname-lastname-feature-a main
  21. Use the commit ID to start an interactive rebase:

    $ git rebase -i id-from-previous-step
  22. Adjust the commits to squash the second and third commit (your IDs will be different) and save the file:

    pick 72002a4 Added part 1
    squash 7369669 Added part 2
    squash bea0641 Added more
  23. In the next file that appears, modify the commit message if desired and save the file.

  24. Use the rebase command to continue with the interactive rebase (you should receive a message about the conflict):

    $ git rebase origin/main
  25. Edit part.txt to resolve the conflict.

  26. Mark the resolution and continue with the rebase:

    $ git add part1.txt
    $ git rebase --continue
  27. Modify the commit message if desired.

  28. Force push the changed to the branch:

    $ git push -f
  29. Return to GitHub, merge the pull request, and delete the branch.

  30. In your local repository, perform a pull for the main branch, and delete the feature branch:

    $ git switch main
    $ git pull
    $ git branch -d firstname-lastname-feature-a

Resources

  • Git Graph extension for Visual Studio Code

    • Once installed, simply select [File > Open] in Visual Studio code and select a repository directory (one that contains a .git folder)
  • dangitgit.com

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published