This file includes the steps for the hands-on lab exercises for each chapter.
To check if Git is currently installed on your machine:
$ git -vThis should output a version string like the following:
git version 2.49.0If the command instead produces an error, you should install Git using the instructions for your specific platform.
-
Execute a command to view all of the Git's configuration parameters and where they are located:
$ git config --list --show-origin -
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"
-
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 -
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
-
Generate a new SSH key pair (if necessary):
$ ssh-keygen -t ed25519 -C "your_email@example.com" -
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 -Hbefore starting the ssh-agent, or you may need to useexec ssh-agent bashorexec ssh-agent zshto run the ssh-agent. -
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
-
In the upper-right corner of any page on GitHub, click your profile photo, then click Settings.
-
In the "Access" section of the sidebar, click SSH and GPG keys.
-
Click New SSH key or Add SSH key.
-
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".
-
Select a key type of Authentication Key.
-
In the "Key" field, paste your public key.
-
Click Add SSH key.
-
If prompted, confirm access to your account on GitHub.
-
Repeat steps 4-9 again but select Signing Key as the key type.
-
Clone the training repository by using the SSH URL from the repository page.
$ 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").
-
Ensure you are in the root directory of the repository:
$ cd git-fundamentals -
Add two new files:
$ echo "This is file a" > a.txt $ echo "This is file b" > b.txt
-
Use the status command to confirm that the files are untracked:
$ git status -
Add both files to the staging area and view the status:
$ git add -A $ git status
-
Remove b.txt from the staging area and view the status:
$ git restore --staged b.txt $ git status
-
Add b.txt back to the staging area:
$ git add b.txt -
Modify the contents of b.txt and view the status.
-
Stage the change made to b.txt and view the status:
$ git add -A -
Create a new commit for the addition of the two files and check the status:
$ git commit -m "Added a and b" -
Use the log command and the status command to examine the effect of the commit:
$ git log --oneline $ git status
-
Undo the last commit by using the reset command:
$ git reset HEAD~1 -
View the effect of the reset by using the status and log commands:
$ git status $ git log --oneline
-
Use the branch command to confirm that you are on the main branch:
$ git branch -
Use the status command to confirm that you have two untracked files (a.txt and b.txt):
$ git status -
Create a new branch named feature-a:
$ git checkout -b feature-a -
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"
-
Use the log command to view the new commit:
$ git log --oneline -
Switch back to the main branch:
$ git switch main -
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
-
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
-
Switch back to the feature-a branch and add a new file:
$ git switch feature-a $ echo "This is file c" > c.txt
-
Stage the change and create a commit:
$ git add -A $ git commit -m "Added c"
-
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"
-
Use the merge command to perform a 3-way merge:
$ git merge feature-a -
Now that the merge has been completed, delete the feature-a branch:
$ git branch -D feature-a -
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"
-
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"
-
Try to merge feature-b into the main branch and take note of the conflict message:
$ git merge feature-b -
Use the status command to get more information about the merge conflict:
$ git status -
Edit e.txt to resolve the conflict.
-
Use the merge command to stage the conflict resolution and create a merge commit:
$ git merge --continue -
With the merge complete, delete the feature-b branch:
$ git branch -D feature-b -
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"
-
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"
-
Start the process of performing a squashed merge:
$ git merge --squash feature-c -
Use the commit command to complete the merge and review the commit message:
$ git commit -
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
-
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"
-
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"
-
Switch back to feature-d and rebase the branch onto the top of main:
$ git switch feature-d $ git rebase main
-
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
-
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
-
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.
-
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 -
Execute the git fetch command followed by git status and notice that the state is now accurately reflected:
$ git fetch $ git status
-
Perform a pull to bring get the local main branch in sync with the origin:
$ git pull -
Create a new branch and use your name as part of the branch name:
$ git checkout -b firstname-lastname-feature-a -
Switch to the directory under student-files that you created earlier:
$ cd student-files/firstname-lastname -
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"
-
Push the feature branch to the origin:
$ git push -u origin firstname-lastname-feature-a -
Use the GitHub web interface to create a new pull request for the branch (the title and description can be anything you like):
-
Use the "Pull Requests" tab to navigate to the new pull request and explore the commits and files changed.
-
Add a comment to a line in part1.txt and click "Start a review".
-
Add a suggestion for a line in part2.txt and click "Add review comment".
-
Click "Finish your review" in the upper-right, provide a general comment, and click "Submit review".
-
Navigate back to the conversation view for the pull request.
-
Resolve the conversation for the comment about part1.txt.
-
Commit the suggestion for that was provided for part2.txt.
-
Use the GitHub web interface to manually add another part1.txt file in the directory with your name in in the main branch.
-
Navigate back to the pull request and notice that the pull request now has conflicts that must be resolved.
-
Back in your local repository, perform a pull for both branches:
$ git switch main $ git pull $ git switch firstname-lastname-feature-a $ git pull
-
Obtain the commit ID for the last commit on the main branch:
$ git merge-base firstname-lastname-feature-a main -
Use the commit ID to start an interactive rebase:
$ git rebase -i id-from-previous-step -
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
-
In the next file that appears, modify the commit message if desired and save the file.
-
Use the rebase command to continue with the interactive rebase (you should receive a message about the conflict):
$ git rebase origin/main -
Edit part.txt to resolve the conflict.
-
Mark the resolution and continue with the rebase:
$ git add part1.txt $ git rebase --continue
-
Modify the commit message if desired.
-
Force push the changed to the branch:
$ git push -f -
Return to GitHub, merge the pull request, and delete the branch.
-
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
-
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)
