2

I'd like to un-commit changes against which I've run "git commit."

In particular, I'd like to specify the files.

Example:

> #5 commits ago 
> commit -m "bug fix" A.java
> 
> #bunch of commits occurred
> 
> [command to uncommit] A.java

Note I do not want to remove A.java from my repo. I just want to return it to its base state, i.e. when I had run git clone repo.

Thanks, Kevin

2

3 Answers 3

3

Be careful with rebasing, if you have pushed your commit already you'll have to force push and will probably run into troubles unless you know what you are doing.

Use git revert $SHAYOUWNATOREVERT to revert the commit you don't want anymore. This will basically create an "inverted" commit that undoes the given one.

Sign up to request clarification or add additional context in comments.

3 Comments

Then the interactive rebase is the way to go.
So when I run "git push origin," can I append a SHA to only push the SHA's that I'd like to push? Some of my git commit's should not be pushed to master.
If nothing has been pushed, I'd agree with @hvgotcodes' answer as well. I have been answering under the assumption that you both committed and pushed your changes. Rebase away if you haven't pushed yet.
2

You can do a rebase interactive and remove the commit. It might not work if later commits depend on that commit though.

git rebase -i HEAD~5

and just delete the appropriate line, then save and exit.

DO NOT do this if you have pushed to remote.

Comments

2

Unless A.java contains private data that cannot exist in the repo history, you should commit a deletion of the file, not try to remove its previous existence.

git rm A.java

If the file contains sensitive data which must be removed, it is possible to remove the history of the file, but it will rewrite refs, require a forced push, and require extra work on the part of anyone who has cloned or pulled your current refs.

https://help.github.com/articles/remove-sensitive-data

In short:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch A.java' --prune-empty --tag-name-filter cat -- --all

Edit to incorporate answer if trying to restore a version from another repo or branch:

If A.java is a file that you've modified, and you wish to return it to the original version that is available at upstream/master then this will replace your current copy:

git checkout upstream/master A.java

6 Comments

I do not want to remove A.java from the git repo though. After cloning a repo, I then modified and committed A.java. Now, I'd like to undo those changes and revert/return A.java to its original, cloned version.
Assuming upstream/master contains the version you want, git checkout upstream/master A.java will get you the original version.
Can I just replace "upstream/master" with the https://... link to the repo that I cloned?
Not as far as I know. git remote add upstream https://... will make the checkout command work assuming master exists upstream and contains the version you want.
Can you please tell me what 'upstream master' means?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.