Skip to content

Latest commit

 

History

History
318 lines (249 loc) · 5.45 KB

File metadata and controls

318 lines (249 loc) · 5.45 KB

Basic Git Exercises (1-10)

graph TD;
    A[Initialize Repository] --> B[Clone Repository];
    B --> C[Check Git Status];
    C --> D[Staging and Committing];
    D --> E[Creating & Switching Branches];
    E --> F[Merging Branches];
    F --> G[Undo Last Commit];
    G --> H[Stashing & Restoring];
    H --> I[Rebasing Branch];
    I --> J[Resolving Merge Conflicts]; 
Loading

Exercise 1: Initializing a Repository

graph TD;
    A[Empty Directory] -->|git init| B[Git Repository Created];
    B -->|echo '# My Project' > README.md| C[README.md Added];
    C -->|git add README.md| D[Staged File];
    D -->|git commit -m 'Initial commit'| E[First Commit];
Loading

Starting Point Setup

mkdir my-git-project && cd my-git-project  

Goal

Initialize a Git repository and make the first commit.

Commands

git init  
echo "# My Project" > README.md  
git add README.md  
git commit -m "Initial commit"  

Verification

git status  # Should show a clean working directory  
git log --oneline  # Should show the initial commit  

Exercise 2: Cloning a Repository

graph TD;
    A[Remote Repository] -->|git clone| B[Local Copy];
Loading

Starting Point Setup

Create a remote repository on GitHub/GitLab.

Goal

Clone the repository locally.

Commands

git clone <repository-url>  

Verification

ls -la <repo-name>  # Should list all files  
cd <repo-name> && git status  

Exercise 3: Tracking Changes with Git Status

Starting Point Setup

echo "Some content" > file.txt  

Goal

Check the status of your working directory.

Commands

git status  

Verification

Git should show file.txt as untracked.


Exercise 4: Staging and Committing Changes

graph TD;
    A[Create file1.txt] -->|git add file1.txt| B[Staged];
    B -->|git commit -m 'Added file1.txt'| C[Committed];
Loading

Starting Point Setup

echo "Hello, Git!" > file1.txt  
git add file1.txt  
git commit -m "Added file1.txt"  

Goal

Make another change and commit it.

Commands

echo "New content" >> file1.txt  
git add file1.txt  
git commit -m "Updated file1.txt"  

Verification

git log --oneline -n 2  

Exercise 5: Creating and Switching Branches

graph TD;
    A[main branch] -->|git checkout -b feature-branch| B[feature-branch];
Loading

Starting Point Setup

git checkout -b feature-branch  

Goal

Confirm that you've switched branches.

Commands

git branch  # Should list 'feature-branch' and 'main'  
git checkout main  

Exercise 6: Merging a Branch

graph TD;
    A[main branch] -->|Create new branch| B[feature-branch];
    B -->|Merge back| A;
Loading

Starting Point Setup

git checkout -b new-feature  
echo "Feature work" > feature.txt  
git add feature.txt  
git commit -m "Added feature.txt"  
git checkout main  

Goal

Merge new-feature into main.

Commands

git merge new-feature  
git branch -d new-feature  

Verification

git log --oneline --graph  

Exercise 7: Undoing the Last Commit

graph TD;
    A[Last Commit] -->|git reset HEAD~1| B[Unstaged Changes]; 
Loading

Starting Point Setup

echo "Temporary change" > temp.txt  
git add temp.txt  
git commit -m "Temporary commit"  

Goal

Undo the last commit but keep changes.

Commands

git reset HEAD~1  

Verification

git status  

Exercise 8: Stashing and Restoring Changes

graph TD;
    A[Uncommitted Changes] -->|git stash| B[Changes Stashed];
    B -->|git stash pop| A; 
Loading

Starting Point Setup

echo "Work in progress" > wip.txt  
git add wip.txt  

Goal

Stash changes, switch branches, and restore them.

Commands

git stash  
git checkout main  
git checkout -b another-branch  
git stash pop  

Verification

ls  

Exercise 9: Rebasing a Feature Branch

graph TD;
    A[main branch] -->|Rebase| B[feature-branch Rewritten]; 
Loading

Starting Point Setup

git checkout -b rebase-branch  
echo "Rebase test" > rebase.txt  
git add rebase.txt  
git commit -m "Added rebase.txt"  
git checkout main  
echo "Main branch update" > main.txt  
git add main.txt  
git commit -m "Updated main"  
git checkout rebase-branch  

Goal

Rebase rebase-branch onto main.

Commands

git rebase main  

Verification

git log --oneline --graph  

Exercise 10: Resolving Merge Conflicts

graph TD;
    A[main branch] -->|Conflicting changes| B[feature-branch];
    B -->|Merge Attempt| C[Conflict];
    C -->|Resolve & Commit| D[Merge Completed];
Loading

Starting Point Setup

git checkout -b conflict-branch  
echo "Hello" > conflict.txt  
git add conflict.txt  
git commit -m "Created conflict.txt"  
git checkout main  
echo "Hello, World!" > conflict.txt  
git add conflict.txt  
git commit -m "Updated conflict.txt in main"  
git checkout conflict-branch  

Goal

Merge main and resolve conflicts.

Commands

git merge main  
# Manually resolve conflicts in conflict.txt  
git add conflict.txt  
git commit -m "Resolved conflict"  

Verification

git log --oneline --graph