Git commands are widely used to maintain versioning of the project across Github, Gitlab, Bitbucket, etc. Once in a while, a situation arises where we want to make some modification to the existing particular commit instead of creating a new commit. In this article, we will see how to change a particular commit from a list of commits. Let’s start without any further ado.
Let’s suppose we have pushed 4 commits previously now we want to modify commit number three. Suppose we have these four commits with the following message and hash.
- 4abcd 4th commit message (this is a recent commit)
- 3abcd 3rd commit message
- 2abcd 2nd commit message
- 1abcd 1st commit message
Now we want to edit third commit which will be (2abcd) .
In step 1 we have to copy the commit hash which will the previous commit of the commit we want to modify. In this case, the hash of the commit will be (1abcd).
git rebase -i 1abcd
Now above command will open up a vim editor where you will see all commits above 1abcd, something like this
- pick 3abcd 3rd commit mesSage
- pick 2abcd 2nd commit meSsage
Now we will replace pick word with edit word right in front of the 2abcd. After this, it will look like something
- pick 3abcd 3rd commit meSsage
- edit 2abcd 2nd commit message
Now press CTRL+X then it will ask whether you want to save the changes or not, enter Y, and then finally press the enter button to exit the editor.
Now Lets go into the code and make required changes.
Now stage and commit your changes.
git add .
If you do not want to change commit message run the following command.
git commit --amend
If you want to change your commit message as well then run the following command.
git commit --amend -m "your new commit message here"
Now run the following command to make sure everything is up to date and apply other commits over this commit.
git rebase --continue
Now you are done with the changes and commit, now all remaining is one command and you know what is, yes its
git push -f
To assure you changes run the following commands see all commits in one line.
git log --oneline
That’s all about how to change a particular commit from a list of commits. Hope this article will help you if you have any comment and questions please leave a comment in the below comment box.
- how to scrap data using Node JS
- Creating a simple REST API using NodeJS and ExpressJS
- How to get and compare the previous state using useEffect Hooks
- When to use functional component over class component in ReactJS