git Git恢复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9318431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:29:52  来源:igfitidea点击:

Git reverting back

git

提问by hobbes3

git logsays

git log

commit 2bf345e15dadba9f2e01ef2e0c3890d6cbab1153
Merge: d3b6ba8 aa94bf7
Author: Stack Overflow User <[email protected]>
Date:   Tue Feb 14 01:00:48 2012 -0500

    Merge branch 'master' of bitbucket.org:hobbes3/scoutmob

commit d3b6ba8f1d57c530c2d8ab9bb879fcac67d3de35
Author: Stack Overflow User <[email protected]>
Date:   Tue Feb 14 00:59:37 2012 -0500

    Revert "Added image info window"

    This reverts commit fad04be93334647d1b1e3b4d454a70819213ecac.

commit d119c3961da515c0cb2c6c9154003e52846cf848
Author: Stack Overflow User <[email protected]>
Date:   Tue Feb 14 00:22:56 2012 -0500

    Revert "Moved some files around to combine the common folder between scoutmob and ajax-example"

    This reverts commit 845f8d52c3655f3cf6b03da057281e5fb303721c.

commit aa94bf75fb064c6fe521501765d86c894aa5b436
Author: Another user <[email protected]>
Date:   Sat Feb 11 10:42:03 2012 -0500

    removing .txt

commit b633d23a5b10ad7531855905d6f41897ce6726ad
Author: Another user <[email protected]>
Date:   Sat Feb 11 10:40:04 2012 -0500

    testing data import

commit fad04be93334647d1b1e3b4d454a70819213ecac
Author: Stack Overflow User <[email protected]>
Date:   Sat Feb 11 03:55:54 2012 -0500

    Added image info window

commit 51f9cc7cd19f15540ac2631168a0a91c03a30139
Author: Stack Overflow User <[email protected]>
Date:   Tue Feb 7 22:29:58 2012 -0500

    Added deal info to the info window of the marker.

....

commit 845f8d52c3655f3cf6b03da057281e5fb303721c
Author: Stack Overflow User <[email protected]>
Date:   Tue Jan 31 10:42:21 2012 -0500

    Moved some files around to combine the common folder between scoutmob and ajax-example

'git status' says

'git status' 说

# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
nothing to commit (working directory clean)

git branch -rvsays

git branch -rv

origin/HEAD    -> origin/master
origin/master  aa94bf7 removing .txt
origin/working cb65aef My location now works. Will work on the graphics next.

A couple of days ago my master branch was at the most current commit at fad04be. Then I decided that I wanted to temporary look at my old code. So I reverted back by using git revert 845f8d5.

几天前,我的主分支在fad04be. 然后我决定暂时查看我的旧代码。所以我通过使用git revert 845f8d5.

In hindsight, I realized I should have checked out a new branch first. Then I tried to go back by using git revert 'fad04be. When I ran git statusit said something like Your branch is ahead of 'origin/master' by 1 commit.

事后看来,我意识到我应该先检查一个新的分支。然后我尝试使用git revert 'fad04be. 当我运行时,git status它说的是Your branch is ahead of 'origin/master' by 1 commit.

So next I tried to git pullfrom the remote repository to revert my current code back, and now I'm all confused.

所以接下来我尝试git pull从远程存储库恢复我当前的代码,现在我很困惑。

I just want both my local and remote repository to be on commit fad04be. What did I do wrong and what can I do?

我只希望我的本地和远程存储库都处于提交状态fad04be。我做错了什么,我能做什么?

回答by Claude

NOTE: before you follow any of my advice, make a full copy of your directory. That way you can always get back to the current situation

注意:在您遵循我的任何建议之前,请制作您目录的完整副本。这样你总能回到现在的状态

As ouah mentions indeed, your master should now be exactly what you want it to be, with two extra commits, namely the reverts of 845f8d5 and fad04be.

正如 ouah 确实提到的,您的 master 现在应该正是您想要的样子,有两个额外的提交,即 845f8d5 和 fad04be 的还原。

You can do 2 things now. If you didn't push anything to the remote yet, you can put the master pointer back to fad04be by

你现在可以做两件事。如果您还没有向遥控器推送任何内容,您可以通过以下方式将主指针放回 fad04be

git reset --HARD fad04be

Alternatively if you already pushed something to the remote or someone else, you can revert the reverts. If you do a git log, you will see the two most recent commits being the reverts of 845f8d5 and fad04be. You can revert those reverts, by doing

或者,如果您已经将某些内容推送到遥控器或其他人,则可以还原还原。如果你做一个 git log,你会看到最近的两个提交是 845f8d5 和 fad04be 的还原。您可以通过执行以下操作来还原这些还原

git revert <commit-id-of-revert>

Next time you want to see an old revision, indeed use:

下次您想查看旧版本时,确实使用:

git checkout <revision-id>

Don't forget to do a

不要忘记做一个

git checkout master

when you're done. If you change and commit stuff without doing this, your commits will go somewhere but not in the master branch (git will complain about being in a detached head state, but this warning is easy to miss -- trust me, I've been there :))

当你完成时。如果你在不这样做的情况下更改和提交内容,你的提交将在某个地方而不是在 master 分支中(git 会抱怨处于分离的头部状态,但这个警告很容易错过——相信我,我去过那里:))

Too see an old revision, there is no need to create another branch first.

太看旧版本了,没必要先创建另一个分支。

回答by Nick Martin

A git revert is actually a commit that just undoes the prior commit. I take it the pull didn't do anything? You should only need to push the revert.

git revert 实际上是一个提交,它只是撤销先前的提交。我认为拉没有做任何事情?您应该只需要推动恢复。

回答by ouah

Use git checkout <commit-id>to take a quick look to a commit. git revert <commit-id>will revert the single commit <commit-id>by adding a new commit.

用于git checkout <commit-id>快速查看提交。git revert <commit-id><commit-id>通过添加新提交来恢复单个提交。