top of page

GIT - Day-3

project to practice resolving Git conflicts.


### Step 1: Set Up the Project


1. Create a new directory for your project and navigate into it:

mkdir git-conflict-practice

cd git-conflict-practice

2. Initialize a Git repository:

git init

### Step 2: Create and Commit Initial Files


1. Create a new file:

echo "Initial content" > file.txt

2. Stage and commit the file:

git add file.txt

git commit -m "Initial commit with file.txt"

### Step 3: Create and Switch Branches


1. Create a new branch named `branch1`:

git branch branch1

2. Create another branch named `branch2`:

git branch branch2

### Step 4: Make Conflicting Changes on Both Branches


1. Switch to `branch1` and make changes:

git checkout branch1

echo "Changes in branch1" >> file.txt

git add file.txt

git commit -m "Add changes in branch1"

2. Switch to `branch2` and make conflicting changes:

git checkout branch2

echo "Conflicting changes in branch2" >> file.txt

git add file.txt

git commit -m "Add conflicting changes in branch2"

### Step 5: Merge Branches and Resolve Conflicts


1. Switch back to the main branch:

git checkout main

2. Merge `branch1` into `main`:

git merge branch1

*Explanation*: This merge should succeed without any conflicts.


3. Merge `branch2` into `main`:

git merge branch2

*Explanation*: This merge will produce a conflict because both `branch1` and `branch2` modified `file.txt`.


4. Git will show a message indicating there are conflicts. Open `file.txt` to see the conflict markers:

cat file.txt

    The content will look something like this:

    txt

    Initial content

    Changes in branch1

    <<<<<<< HEAD

    =======

    Conflicting changes in branch2

    >>>>>>> branch2


5. Resolve the conflict by editing the file. You can manually choose how to combine the changes:

txt

Initial content

Changes in branch1

Conflicting changes in branch2

6. Stage the resolved file:

git add file.txt

7. Commit the merge:

git commit -m "Resolve merge conflict between branch1 and branch2"

### Step 6: Verify the Merge


1. Check the Git log to verify the merge:

git log --oneline


You should see a merge commit with the message you provided.



### Explanation of Commands


- `git init`: Initializes a new Git repository.

- `echo "text" > file.txt`: Creates a file with the specified text.

- `echo "text" >> file.txt`: Appends text to the file.

- `git add <file>`: Adds changes to the staging area.

- `git commit -m "<message>"`: Records changes to the repository.

- `git branch <branch-name>`: Creates a new branch.

- `git checkout <branch-name>`: Switches to the specified branch.

- `git merge <branch-name>`: Merges the specified branch into the current branch.

- `cat <file>`: Displays the content of the file.

- `git log --oneline`: Shows the commit history in a compact format.


By following these steps, you'll gain hands-on experience with creating, resolving, and committing merge conflicts in Git.


 

###Project to Practice Git Stash with a Scenario


In this sample project, you'll work through a scenario where you're developing a new feature but need to quickly fix a bug in the main branch. You'll use Git stash to save your work on the feature, switch to the main branch to fix the bug, and then return to your feature development.


### Step 1: Set Up the Project


1. Create a new directory for your project and navigate into it:

mkdir git-stash-scenario

cd git-stash-scenario

2. Initialize a Git repository:

git init

3. Create and commit initial files:

echo "Initial content" > main.txt

git add main.txt

git commit -m "Initial commit with main.txt"

### Step 2: Start Developing a New Feature


1. Create a new branch for the feature:

git checkout -b feature-branch

2. Make some changes to `main.txt` and create a new file for the feature:

echo "Feature development in progress" >> main.txt

echo "Feature-specific content" > feature.txt

3. Stage the changes but don't commit them yet:

git add main.txt feature.txt

### Step 3: Handle an Urgent Bug Fix


1. Switch back to the `main` branch:

git checkout main

*Note*: Git will prevent you from switching branches if you have uncommitted changes. This is where Git stash comes in handy.


2. Stash your changes:

git stash

3. Verify that your changes are stashed:

git stash list

You should see a stash entry like `stash@{0}: WIP on feature-branch: <commit-hash> <commit-message>`.


4. Fix the bug in the `main` branch:

echo "Bug fix" >> main.txt

git add main.txt

git commit -m "Fix a critical bug"

### Step 4: Return to Feature Development


1. Switch back to your feature branch:

git checkout feature-branch

2. Apply your stashed changes:

git stash pop

This will apply the stashed changes and remove the stash entry.


3. Verify the changes are back:

cat main.txt

cat feature.txt

You should see your feature development content and the bug fix in `main.txt` and the `feature.txt` file back.


4. Continue working on your feature and commit the changes:

echo "More feature development" >> feature.txt

git add main.txt feature.txt

git commit -m "Continue feature development after bug fix"

### Step 5: Additional Stash Operations (Optional)


1. Create another change and stash it with a message:

echo "Temporary changes" >> feature.txt

git stash save "Temporary changes for testing"

2. List all stashes:

git stash list

3. Show the details of the stash:

git show stash@{0}

4. Apply the stash without removing it from the list:

git stash apply stash@{0}

5. Drop the stash:

git stash drop stash@{0}

6. Stash changes interactively:

echo "Interactive stash" >> feature.txt

git stash -p

7. Stash including untracked files:

touch untracked_file.txt

git stash -u

8. Clear all stashes:

git stash clear

By following these steps, you'll gain practical experience using Git stash commands to save and manage your work in progress, especially when handling urgent interruptions.


 

project that helps you practice the git rebase command with a realistic scenario.


Scenario: Feature Development with Rebase In this scenario, you'll be working on a feature in a separate branch while other changes are being made to the main branch. You'll use git rebase to integrate changes from the main branch into your feature branch.


### Step 1: Set Up the Project


1. Create a new directory for your project and navigate into it:

mkdir git-rebase-practice

cd git-rebase-practice

2. Initialize a Git repository:

git init

3. Create and commit initial files:

echo "Initial content" > main.txt

git add main.txt

git commit -m "Initial commit with main.txt"

### Step 2: Create and Switch Branches


1. Create a new branch for feature development:

git checkout -b feature-branch

2. Make some changes and commit them in the feature branch:

echo "Feature work 1" >> feature.txt

git add feature.txt

git commit -m "Add feature work 1"


echo "Feature work 2" >> feature.txt

git add feature.txt

git commit -m "Add feature work 2"

### Step 3: Simulate Changes in the Main Branch


1. Switch back to the `main` branch:

git checkout main

2. Make some changes and commit them in the main branch:

echo "Main branch work 1" >> main.txt

git add main.txt

git commit -m "Add main branch work 1"


echo "Main branch work 2" >> main.txt

git add main.txt

git commit -m "Add main branch work 2"

### Step 4: Rebase Feature Branch onto Main Branch


1. Switch back to the feature branch:

git checkout feature-branch

2. Rebase the feature branch onto the main branch:

git rebase main

*Explanation*: This moves the feature branch commits to be based on top of the latest commit in the main branch, resulting in a linear history.


### Step 5: Resolve Any Conflicts


1. If there are any conflicts during the rebase, Git will pause and prompt you to resolve them. For example, if both branches modified the same part of a file, you'll see conflict markers in the file:

cat main.txt

The content might look like this:

txt

Initial content

Main branch work 1

<<<<<<< HEAD

Main branch work 2

=======

Feature work 2

>>>>>>> feature-branch

2. Resolve the conflict manually by editing the file:

txt

Initial content

Main branch work 1

Main branch work 2

Feature work 2

3. Stage the resolved file:

git add main.txt

4. Continue the rebase:

git rebase --continue

5. Repeat steps 1-4 for any additional conflicts.


### Step 6: Verify the Rebase


1. Check the commit history:

git log --oneline

You should see a linear history with your feature branch commits on top of the main branch commits.


### Explanation of Commands


- `git init`: Initializes a new Git repository.

- `echo "text" >> file.txt`: Appends text to the file.

- `git add <file>`: Adds changes to the staging area.

- `git commit -m "<message>"`: Records changes to the repository.

- `git checkout -b <branch-name>`: Creates and switches to a new branch.

- `git rebase <branch-name>`: Reapplies commits on top of another base tip.

- `git rebase --continue`: Continues the rebase after resolving conflicts.

- `git log --oneline`: Shows the commit history in a compact format.


By following these steps, you'll gain hands-on experience with the `git rebase` command and understand how it helps create a cleaner, linear commit history.

DevOps Course New Batch | 6th April 2025 | 6:00  to 7:30 AM IST Price : 22000/-Rs | Duration : 2 Months

bottom of page