0

I made a commit 1st yesterday and second commit today in single PR .

but later I found that I added extra file in first commit and want to delete.

So I revert commit using following command

git reset --soft HEAD~2 

and

git rm --cached apk/local_setting.py 

but I want to ask that how to push those file back to remote without changing previous commits?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Sanwar lal
  • 17
  • 6
  • commit should be back in same branch – Sanwar lal Jun 16 '21 at 11:45
  • 1
    If it the end result you just want `apk/local_setting.py` removed in a new commit that removes it, which is your only option if you don't want to change previous commits, do `git rm apk/local_setting.py` and commit that. – joanis Jun 16 '21 at 12:07
  • You have not done anything to the remote. The only thing that travels to the remote is _commits_. You have not committed or pushed. – matt Jun 16 '21 at 12:07

1 Answers1

0

Probably you are talking about git revert command documented at https://git-scm.com/docs/git-revert and at https://www.atlassian.com/git/tutorials/undoing-changes/git-revert .

For example:

git revert {{commit-hash}}

You can also add the --no-commit option in order to prevent the automatic commit, letting you the opportunity to choose which changes should be reverted. After that you have to issue a git commit command to accept reverted changes.

That command produces a new commit witch invalidate the commit you selected, but leaving the history untouched.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • How the revert is different from git reset --soft – Sanwar lal Jun 16 '21 at 11:49
  • As explained at https://www.atlassian.com/git/tutorials/undoing-changes/git-revert : "It's important to understand that git revert undoes a single commit—it does not "revert" back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert.". And as I wrote in my answer, `git revert` produces and new commit which keep intanct the past history of the project. – Antonio Petricca Jun 16 '21 at 11:54
  • https://stackoverflow.com/a/8358039/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+difference+reset+revert – phd Jun 16 '21 at 12:01