I'm working on a branch where I have changed file A in several commits and now I want to revert all changes from it so that file A's state is same as initial state it had when I had first created the branch. What is the easiest way to achieve this?
To revert multiple commits in the middle of the history, use an interactive rebase. Find the last commit's hash containing all the commits you want to remove. Start an interactive rebase session with git rebase -i <hash>. In the interactive rebase edit screen, remove the commit lines you want to remove.
You can follow this procedure: git revert -n <*commit*> ( -n revert all the changes but won't commit them) git add <*filename*> (name of the file/s you want to revert & commit) git commit -m 'reverted message' (add a message for reverting)
First, you'll need to find the ID of the revision you want to see. This assumes that you're developing on the default main branch. Once you're back in the main branch, you can use either git revert or git reset to undo any undesired changes.
The git revert command is a forward-moving undo operation that offers a safe method of undoing changes. Instead of deleting or orphaning commits in the commit history, a revert will create a new commit that inverses the changes specified. Git revert is a safer alternative to git reset in regards to losing work.
git checkout <sha1_of_commit> file/to/restore
It will revert file to state after <sha1_of_commit>
commit.
If you want to revert it to state before this commit use
git checkout <sha1_of_commit>~1 file/to/restore
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With