Intermediate git usage (self-study)
Intermediate git usage (optional)
If you do not have much experience with git yet, you can use this page as a self-study guide to learn more about using git. There is no deliverable for this guide, and we will not review or grade your work unless you ask us (during the lab sessions).
Resources:
1. Interactive shell/git tutorial: https://github.com/jaxtonw/iticse2024-shell-tutor Links to an external site.
-
- Some materials can be useful, but the scripts often have issues (both on Linux and Mac OS X). Minimum selection for beginners:
- assn0-intro: 5 (SSH keys), 6 (git)
- assn3.1-advanced-git: git checkout, diff
This tool may not work in all laptops.
- Progit: https://git-scm.com/book/en/v2
Links to an external site.
- Ch. 2 up to [incl.] §2.5
- Ch. 3 up to [incl.] §3.2
- Interactive tool for practice and visualization: https://learngitbranching.js.org/
Links to an external site.
Exercise 1
Step 1: Create and Set Up a Repository
- Create a New Directory:
- mkdir git-practice
- cd git-practice
- Initialize an Empty Git Repository:
git init
- Write in the file
echo "Hello World" > file1.txt
echo "This is a Git exercise" > file2.txt
- Check Status of the Repository
git status
Step 2: Stage the files
- Add files to the staging area:
git add file1.txt file2.txt
- Check git status again
Git status
Step 3: Undo a Staged Change
- Undo the Staging of a File
git restore --staged file1.txt
- git status
git status
- Add file.txt back to the Staging area
Git add file.txt
- Commit the changes
git commit -m "Initial commit with file1.txt and file2.txt"
Step 4: Practice Removing a Tracked File and Undoing with git restore
- Remove a file
git rm file2.txt
- Check git status
git status
- Undo the Removal
git restore --staged file2.txt
git restore file2.txt
- Check git status
Step 5: Amend the Last Commit Message
- Amend the Commit message
git commit --amend -m "Initial commit with files: file1.txt and file2.txt"
- Check git log to see the updated commit message
git log -1
Exercise 2 (Merge without conflict)
- You start in branch ‘main’. Ensure that you are in main branch to begin.
- Create and switch to a new branch named ‘bugFix’.
- Make changes in the ‘bugFix’ branch. Create or edit a file X.
- Commit the changes in the ‘bugFix’ branch.
- Switch back to the ‘main’ branch.
- Make changes in the ‘main’ branch. Create or edit another file in the ‘main’ branch. Save these changes.
- Commit changes in the ‘main’ branch.
- Merge the ‘bugFix’ branch into ‘main’ branch.
Exercise 3 (Merge with conflict)
Your goal is to create two branches, ‘main’ and ‘‘bugFix’ , where each branch contains edits to the same file in the same line. These changes will result in a merge conflict when you attempt to combine the branches. You will then resolve the conflict and merge the branches to consolidate all changes into the ‘main’ branch.