Solutions to the rebasing exercises, all done in the dev-1alice/ directory:
-
Switch the order of commits
04-fourthand05-fifth- Start with
git rebase -i HEAD~5, switch the lines with those two commits, then save the file. You're done! - Graphs:
- Start with
-
Merge the changes of
branch2intomainbut NOT the changes ofbranch1- Start in
branch2withgit checkout branch2 - Now do
git rebase -i HEAD~8, remove the two commits frombranch1, save the file git checkout maingit merge branch2- That's it, you're done! Verify with
git log --pretty=oneline. - Graphs:
- Start in
-
Squash the two commits in
branch1then push toorigin- Check out the branch:
git checkout branch1 - Rebase the branch and squash the final commit into the previous one, so
branch1only has one commit. - Push with
git push --force-with-lease - Verify by changing into
../repo.gitand runninggit log --pretty=oneline branch1 - Graphs:
- Check out the branch:
-
Switch the order of commits
04-fourth and 05-fifth, push toorigin- Start with
git rebase -i HEAD~5, switch the lines with those two commits, then save the file. - There are two ways to do this:
git pull && git push- This is the preferred way, and will cause your (re-written) history to be merged in with what's in
origin.
- This is the preferred way, and will cause your (re-written) history to be merged in with what's in
git push --force-with-lease- This will overwrite what's in
originand is only recommended if you are in a branch that only you work on. - The difference between
--force-with-leaseversus--forceis that the latter will overwrite the remote with your changes.--force-with-leaserequires that you have the most recent commit (the HEAD) from the remote, to ensure that you don't overwrite changes which were checked in since your lastpull. This makes it safer, or at least less unsafe.
- This will overwrite what's in
- Verify by changing into
../repo.gitand runninggit log --pretty=oneline - Graphs:
- Start with
-
Switch the order of commits
01-first-will-conflictand02-second-will-conflict, THEN push toorigin- Start with
git rebase -i HEAD~5, switch the lines with those two commits, then save the file. - Edit
file.txtand resolve the conflicts git add file.txt- Note that
git logwill show a VERY incomplete history at this point. That's fine--you traveled back in time.
- Note that
git commit- Running
git statustells you that you're not done yet, and tells you what to do next: git rebase --continue- Uh oh, we have another conflict because the next commit also changes
file.txt. - Edit
file.txtand resolve the conflicts git add file.txtgit commitgit rebase --continue- At this point, you've now merged your changes into main, let's push them with
git push origin. - That's it, you're done!
- Verify by changing into
../repo.gitand runninggit log --pretty=oneline
- Start with
-
Run
git rebase -iand delete commit04-fourth. Then recover it.- Use
git reflogto find the commit you removed frommain. git show COMMIT_IDcan be used to confirm it's the commit you want.git merge COMMIT_IDwill merge that commit back intomain- Extra Credit: Use
git rebase -i HEAD~5to move the commit back to its original location - There are a few other ways as well. Feel free to play around in Git, that's what this repo is for!
- Use
-
Switch the order of commits
01-first-will-conflictand02-second-will-conflict- Start with
git rebase -i HEAD~5, switch the lines with those two commits, then save the file. - Edit
file.txtand resolve the conflicts git add file.txt- Note that
git logwill show a VERY incomplete history at this point. That's fine--you traveled back in time.
- Note that
git commit- Running
git statustells you that you're not done yet, and tells you what to do next: git rebase --continue- Uh oh, we have another conflict because the next commit also changes
file.txt. - Edit
file.txtand resolve the conflicts git add file.txtgit commitgit rebase --continue- No more conflicts, so that's it, you're done! Verify with
git log. - Graphs:
- Start with
If the commit history for any of the above look "weird" when you're done, that's because you're rewriting history, so yeah. The tool tig can make the history a little clearer to read.