Home > Enterprise >  How to "Un-push" a file in git?
How to "Un-push" a file in git?

Time:11-11

Hello I'm very new to Git. I'm working on my branch, and I accidentally committed and pushed a file that I modified (but do not want in my pull request). For example: I pushed fileA.py, fileB.py, fileC.py. However I only want fileA.py and fileB.py in the pull request.

From

- FileA.py
- FileB.py
- FileC.py

To

- FileA.py
- FileB.py

Most of the answers I have found involve deleting fileC.py. I don't want to actually delete fileC.py, I still want it in the repo just the unmodified version as in master. Is there a git command to achieve this? Thank you for any help!

CodePudding user response:

WARNING: Since you mentioned a pull request I am assuming that you are pushing to a separate branch that nobody else is working on other than you. Otherwise, doing a force push is usually discouraged.


Let's assume some state:

touch fileA.py fileB.py fileC.py
git add .
git commit -m "my message"
git push
$ ls
fileA.py*  fileB.py*  fileC.py*  
$ git ll
* 537443d - (HEAD -> my-branch) my message (9 seconds ago) <Aleksander Stelmaczonek>
  0     0       fileA.py
  0     0       fileB.py
  0     0       fileC.py  

Note: git ll is my custom command alias, see the definition at the end.


To remove the fileC.py from the commit and yet leave it in the working dir you can use git rm command and then amend your commit and re-push it (force push) replacing the original commit with an ammended one. Please, note that by default this command also removes the file from the disk and you need to use --cached option.

$ git rm -h
usage: git rm [<options>] [--] <file>...

    -n, --dry-run         dry run
    -q, --quiet           do not list removed files
    --cached              only remove from the index
    -f, --force           override the up-to-date check
    -r                    allow recursive removal
    --ignore-unmatch      exit with a zero status even if nothing matched

Now:

$ git rm --cached fileC.py
rm 'fileC.py'

$ git status
On branch my-branch
Changes to be committed:
        deleted:    fileC.py

Untracked files:
        fileC.py

$ git commit --amend -m "my message"
[my-branch beebdc1] my message
 Author: Aleksander Stelmaczonek <[email protected]>
 Date: Wed Nov 10 18:25:37 2021  0100
 2 files changed, 0 insertions( ), 0 deletions(-)
 create mode 100644 fileA.py
 create mode 100644 fileB.py

$ git ll
941acb2 - (HEAD -> my-branch) my message (13 seconds ago) <Aleksander Stelmaczonek>
  0     0       fileA.py
  0     0       fileB.py

$ ls
fileA.py*  fileB.py*  fileC.py*

$ git push --force

I would like strongly emphasize that doing a force push is a really bad idea if there is more than one person working on this branch. Potential issues are not so easy to resolve, especially for a novice.

Generally speaking, I highly recommend reading the Git Book. It's free and well written. For novices, worth reading are chapters 1, 2, 3, 7.1, 7.2, 7.3, and 7.7.


Personally, for all stuff related to creating and modifying commits I usually just use a GUI. Find out what GUI tool suits you best. I use https://git-fork.com/.


This is how you can enhance your git experience with my git ll command (and others):

git config --global alias.ll "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)            
  • Related