-1

I've screw up my code, and sadlly I've made several commit already and found a bug which looks like a myth to fix, so the only solusion should be to get the copy I uploaded to github earlier (which is not a screwed up version). (Oh thanks the GREAT GITHUB! )

I tried run git clone https://github.com/username/Spoon-Knife.git

    remote: Counting objects: 253, done.
    remote: Compressing objects: 100% (150/150), done.
    Receiving objects:  64% (162/253), 20.00 Kremote: Total 253 (delta 119), reused
    Receiving objects:  66% (167/253), 20.00 KiB | 15 KiB/s
    Receiving objects: 100% (253/253), 60.99 KiB | 15 KiB/s, done.
    Resolving deltas: 100% (119/119), done.

but it seems not working. Some files remained unchanged. How do I deal with it? to pull from the remote github version?

ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
  • This question has been answered a thousand times on here – Jason Jun 01 '13 at 05:02
  • Are you checking out a fresh copy or somehow trying to revert the copy with unwanted commits? A fresh copy should obviously only contain what's in the repo you are cloning. – tripleee Jun 01 '13 at 05:05
  • 1
    @Jason, but I didnt find the answer through google. I dont know what is the right word for pull/rollback – ZK Zhao Jun 01 '13 at 05:10
  • @tripleee, Well.... I just run the command in the existing code file(not a new one). And the screwed-up code is not changed after run `git clone` – ZK Zhao Jun 01 '13 at 05:12
  • http://stackoverflow.com/search?q=reverse+git+commit – Jason Jun 01 '13 at 05:13
  • http://stackoverflow.com/questions/1809484/git-how-to-reverse-merge-a-commit – Jason Jun 01 '13 at 05:14
  • @jason, its not reverse git commit at all. Maybe I didnt get my idea across. I just want to replace my local code with the one in github. – ZK Zhao Jun 01 '13 at 05:15
  • You could roll back your local branch to the hash of the commit on github using git reset commit – Jason Jun 01 '13 at 05:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31019/discussion-between-cqcn1991-and-jason) – ZK Zhao Jun 01 '13 at 05:20

2 Answers2

6

You can do this:

git reset --hard <old-commit-id>
git push -f
1

In case if you made one bad commit and after it one or more commits that you need, I would prefer to use interactive rebase. For example you push three commits, first one is crappy and two others you need. You need to start interactive rebase with a command

git rebase -i HEAD~3

-i means that rebase is interactive

HEAD~3 means that you will work with three last commits.

Running this command text editor will be opened with text like this

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

You need to remove first line with crappy commit, and exit from text editor. After finishing rebase all changes that were committed by mistake will be removed.

Check if commits history is ok. If so, then run following command

git push -f

This command will move branch pointer on your last commit.

More information about interactive rebase you can get here http://git-scm.com/book/en/Git-Tools-Rewriting-History

user2399170
  • 622
  • 7
  • 11