Jake
Jake

Reputation: 1380

How to remove a file from pull request/commit?

My gitignore file looks like this:

# Ignore bundler config.
 /.bundle

# Ignore all logfiles and tempfiles.
/log/*
!/log/.keep
/tmp
coverage/
public/system/
.DS_Store
.env
logfile
development.log

For whatever reason I keep ending up with my development.log file present in my commits and in pull requests. Right now I have an open pull request with the full development.log file present. How do I remove it?

I've tried the following:

  1. git reset HEAD^ app/log/development.log
  2. git commit --amend --no-edit
  3. git push

End up with The current branch resource has no upstream branch. To push the current branch set the remote as upstream, use git push --set-upstream origin resource. I do that and then I check my PR and the file is still there because clearly it's a no-edit.

I've also tried

  1. git log, found the commit with the file
  2. git reset --soft commit
  3. git push

The problem here is that the file is present locally, and should be, but I want it stripped from the PR.

How do I remove a file from a pull request but not remove it locally?

Upvotes: 2

Views: 11228

Answers (3)

TacoEater
TacoEater

Reputation: 2278

If you want to keep the changes just move them to a different branch both of these work:

  1. rename the file in your IDE, commit and push the change without staging the renamed file, then checkout the correct branch, rename it back to what it was, stage, commit and push to the correct branch.

  2. git rm --cached <filepath> commit and push the change without staging the deleted file back, move to the correct branch, then stage, commit and push the file.

Upvotes: 0

infused
infused

Reputation: 24347

First remove the log file from the git index while leaving it on disk with the --cached option:

git rm --cached log/development.log

Then check in the file removal:

git commit -m 'remove development log'

Upvotes: 8

Gokul P
Gokul P

Reputation: 464

If you already pushed that file just delete the file and commit the deleted file and push to remote.

If only committed not pushed into remote just try git reset HEAD~1(The number 1 means reset one commit) remove the file and push to remote.

Upvotes: 0

Related Questions