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. 
  1. 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
  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:
      1. mkdir git-practice
      2. 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 

Git workflow

Exercise 2 (Merge without conflict)

  1. You start in branch ‘main’. Ensure that you are in main branch to begin.
  2. Create and switch to a new branch named ‘bugFix’.
  3. Make changes in the ‘bugFix’ branch. Create or edit a file X.
  4. Commit the changes in the ‘bugFix’  branch.
  5. Switch back to the ‘main’ branch.
  6. Make changes in the ‘main’ branch. Create or edit another file in the ‘main’ branch. Save these changes.
  7. Commit changes in the ‘main’ branch.
  8. 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.