Solutions to the rebasing exercises, all done in the dev-1alice/
directory:
-
Switch the order of commits
04-fourth
and05-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
branch2
intomain
but NOT the changes ofbranch1
- Start in
branch2
withgit checkout branch2
- Now do
git rebase -i HEAD~8
, remove the two commits frombranch1
, save the file git checkout main
git merge branch2
- That's it, you're done! Verify with
git log --pretty=oneline
. - Graphs:
- Start in
-
Squash the two commits in
branch1
then push toorigin
- Check out the branch:
git checkout branch1
- Rebase the branch and squash the final commit into the previous one, so
branch1
only has one commit. - Push with
git push --force-with-lease
- Verify by changing into
../repo.git
and 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
origin
and is only recommended if you are in a branch that only you work on. - The difference between
--force-with-lease
versus--force
is that the latter will overwrite the remote with your changes.--force-with-lease
requires 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.git
and runninggit log --pretty=oneline
- Graphs:
- Start with
-
Switch the order of commits
01-first-will-conflict
and02-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.txt
and resolve the conflicts git add file.txt
- Note that
git log
will show a VERY incomplete history at this point. That's fine--you traveled back in time.
- Note that
git commit
- Running
git status
tells 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.txt
and resolve the conflicts git add file.txt
git commit
git 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.git
and runninggit log --pretty=oneline
- Start with
-
Run
git rebase -i
and delete commit04-fourth
. Then recover it.- Use
git reflog
to find the commit you removed frommain
. git show COMMIT_ID
can be used to confirm it's the commit you want.git merge COMMIT_ID
will merge that commit back intomain
- Extra Credit: Use
git rebase -i HEAD~5
to 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-conflict
and02-second-will-conflict
- Start with
git rebase -i HEAD~5
, switch the lines with those two commits, then save the file. - Edit
file.txt
and resolve the conflicts git add file.txt
- Note that
git log
will show a VERY incomplete history at this point. That's fine--you traveled back in time.
- Note that
git commit
- Running
git status
tells 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.txt
and resolve the conflicts git add file.txt
git commit
git 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.