DEV Community

Cover image for Git Rebase: How to Handle Request for Changes to Specific Commits
Rusydy
Rusydy

Posted on • Edited on

Git Rebase: How to Handle Request for Changes to Specific Commits

Introduction

After completing my feature branch, I created a pull request to merge my changes into the master branch. However, I received feedback from a reviewer who suggested changes to my code. To address this, I need to make the following updates:

  • In my pull request, there are three commits. For the second commit, I need to remove a few files and make substantial changes to one file.
  • I also have to merge the third commit into the first commit, but I want to keep the second commit unchanged.

Solution

first problem: Updating a Specific Commit

Suppose we have three commits in our pull request, and we have to update the second commit by removing some files and making significant changes to one file.

Here are the steps to address this:

  1. First, make sure we are on the branch that contains the three commits. We can check this by running git log and making sure we see all three commits listed.
  2. Next, run git rebase -i HEAD~3. This will open an editor with the last three commits listed.
  3. Change the word pick to edit on the line that corresponds to the second commit. Save and close the editor.
  4. Git will then start the interactive rebase. It will stop at the second commit and give us a shell to work on. We can now make any changes we want to the second commit. When we are done, run git add to add the changes to the staging area.
  5. Use git commit --amend to commit the changes to the second commit. We can change the commit message if we'd like to.
  6. Finally, use git rebase --continue to resume the interactive rebase and apply the changes to the remaining commits.

This will leave we with three commits, but the second commit will have the changes we made.

second problem: Merging Commits while Keeping One Untouched

Suppose we also need to merge the third commit into the first commit, but we want to keep the second commit unchanged. Here are the steps to address this:

  1. Same as before, make sure we are on the branch that contains the three commits. We can check this by running git log and making sure we see all three commits listed.
  2. Run git rebase -i HEAD~3. This will open an editor with the last three commits listed.
  3. In the text editor, change the position of the third commit so that it's above the second commit.

    For example:

    pick 1a2b3c4 First commit
    pick 2a3b4c5 Second commit
    pick 3a4b5c6 Third commit
    

    becomes

    pick 1a2b3c4 First commit
    pick 3a4b5c6 Third commit
    pick 2a3b4c5 Second commit
    
  4. Change the word pick to squash on the line that corresponds to the third commit.

    pick 1a2b3c4 First commit
    squash 3a4b5c6 Third commit
    pick 2a3b4c5 Second commit
    
  5. Save and close the editor.

  6. Git will combine the changes from the first and third commits into a single commit. We will be prompted to enter a new commit message for the merged commit. Edit the message as desired and save it.

  7. Once the merge commit message is saved, the interactive rebase process will complete and the new commit will be added to the repository.

The result will be two commits, the first with the changes from the first and third commits, and the second with the changes from the second commit.

Conclusion

This is a compelling tool that can be used to change the history of our repository. It's important to understand how it works and how to use it properly. If we are not confident, we can always run git reflog to see the history of our repository. This will show us the history of all the commits, even if they've been removed from the repository.

Top comments (0)