Skip to content

Instantly share code, notes, and snippets.

@jasonadkison
Created August 4, 2023 20:48
Show Gist options
  • Save jasonadkison/de4eb39716b18dcf2e537fa3fba8e1ea to your computer and use it in GitHub Desktop.
Save jasonadkison/de4eb39716b18dcf2e537fa3fba8e1ea to your computer and use it in GitHub Desktop.
Rebase a commit
  1. Open Visual Studio Code (VSCode) and navigate to the repository you want to work with.

  2. Make sure you are on the branch that contains the commit you want to edit. You can use git checkout <branch-name> to switch to the desired branch.

  3. Find the commit hash of the commit you want to edit by using git log.

  4. Start an interactive rebase using the following command:

    git rebase -i <commit-hash>^
    

    Replace <commit-hash> with the hash of the commit you want to edit. Note the ^ symbol at the end of the commit hash; it is essential for including the specified commit in the rebase.

  5. Git will open a file in the editor that shows a list of commits starting from the commit you want to edit. Mark the line for the commit you want to edit with the word "edit."

  6. Save and close the editor.

  7. Now, Git will apply all the commits in the list, including the one you marked for editing. It will then pause at the commit you want to modify.

  8. Make the necessary changes to your files as needed using VSCode or any other editor.

  9. After making the changes, stage them using git add .

  10. Amend the commit with the new changes using the following command:

    git commit --amend --no-edit
    

The --no-edit flag will retain the same commit message.

  1. Continue the rebase using the following command:

    git rebase --continue
    
  2. If there are any conflicts during the rebase, Git will pause and ask you to resolve them manually. Use git add to stage the resolved changes, and then use git rebase --continue to continue the rebase process.

  3. After all the commits are successfully applied, you might need to force push the branch to the remote repository (origin) since you've rewritten the commit history. Use the following command:

    git push origin <branch-name> --force
    

Replace <branch-name> with the name of the branch you are working on.

That's it! You have now edited the specific commit in the Git history and rewritten the commit history with the changes. Keep in mind that rewriting history can cause issues for other collaborators, so use it with caution and communicate with your team if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment