227

Is there a possibility to revert a committed file in Git? I've pushed a commit to GitHub and then I realized that there's a file which I didn't want to be pushed (I haven't finished the changes).

Cœur
  • 37,241
  • 25
  • 195
  • 267
kmaci
  • 3,094
  • 2
  • 18
  • 28
  • 1
    Do you want to remove the file completly from GitHub after a push? Otherwise you just: git rm , git push. – rdrmntn Aug 21 '13 at 12:35
  • http://stackoverflow.com/questions/2466735/checkout-only-one-file-from-git – René Höhle Aug 21 '13 at 12:38
  • Possible duplicate of [How can I remove a commit on github?](http://stackoverflow.com/questions/448919/how-can-i-remove-a-commit-on-github) and [How to undo the last Git commit?](http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit). –  Aug 21 '13 at 12:58

10 Answers10

267

update: added safer method

preferred method:

  1. check out the previous (unchanged) state of your file; notice the double dash

    git checkout HEAD^ -- /path/to/file
    
  2. commit it:

    git commit -am "revert changes on this file, not finished with it yet"
    
  3. push it, no force needed:

    git push
    
  4. get back to your unfinished work, again do (3 times arrow up):

    git checkout HEAD^ -- /path/to/file
    

effectively 'uncommitting':

To modify the last commit of the repository HEAD, obfuscating your accidentally pushed work, while potentially running into a conflict with your colleague who may have pulled it already, and who will grow grey hair and lose lots of time trying to reconcile his local branch head with the central one:

To remove file change from last commit:

  1. to revert the file to the state before the last commit, do:

    git checkout HEAD^ /path/to/file
    
  2. to update the last commit with the reverted file, do:

    git commit --amend
    
  3. to push the updated commit to the repo, do:

    git push -f
    

Really, consider using the preferred method mentioned before.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Frido Emans
  • 5,120
  • 2
  • 27
  • 42
  • 2
    Unless you're the only developer on a project, you really shouldn't use git push -f. It creates way more problems than it's worth. Just remove the file then do a new commit. – Richard Morgan Oct 13 '17 at 21:10
  • Is this doable with commits further back in history? E.g. Could I do this with `git checkout HEAD~2 /path/to/file`? Edit: Looks like what I wanted in my case was simply `git rm /path/to/file` – starscream_disco_party Oct 10 '18 at 16:52
  • 1
    Using zsh on my mac and I had to change the command to `git checkout HEAD~ /path/to/file` to avoid having to escape. – whatapalaver Mar 15 '21 at 17:30
  • 1
    Can someone please explain what does `HEAD^` mean? – alumi Nov 12 '22 at 06:36
  • 1
    HEAD^ means the first parent of the tip of the current branch. (From: https://stackoverflow.com/a/1956054/2380702) – Frido Emans Dec 20 '22 at 09:40
178

If you want to remove the file from the remote repo, first remove it from your project with --cache option and then push it:

git rm --cached /path/to/file
git commit -am "Remove file"
git push

(This works even if the file was added to the remote repo some commits ago) Remember to add to .gitignore the file extensions that you don't want to push.

Aks
  • 7,469
  • 1
  • 12
  • 11
micoru
  • 1,905
  • 1
  • 10
  • 6
  • 13
    This does remove the files from this commit forward, but the files are still visible within the older commits. – Ken Mar 13 '19 at 10:47
  • 1
    I like because it removes files only for git also when .gitignore doesn't works fine. I want to say something: if you need remove a lot of files, you can use the next commands: git rm -r --cache /path/to/file As you can see i added the "-r" optional command to make the work recursively. Thanks a lot @micoru <3 – Richard Rebeco Jun 01 '21 at 19:52
  • 1
    Use `git rm --cached /path/to/file` then add, commit and push. Just like above answer, but you need to use `--cached` not `--cache`. – J Olson Feb 08 '22 at 23:23
  • 1
    Note: Following these steps will remove (delete) the file from the master when merged. So keep a note of this. if you removed `xyz.html`, then this file won't exist on server if it was intended for. – Deepak Yadav Sep 01 '22 at 15:29
59

You can revert only one file to a specified revision.

First you can check on which commits the file was changed.

git log path/to/file.txt

Then you can checkout the file with the revision number.

git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /path/to/file.txt

After that you can commit and push it again.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • wow thank you, i accidentally committed and pushed something and your answer was a life saver :) – dev_ios999 Jan 29 '21 at 02:39
  • I was doing this like you said and couldn't think of why it wasn't working...then I realized I had to get the previous commit that I wanted to roll back to not the one I just committed. I don't know why I wasn't thinking of that. I would be curious why 3 people voted down on this answer because it works as intended. – Rich Jul 28 '23 at 17:06
7

You can use the following workflow:

git reset --soft HEAD~1
git reset HEAD /path/to/file
5

Reset the file in a correct state, commit, and push again.

If you're sure nobody else has fetched your changes yet, you can use --amend when committing, to modify your previous commit (i.e. rewrite history), and then push. I think you'll have to use the -f option when pushing, to force the push, though.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
5
  1. Get the hash code of last commit.

    • git log
  2. Revert the commit
    • git revert <hash_code_from_git_log>
  3. Push the changes
    • git push

check out in the GHR. you might get what ever you need, hope you this is useful

Jacob Boertjes
  • 963
  • 5
  • 20
krish babu
  • 59
  • 1
  • 1
5

Use git rm --cached /path/to/file then add, commit and push. Just like above answer, but you need to use --cached not --cache.

J Olson
  • 167
  • 2
  • 8
0

I followed these steps [it always happens to me especially with the node modules folder]

1- git log

2- git revert 04409a858709c82fbf9eae7559d6ae9c34b6fbe6

change the number with your committed one

<it will be long numbers like this 04409a858709c82fbf9eae7559d6ae9c34b6fbe6>

3-git push

This worked for me, but keep in mind that, there is a trick here your first commit will be the last one in the list so check the dates and the commits message.

check this image from my terminal

  • 1
    This is the same guidance as [@krush babu's 2018 answer](https://stackoverflow.com/a/51254309/3025856), but with some additional commentary. It would be better suited as a comment on that answer and not a new answer. (Acknowledging that you don't yet have enough reputation to upvote answers.) – Jeremy Caney Dec 14 '22 at 00:48
0

This is what I do in such case (simple)

  1. Go to the git GUI (it is supposed to be in your IDE, next to the terminal).
  2. Go to your log tab and search for the relevant commit the file is in it.
  3. On the right side there should be a list of all your pushed files.
  4. Right click on the requested file and select the revert option.
  5. Commit and push, the file will be no longer there since there are no changes (its first status and last status are the same)
-1

I would do the following, say if the branch from which you have made you branch is 'develop'

Do :

git checkout develop /path/to/file

You can find path to file by making a small change in that file and do git status