Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo changes in a file after multiple commits in Git?

Tags:

git

git-revert

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?

like image 842
smohadjer Avatar asked May 15 '15 08:05

smohadjer


People also ask

Can I git revert multiple commits?

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.

How do you revert changes in file after commit?

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)

How do I revert changes in git?

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.

Can we undo changes after commit?

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.


1 Answers

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
like image 200
Domen Blenkuš Avatar answered Oct 05 '22 23:10

Domen Blenkuš