This guide will walk you through practicing git rebase on a local repository. We'll start by creating a simple repository and make some commits, then practice rebasing a branch onto the updated main branch.
1. Initialize an empty repository:
Open your terminal and navigate to the directory where you want to create the repository. Then, run:
git init
This initializes an empty Git repository in the current directory.
2. Create a text file and commit it:
Create a new file named test.txt
and add some content to it. Then, commit the changes using the following commands:
echo "Initial content" > test.txt
git add test.txt
git commit -m "Added initial content"
3. Create a new branch:
Create a new branch named feature
and switch to it:
git checkout -b feature
4. Modify the existing file and add a new file in the feature branch:
Change the content of test.txt
and create a new file named new_file.txt
in the feature
branch. Then, commit these changes:
echo "Content updated in feature branch" > test.txt
touch new_file.txt
git add .
git commit -m "Updated content and added new file in feature branch"
5. Switch back to main branch and make further changes:
Switch back to the main
branch using:
git checkout main
Make further changes to test.txt
and create another new file named another_file.txt
. Commit these changes:
echo "Another change in main branch" >> test.txt
touch another_file.txt
git add .
git commit -m "More changes in main branch"
6. Rebase the feature branch onto main:
Now, let's rebase the feature
branch onto the latest commit in the main
branch:
git checkout feature
git rebase main
This command will try to replay the commits from the feature
branch on top of the latest commit in the main
branch.
7. Resolve conflicts (if any):
If the rebasing process encounters any conflicts (i.e., situations where the same lines of code were modified in both branches), it will stop and ask you to resolve them manually. Edit the conflicting files to choose the desired changes and then use git add
followed by git rebase --continue
to resume the rebasing process.
Once the rebase is complete, you'll have a cleaner commit history where the changes from the feature
branch are applied on top of the latest changes in the main
branch.
Remember: This is just a practice scenario. Be careful when rebasing local branches that have been shared with others.