1️⃣ Checkout to the Branch You Want to Rebase:
git checkout your-branch-name
2️⃣ Identify the Commit Before Your First Commit: Use git log to find the commit hash before your first commit. For example, if A is the commit before your first commit:
git log
Look for the commit hash, let’s say it’s A.
3️⃣ Start an Interactive Rebase: Start an interactive rebase with the commit hash A:
git rebase -i A
4️⃣ Squash the Commits: In the interactive rebase window, change the word pick to squash (or s) for all commits you want to combine into one. The first commit should remain as pick.
Example:
pick abcdef1 First commit message
squash abcdef2 Second commit message
squash abcdef3 Third commit message
5️⃣ Edit the Commit Message: After saving and closing the editor, another editor window will open, allowing you to edit the commit message of the combined commit. Edit the commit message as needed, then save and close the editor.
6️⃣ Complete the Rebase: Git will reapply the changes and combine the commits into one. If there are conflicts, resolve them, then continue the rebase:
git rebase --continue
7️⃣ Push the Rebased Branch: If you have already pushed the branch to a remote repository, you will need to force push the rebased branch:
git push --force
#git #rebase #lesson