Once again, another git magic that might be of help to some of you. This research came up when I accidentally added a couple of unwanted files and wanted to remove them from a commit.
We all know that to update an existing commit, we shall follow this git magic to amend commits. However, how do we drop changes to a particular file in a commit?
While that is easy, it is also tricky. This blog post covers scenarios and respective commands that help you understand what should be done.
/**
* @disclaimer
* Please read this post fully before executing any command. My scenario might not be the same as yours.
*/
There are two scenarios to be handled here,
- Remove a newly added file
- Remove changes to an existing file
Remove a newly added file
1. Remove your file from the local folder
2. Stage and amend the commit
> git add config.json
> git commit --amend
Remove changes to an existing file
This is tricky but easy-to-solve scenario once you know it's doable. This is the scenario where there's a file already in the branch and you made some accidental changes to it and committed it. To remove the accidental changes and put it back to what it was before,
1. Checkout the file from the parent branch
Let's say you have made changes to webpack.config.js but don't need that change anymore and let's assume you are working in a branch that was created from an upstream/main branch.
Go to the root folder of your repository and execute the below command,
> git checkout upstream/main -- webpack.config.js
2. Stage and commit
> git add config.json
> git commit --amend
Tada! With this, we come to an end of this git flow. If there's anything that you might want me to add or discuss or if there's anything I've missed, please leave comments below.
Comments
Post a Comment