简体   繁体   中英

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.

您可以重置为HEAD: git reset --hard HEAD

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

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 add ed).

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.

由于您尚未提交任何内容,以下命令将重置您修改的文件:

git checkout .

you can try

git stash
git pull origin your-branch

你试过git checkout -f master吗?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM