Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rebase-Split-Commit] Exercise #118 #375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions rebase-split-commit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Rebase Split Commit

## Setup

- Run `source setup.sh` (or `.\setup.ps1` in PowerShell)

## Task

We are expecting that you know the basic of `git rebase` if not then please visit [here](https://github.com/eficode-academy/git-katas/tree/master/rebase-branch) before moving with the task.

If you want split most recent commit then you can either use `rebase` or soft `reset` the most recent commit and split it manually.

In this task we are going to use interactive mode of `rebase`, for spliting the commit in between somewhere in commits below commands can be used:

- Start the `rebase` in interactive mode by either supplying the commit sha from which you want to split commit or use `--root` option to start the `rebase` from start of the first commit. Suppose we want to split from the last 3 commit then we can run command `git rebase -i HEAD~3`.
- After the interactive mode is started replace the word `pick` with `edit` or `e` for short to mark that this is the commit you want to edit, After you are done edit the editor.
- As soon you exit the editor `rebase` interactive mode with start and it drop you to the first `edit` mark commit, Now if you want to split the commit you can need to run `git reset --soft HEAD~1` command which will uncommit the current commit changes and move all the changes to staged state then you can just either unstage change and commit the file separately or make changes to the file and commit, After you are done just run `git rebase --continue` command.
- Follow the same procedure for all the `edit` marked commit until you reach to the end and their are no longer changes that need to be done you will return to the `HEAD` with the recent split changes included.
- You can confirm if the recent changes are include with either `git log` command or `git log --patch` to see the changes in detail.

**Note:** The above steps can also be used to add additional commit to `edit` mark commit without spliting the commit

## Useful commands

- `git log`
- `git log --patch` # log with diff
- `git rebase -i HEAD~3` # start rebase move at pos
- `git reset --soft HEAD~1` # reset the previous commit and move all the changes to stage state
- `git rebase --continue` # contine to next commit in rebase mode
- `git rebase --abort` # exit and abort rebase mode
27 changes: 27 additions & 0 deletions rebase-split-commit/setup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
. ..\utils\make-exercise-repo.ps1

Set-Content "1.txt" -Value ""
git add 1.txt
git commit -m "1"

git tag first-commit

Set-Content "2.txt" -Value ""
git add 2.txt
git commit -m "2"

Set-Content "3.txt" -Value ""
git add 3.txt
git commit -m "3"

Set-Content "4.txt" -Value ""
git add 4.txt
git commit -m "4"

Set-Content "5.txt" -Value ""
git add 5.txt
git commit -m "5"

Set-Content "6.txt" -Value ""
git add 6.txt
git commit -m "6"
30 changes: 30 additions & 0 deletions rebase-split-commit/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

source ../utils/utils.sh

make-exercise-repo

touch 1.txt
git add 1.txt
git commit -m "1"
git tag first-commit

touch 2.txt
git add 2.txt
git commit -m "2"

touch 3.txt
git add 3.txt
git commit -m "3"

touch 4.txt
git add 4.txt
git commit -m "4"

touch 5.txt
git add 5.txt
git commit -m "5"

touch 6.txt
git add 6.txt
git commit -m "6"
Comment on lines +12 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor repetitive code using a loop and improve commit messages.

The current implementation is repetitive. We can improve this by using a loop to create, add, and commit the files.

Consider refactoring this section using a loop. Here's an example:

for i in {1..6}
do
    touch "${i}.txt"
    git add "${i}.txt"
    git commit -m "Add file ${i}.txt"
    if [ "$i" -eq 1 ]; then
        git tag first-commit
    fi
done

This refactoring will make the script more concise and easier to maintain. It also improves the commit messages to be more descriptive.

Comment on lines +1 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance script robustness with error handling and exit status.

While the overall structure of the script is good, we can improve its robustness and usability.

Consider the following enhancements:

  1. Add error handling, especially for the git commands. For example:
if ! git add "${i}.txt"; then
    echo "Failed to add ${i}.txt" >&2
    exit 1
fi
  1. Add an exit status at the end of the script:
echo "Repository setup completed successfully."
exit 0

These changes will make the script more reliable and provide better feedback, especially when used in automated processes.