Mikey
Mikey

Reputation: 4742

Git: how to roll back to last push/commit

I am programming. I add beautiful code and commit and push it to my repo like:

git add *
git commit
//writes message
git push unfuddle master

Now i go in and screw everything up. I have not issued a git command since I push the beauty. How do i make my local files go back to what was committed?

git pull says my local repo is "up to date"

git checkout spams out my screen and doesnt seem to change anything.

To be clear: I am not trying to modify my remote repo, just get my local repo to look like the server.

My current solution is to delete the folder and reclone the repo. That can't be right.

Upvotes: 35

Views: 42593

Answers (8)

sjcoder
sjcoder

Reputation: 181

git checkout branchname git reset --hard c4e0424 git push origin +branchname

You can use --soft instead of --hard
--hard you will LOSE your changes
--soft leave your changed files of commit

Upvotes: 0

Adam Dymitruk
Adam Dymitruk

Reputation: 129584

UPDATE:

The best way to do this is to

git stash -u

Do not

git reset --hard HEAD

it is an operation that cannot be undone and you can truly nuke your work unless you're using time machine on a Mac or the new backup service on Windows 8. Linux may have a scheme like that although I don't use one.

If you also want ignored files nuked, you can

git clean -xdf

Upvotes: 23

tonny zhang
tonny zhang

Reputation: 31

you can try

git stash
git pull origin your-branch

Upvotes: 2

ralphtheninja
ralphtheninja

Reputation: 133008

Since you haven't committed anything yet, the following command will reset your modified files:

git checkout .

Upvotes: 8

simont
simont

Reputation: 72557

git pull says my local repo is "up to date"

git pull is telling you your repository is up to date because your local repository and your remote repository both point to the same HEAD (the last commit). This is due to your git push, which synced the remote with your local repository.

Git doesn't compare the changes that haven't been committed to the remote branch when it decides what to pull; thus, from Gits point of view, your local and remote repositories are at the same point in time, even though your local repository has unstaged changes (changes that you have not git added).

There's a nice diagram on the bottom of this page which shows you how the process works - you make some changes, stage them (by running git add), and then finally commit them to the repository (through the creatively named git commit).

To be clear: I am not trying to modify my remote repo, just get my local repo to look like the server.

Others have pointed out the methods to do this: git reset --hard HEAD (reset the index [staged files] and the working tree [unstaged files] to the last commit), but it's worth having an understanding of what you're trying to achieve - if I'd known how git was tracking my files the first time I messed up my working tree, I'd have saved hours of fretting.

Upvotes: 3

Rodrigo Flores
Rodrigo Flores

Reputation: 2461

If you does not have committed anything, you can simply do a $ git reset --hard. If you have committed some stuff, you can do a git reset --hard origin/master to go back to the version that is on the remote.

Upvotes: 1

jjlin
jjlin

Reputation: 4703

Did you try git checkout -f master?

Upvotes: 1

three
three

Reputation: 8478

You can reset to HEAD: git reset --hard HEAD

Upvotes: 28

Related Questions