Revert changes to a file in a commit

GitVersion ControlRevert

Git Problem Overview


I want to revert changes made by a particular commit to a given file only.

Can I use git revert command for that?

Any other simple way to do it?

Git Solutions


Solution 1 - Git

The cleanest way I've seen of doing this is described here

git show some_commit_sha1 -- some_file.c | git apply -R

Similar to VonC's response but using git show and git apply.

Solution 2 - Git

Assuming it is ok to change the commit history, here's a workflow to revert changes in a single file in an earlier commit:

For example, you want to revert changes in 1 file (badfile.txt) in commit aaa222:

aaa333 Good commit
aaa222 Problem commit containing badfile.txt
aaa111 Base commit

Rebase on the base commit, amend the problem commit, & continue.

  1. Start interactive rebase:

    git rebase -i aaa111

  2. Mark the problem commit for edit in the editor by changing pick to e (for edit):

    e aaa222 pick aaa333

  3. Revert changes to the bad file:

    git show -- badfile.txt | git apply -R

  4. Add the changes & amend the commit:

    git add badfile.txt git commit --amend

  5. Finish the rebase:

    git rebase --continue

Solution 3 - Git

git revert is for all file contents within a commits.

For a single file, you can script it:

#!/bin/bash

function output_help {
    echo "usage: git-revert-single-file <sha1> <file>"
}

sha1=$1
file=$2

if [[ $sha1 ]]; then
git diff $sha1..$sha1^ -- $file | patch -p1
else
output_help
fi

(From the git-shell-scripts utilities from smtlaissezfaire)


Note:

another way is described here if you have yet to commit your current modification.

git checkout -- filename

git checkout has some options for a file, modifying the file from HEAD, overwriting your change.


Dropped.on.Caprica mentions in the comments:

> You can add an alias to git so you can do git revert-file <hash> <file-loc> and have that specific file be reverted.
See this gist.

[alias]
  revert-file = !sh /home/some-user/git-file-revert.sh

Solution 4 - Git

Much simpler:

git reset HEAD^ path/to/file/to/revert

then

git commit --amend   

and then

git push -f

the file is gone and commit hash, message, etc is the same.

Solution 5 - Git

I would simply use the --no-commit option to git-revert and then remove the files you don't want reverted from the index before finally committing it. Here's an example showing how to easily revert just the changes to foo.c in the second most recent commit:

$ git revert --no-commit HEAD~1
$ git reset HEAD
$ git add foo.c
$ git commit -m "Reverting recent change to foo.c"
$ git reset --hard HEAD

The first git-reset "unstages" all files, so that we can then add back just the one file we want reverted. The final git-reset --hard gets rid of the remaining file reverts that we don't want to keep.

Solution 6 - Git

git reset HEAD^ path/to/file/to/revert/in/commit

The above command will take file out of commit, but it will reflect in git status.

git checkout path/to/file/to/revert/in/commit

The above command will revert the changes (as a result you get file same as HEAD).

git commit

(Pass --amend to amend commit.)

git push

With this, the file which is already in the commit is removed and reverted.

The above steps should be followed from the the branch where the commit is made.

Solution 7 - Git

If you'd like to reset the changes on a file from your last commit, this is what I'm usually using. I think this is the simplest solution.

Please note that the file will be added to the staging area.

git checkout <prev_commit_hash> -- <path_to_your_file>

Hope it helps :)

Solution 8 - Git

You can follow this procedure:

  1. git revert -n <*commit*> (-n revert all the changes but won't commit them)
  2. git add <*filename*> (name of the file/s you want to revert & commit)
  3. git commit -m 'reverted message' (add a message for reverting)
  4. after committing discard the other files changes so the files stay updated with the changes you committed before the revert

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlprsdView Question on Stackoverflow
Solution 1 - GitmgalgsView Answer on Stackoverflow
Solution 2 - GitteeView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow
Solution 4 - GitForrestView Answer on Stackoverflow
Solution 5 - GitDan MouldingView Answer on Stackoverflow
Solution 6 - GitBharath T SView Answer on Stackoverflow
Solution 7 - GitGabeekaView Answer on Stackoverflow
Solution 8 - GitImran SahilView Answer on Stackoverflow