Week 4 Task – Daily Practice Tasks - snir1551/DevOps-Linux GitHub Wiki

Week 4 Task – Daily Practice Tasks

Task 1: Branching & Switching

Steps:

  • Create a new local Git repository:
mkdir my-git-project
cd my-git-project
git init
  • Create main branch:
git branch -M main
echo "Initial content" > README.md
git add README.md
git commit -m "Initial commit"
  • Create two branches:
git branch feature-a
git branch feature-b
  • show the branches that you have:
git branch

you need see: main, feature-a, feature-b

  • Switch between branches:
git switch feature-a
git switch feature-b
  • Add a simple change and commit it in each branch:

In feature-a:

git switch feature-a
echo "Hello from feature-a" > greetings.txt
git add greetings.txt
git commit -m "Add greetings.txt in feature-a"

In feature-b:

git switch feature-b
echo "Hello from feature-b" > greetings.txt
git add greetings.txt
git commit -m "Add greetings.txt in feature-b"

Task 2: Simulate and Resolve Merge Conflicts

  • Modify the same line in a file on both feature-a and feature-b
git switch feature-a
echo "Hello from feature-a" > greetings.txt
git add greetings.txt
git commit -m "Update greetings.txt in feature-a"
git switch feature-b
echo "Hello from feature-b" > greetings.txt
git add greetings.txt
git commit -m "Update greetings.txt in feature-b"

Now both branches have different changes in the same file (greetings.txt).

  • Merge one branch into the other and observe the conflict

For example, merge feature-b into feature-a:

git switch feature-a
git merge feature-b

You will see a conflict like this:

Auto-merging greetings.txt
CONFLICT (add/add): Merge conflict in greetings.txt
Automatic merge failed; fix conflicts and then commit the result.
  • Resolve the conflict using either command-line or VS Code

To view the conflict markers in the file, run:

cat greetings.txt

This will display something like:

image

To edit the file and resolve the conflict, open it with Vim:

vim greetings.txt

image

After editing the file, the conflict is resolved like this: image

after saved greetings.txt file

git add greetings.txt
git commit -m "merged feature-b into feature-a"

Task 3: Rebase and Cherry-Pick

  • Use git rebase to reapply commits from feature-a onto main

    view the graph of the commits, we want that will be linear

    git log --oneline --graph --all
    

    image

    Switch to feature-a:

    git switch feature-a
    

    Rebase onto main:

    git rebase main
    
  • Document what happens to the commit history

    • Rebase moves the commits from feature-a and reapplies them on top of main.
    • The commits from feature-a get new commit hashes, because Git is creating new commits during the rebase.
    • The commit history becomes linear: It looks as if the feature-a changes were made after the latest commit on main.
  • Use git cherry-pick to apply a single commit from feature-b to main

    First, find the commit hash in feature-b:

    git log feature-b --oneline
    

    Switch to main:

    git switch main
    

    Apply the commit:

    git cherry-pick yourcommit
    
  • Explain the difference between rebase and merge in your own words

Rebase Merge
Moves commits from one branch onto another, creating new commits Combines changes from both branches into a new merge commit
Creates a linear history (no merge commits) Keeps the full branching history (shows splits and merges)
Commit hashes change Commit hashes stay the same
Ideal for cleaning up a feature branch before merging Ideal for combining work from different branches
Can cause conflicts that need to be resolved per-commit Can cause conflicts but usually resolved all at once during the merge

Create a new GitHub repository & Set the remote in your local repository

  • Go to https://github.com/new.

  • Give your repository a name (e.g., MyProject).

  • Keep it empty (do not add README, .gitignore, or license for now).

  • git remote set-url origin [email protected]:your-username/your-repository.git

  • Set the remote in your local repository

  • In your local Git project folder, connect your local repository to the GitHub repository via SSH

GitHub Pull Requests & Code Review

  • Push both branches (feature-a, feature-b) to your GitHub repository

    Push feature-a:

    git push -u origin feature-a
    

    Push feature-b:

    git push -u origin feature-b
    
  • Create a pull request from one branch into main

    • Go to your GitHub repository.
    • Click on the "Compare & pull request" button for feature-a (or feature-b).
    • Set the base branch to main and the compare branch to feature-a.
    • Click "Create pull request".
    • Add a meaningful title and description explaining your changes.
  • Request a review from a classmate or mentor

    • On your pull request page, click "Reviewers" in the sidebar.
    • Select a classmate or mentor from the list to request their review.
  • Write at least one constructive code comment in someone else's pull request

    • Go to a classmate’s pull request on GitHub.
    • Click on "Files changed".
    • Add a comment on a specific line of code (click the + icon).
    • Your comment should be constructive

Task 5: Stash, Amend, and Cleanup

  • Make local changes and store them using git stash

    Make a change to a file, for example:

    echo "Temporary changes" >> greetings.txt
    

    Check the change:

    git status
    

    Stash the changes:

    git stash
    

    our working directory is now clean again. You can view your stash with:

    git stash list

  • Restore the changes using git stash pop

    To bring your stashed changes back:

    git stash pop
    

    This restores the latest stashed changes and removes them from the stash stack.

  • Amend your last commit using git commit --amend

    Make a small additional change:

    echo "Fix bug and change commit" >> greetings.txt
    git add notes.txt
    

    Amend the last commit:

    git commit --amend
    

    You’ll enter your editor to modify the commit message (or keep it the same and save).
    This replaces the last commit with a new one that includes the updated changes.

⚠️ **GitHub.com Fallback** ⚠️