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

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

3 Answers3

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.

Niko Sams
  • 4,304
  • 3
  • 25
  • 44
  • 2
    Then the interactive rebase is the way to go. – Niko Sams Oct 09 '12 at 19:21
  • 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. – Kevin Meredith Oct 09 '12 at 19:28
  • 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. – cjc343 Oct 09 '12 at 19:41
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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
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
cjc343
  • 3,735
  • 1
  • 29
  • 34
  • 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. – Kevin Meredith Oct 09 '12 at 18:51
  • Assuming `upstream/master` contains the version you want, `git checkout upstream/master A.java` will get you the original version. – cjc343 Oct 09 '12 at 18:54
  • Can I just replace "upstream/master" with the https://... link to the repo that I cloned? – Kevin Meredith Oct 09 '12 at 19:01
  • 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. – cjc343 Oct 09 '12 at 19:05
  • Can you please tell me what 'upstream master' means? – Kevin Meredith Oct 09 '12 at 19:15
  • `upstream` is often used to refer to a repo you've forked from. It is often used as the remote name for a repository you've forked, but there's nothing that requires you to. You can replace `upstream` with anything you'd like in the `remote add` command. `master` is the branch name you want. It is often the default/primary branch. If you forked a different branch, you would want to continue to use that branch, not `master`. More on upstream/downstream: http://stackoverflow.com/questions/2739376/definition-of-downstream-and-upstream – cjc343 Oct 09 '12 at 19:24