Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Remove committed file after push

Tags:

git

github

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

like image 295
kmaci Avatar asked Sep 25 '22 07:09

kmaci


People also ask

Can you undo commit after push?

If you have a commit that has been pushed into the remote branch, you need to revert it. Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line.

How do I remove a commit in git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.


Video Answer


1 Answers

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.

like image 230
xor Avatar answered Oct 10 '22 09:10

xor