Interactive Rebase
Objective: The objective of this assignment is to learn how to perform an interactive rebase in Git. Interactive rebasing allows you to modify commit history by reordering, combining, or editing commits. This assignment will help you practice using interactive rebase to manage commit history effectively.
You want to squash, reorder, or edit commits before pushing them to the remote repository to maintain a cleaner commit history.
Instructions:
Part 1: Repository Setup
1. Create a new directory on your computer for this assignment and name it "GitInteractiveRebaseAssignment."
2. Navigate to the "GitInteractiveRebaseAssignment" directory in your terminal or command prompt.
3. Initialize a new Git repository in this directory.
Part 2: Making Commits
1. Open a text editor or use a command-line text editor like Vim or Nano to create and edit files in your repository.
2. Add and commit several changes with meaningful commit messages.
Part 3: Interactive Rebase
1. Use the following Git command to start an interactive rebase, where you can modify your commit history:
git rebase -i HEAD~<number-of-commits>
Replace `<number-of-commits>` with the number of commits you want to include in the interactive rebase. You can use `pick`, `reword`, `edit`, `squash`, `fixup`, and `drop` to specify what you want to do with each commit in the rebase.
2. Follow the on-screen instructions to modify your commit history as desired. You can reorder, combine, edit commit messages, or even discard commits.
3. Save and close the text editor after making your changes.
4. Complete the rebase by running the following command:
git rebase --continue
Part 4: Viewing Commit History
1. Use the following Git command to view the updated commit history:
git log --oneline
This will display the commit history with abbreviated commit messages.
Solutions
Part 1: Repository Setup
# Create a directory
mkdir GitInteractiveRebaseAssignment
# Navigate to the directory
cd GitInteractiveRebaseAssignment
# Initialize a Git repository
git init
Part 2: Making Commits
Use your preferred text editor to create and edit files in the repository. Then use the following Git commands to add and commit changes:
# Add changes
git add <file1> <file2> ...
# Commit changes with a meaningful message
git commit -m "Your commit message here"
Part 3: Interactive Rebase
# Start an interactive rebase to modify commit history
git rebase -i HEAD~<number-of-commits>
Follow the on-screen instructions, make changes to the commit history as desired, and complete the rebase when finished.
Part 4: Viewing Commit History
# View the updated commit history
git log --oneline