Git: Remove committed file after push
Asked Answered
B

10

237

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).

Behm answered 21/8, 2013 at 12:31 Comment(3)
Do you want to remove the file completly from GitHub after a push? Otherwise you just: git rm <file>, git push.Wedurn
stackoverflow.com/questions/2466735/…Solmization
Possible duplicate of How can I remove a commit on github? and How to undo the last Git commit?.Lay
B
279

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.

Benediction answered 26/2, 2014 at 12:25 Comment(6)
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.Circumvent
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/fileDrysalter
Using zsh on my mac and I had to change the command to git checkout HEAD~ /path/to/file to avoid having to escape.Scarberry
Can someone please explain what does HEAD^ mean?Overpower
HEAD^ means the first parent of the tip of the current branch. (From: https://mcmap.net/q/12347/-what-does-the-caret-character-mean-in-git)Benediction
When to do this: as first aid when you just uploaded a file with a password.Hengelo
D
186

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.

Dunnite answered 19/7, 2018 at 8:33 Comment(4)
This does remove the files from this commit forward, but the files are still visible within the older commits.Bogoch
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 @Dunnite <3Bixby
Use git rm --cached /path/to/file then add, commit and push. Just like above answer, but you need to use --cached not --cache.Rubber
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.Bumblebee
M
66

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.

Mcelroy answered 21/8, 2013 at 12:36 Comment(3)
wow thank you, i accidentally committed and pushed something and your answer was a life saver :)Garlic
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.Faison
Thank you very much, this worked like a charm.Deserted
M
9

You can use the following workflow:

git reset --soft HEAD~1
git reset HEAD /path/to/file
Menon answered 2/7, 2021 at 13:21 Comment(1)
then after that what do we do ?Confirmed
S
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.

Schwarz answered 21/8, 2013 at 12:36 Comment(0)
J
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

Jacklighter answered 9/7, 2018 at 21:31 Comment(0)
R
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.

Rubber answered 18/6, 2021 at 14:19 Comment(1)
This should have been a comment or an edit to the referenced answer.Takeoff
C
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

Corinthian answered 10/12, 2022 at 4:45 Comment(1)
This is the same guidance as @krush babu's 2018 answer, 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.)Vitia
E
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)
Emulsion answered 3/8, 2023 at 9:26 Comment(0)
S
-2

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

Switcheroo answered 13/5, 2023 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.