This article will cover various methods of rewriting and altering Git history. Git uses a few different methods to record changes. Now we will discuss the git commit –amend command with one real example.
Imagine this. You fixed some bugs in more than one file and moved these files to the staging area. You missed some files to move. you want these changes to come in one commit. But some changes were not added to this commitment because some changes were not performed to the staging area. Later you see these few changes, not in the git repository.
If these changes are also required in that commit, how to do that?
In the above image as you can see style.css file was added to the staging area and it had moved to the git repository also. But there is one file to move to the staging area. We need this file with the same commit, how to do that?
1. Move this file index.html to the staging area.
git add .
git commit –amend
So, you can add more than one change in one commit.
Now let’s see if any commit is unwanted, how to remove it from the project timeline? Or commit history? There are two ways.
1. Delete from commit history. How to do that? Let’s see.
In the above image, we don’t need the last commit. How to delete from commit history.
git reset HEAD~1
After deleting this commit from the project timeline or commit history. It only appears in the working directory.
After, we undo the working directory using these commands.
git reset HEAD –hard
git clean -fd
But this method is not preferred.
2. Discard the changes from that commit and create new changes in that commit.
So let’s see how to do that with one example. I will add the previous bug fixing commit again.
Then, discard previous changes.
So this bug fixing commit was removed and a new commit was created to revert bug fixing. Within this method, we don’t delete commits from the timeline. Instead of this we can discard the changes from the commit history and add new changes in the revert commit.
In the next chapter, we will discuss Development Branches. I hope this is useful for all of us. If you want to interested in watching more videos about git. Please watch Appslanka git videos on our youtube channel. https://www.youtube.com/playlist?list=PLZmKantulzuSeqMy-zM56ZC0S7watreeu. Don't forget to subscribe.