user1436111
user1436111

Reputation: 2141

Git - rollback to previous commit

I'm working on a git project with a partner. I made some changes, then accidentally added and committed more files than I intended to and pushed them to the master repository. How do I rollback the remote repository to the last commit, BUT preserve my local copy so I can re-add and commit correctly?

Upvotes: 1

Views: 2692

Answers (2)

Roman
Roman

Reputation: 10403

Answer taken from here: How to undo last commit(s) in Git?

Undo a commit and redo

$ git commit ...              (1)
$ git reset --soft HEAD^      (2)
$ edit                        (3)
$ git add ....                (4)
$ git commit -c ORIG_HEAD     (5)
This is what you want to undo

This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".

Make corrections to working tree files.

Stage changes for commit.

"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.

Upvotes: 1

SLaks
SLaks

Reputation: 888107

You can tell git push to push the remote to a specific revision:

git push origin HEAD~1:master

Explanation:

  • origin is the name of the remote repo
  • HEAD~1 is the source-refspec – the revision to push. HEAD~1 means one commit behind the current local HEAD.
  • master is the target-refspec – the remote branch to push to.

Upvotes: 3

Related Questions