How to edit a commit already pushed?
How can I delete a comment or print, remove one extra line or just organize my changes on a commit already pushed? In this post I'll write about how to edit a commit already pushed using git rebase.
Git Rebase
The command git rebase is very useful when we talk about edit or organize your commits. Not only for edit, you can reorder your commits, delete or squash then, and also define a new branch to be the base for your currently branch, catching the new changes. In this post we will focus only on how to edit a commit already pushed, but the same process can be done to the others functions.
Editing your commit
Follow the below steps to rewrite your commit:
- First of all you go to your folder that was cloned and you want to rewrite the commit;
- After this, you must to run the command
git rebase -i HEAD~1, but instead use the 1 value, use the number of commits that you want to go back. For example: the repository log in the image below shows two commits, if I want to rewrite the commit Update README.md, I must to use the value 2; - When you run the command, you'll see the options that rebase can offer, like bellow, and you can change for the option edit;
- You can see on your code that all the changes will be the ones that you made on that commit, not having the changes from commits ahead. After this, let this terminal window open and change your code;
- Add your changes with
git add .and later close the rebase process withgit rebase --continue - Don't forget to push your changes. Will be necessary to force then, so you can use
git push -f.
If you don't want to save the changes made, you can abort the process with git rebase --abort.
Like was said before, a lot of options can be done with git rebase. This command is very nice to clean your code of changes that aren't necessary about logic, but wrong comments and empty lines. But of course, can be applied in a lot of situations.


Comments
Post a Comment