Skip to content

Commit c202515

Browse files
updated the merge and rebase section
1 parent 400ea14 commit c202515

1 file changed

Lines changed: 73 additions & 1 deletion

File tree

README.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,37 @@ Because both branches had moved forward independently, Git created a `merge comm
311311
* the history shows exactly that parallel work happened
312312
* nothing was rewritten
313313

314+
### Now you want to erase all the commits that you have created so far from both branches.
315+
316+
Go to `<your-merge-branch-name>` and delete the commits that you created using:
317+
318+
```console
319+
git switch <your-merge-branch-name>
320+
git reset --hard HEAD~n
321+
```
322+
Replace n with the number of commits you created on this branch.
323+
324+
After that, visualize the log:
325+
326+
``` console
327+
git log --oneline --graph --all
328+
```
329+
330+
Do the same on the main branch.
331+
332+
``` console
333+
git switch main
334+
git reset --hard HEAD~n
335+
```
336+
337+
Again, replace n with the number of commits you created on the main branch.
338+
339+
Verify the result:
340+
341+
``` console
342+
git log --oneline --graph --all
343+
```
344+
314345
## What is `git rebase`?
315346

316347
`git rebase` takes the commits from one branch and `replays them on top of another branch`,
@@ -444,7 +475,8 @@ So instead of:
444475
you now get:
445476

446477
``` console
447-
A---B---C---D'---E'
478+
main: A---B---C
479+
your-branch: D'---E'
448480
```
449481

450482
> [!IMPORTANT] D' and E' are new commits created by `rebase`.
@@ -455,6 +487,46 @@ They may contain the same changes, but they are rewritten commits.
455487
* the result looks like the work happened in a straight line
456488
* there is usually no merge commit
457489

490+
491+
### Now you want to remove all the commits created during this rebase exercise.
492+
493+
After the rebase, the commits created during this exercise are still on `<your-rebase-branch-name>`, but they were rewritten on top of `main`.
494+
495+
First, go to `<your-rebase-branch-name>` and delete the rebased commits that you created:
496+
497+
```console
498+
git switch <your-rebase-branch-name>
499+
git reset --hard HEAD~n
500+
```
501+
Replace n with the number of commits you created on this branch.
502+
503+
After that, visualize the log:
504+
505+
``` console
506+
git log --oneline --graph --all
507+
```
508+
509+
Now switch to the `main` branch and delete the commit that you created there:
510+
511+
``` console
512+
git switch main
513+
git reset --hard HEAD~n
514+
```
515+
516+
Replace n with the number of commits you created on the main branch during this exercise.
517+
518+
Visualize the history again:
519+
520+
``` console
521+
git log --oneline --graph --all
522+
```
523+
524+
If needed, delete `<your-rebase-branch-name>` as well:
525+
526+
``` console
527+
git branch -D <your-rebase-branch-name>
528+
```
529+
458530
## Configure Merge Strategy
459531

460532
We want to configure Rebase and merge as the only merge strategy.

0 commit comments

Comments
 (0)