git 想将我的主人更改为较旧的提交,我该怎么做?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4359681/
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-10 09:36:16  来源:igfitidea点击:

Want to change my master to an older commit, how can I do this?

git

提问by Blankman

I want to rollback to a previous commit, and then publish that code, then go back to the latest commit.

我想回滚到以前的提交,然后发布该代码,然后返回到最新的提交。

i.e. so my master is pointing to an older commit version just so I can pulish that version, then I want to go back to the latest commit I was one initially.

即所以我的主人指向一个旧的提交版本,这样我就可以发布那个版本,然后我想回到我最初的最新提交。

How can I do this?

我怎样才能做到这一点?

回答by Tyler Brock

If you want to do this and revert the master to the previous commit:

如果要执行此操作并将 master 恢复到之前的提交:

git checkout master~1            # Checkout previous commit on master
git checkout -b new_master       # Create branch for new master
git branch -D master             # Delete old master
git branch -mv new_master master # Make new_master master

Alternatively:

或者:

git reset --hard master~1        # Reset current branch to one commit ago on master

回答by Aristotle Pagaltzis

Your question is unclear. I thinkwhat you are asking for is this:

你的问题不清楚。我你要问的是:

git push -f origin $old_commit_id:master

What will this do? It will push the $old_commit_idcommit to originas the new head of origin's masterbranch.

这会做什么?它会将$old_commit_id提交推送到的分支origin的新负责人。originmaster

If that is what you wanted, you do not need to touch your local masterbranch at all.

如果这就是您想要的,您根本不需要联系您当地的master分支机构。

回答by KawaiKx

use git reset --hard <old commit number>

git reset --hard <old commit number>

it will reset the HEAD to this old commit.

它会将 HEAD 重置为这个旧提交。

additionally, you need to use git push -f originto alter the remote repo too.

此外,您还需要使用git push -f origin来更改远程存储库。

回答by bronson

If you want to avoid force pushing, here's how to revert your repo to an older commit and preserve all intervening work:

如果你想避免强制推送,这里是如何将你的 repo 恢复到较旧的提交并保留所有干预工作:

git checkout 307a5cd        # check out the commit that you want to reset to 
git checkout -b fixy        # create a branch named fixy to do the work
git merge -s ours master    # merge master's history without changing any files
git checkout master         # switch back to master
git merge fixy              # and merge in the fixed branch
git push                    # done, no need to force push!

Done! Replace 307a5cd with whatever commit you want in your repo.

完毕!将 307a5cd 替换为您想要在存储库中的任何提交。

(I know the first two lines can be combined, but I think that makes it less clear what's going on)

(我知道前两行可以合并,但我认为这使得不太清楚发生了什么)

Here it is graphically:

这是图形化的:

c1 -- c2 -- c3 -- c4 -- c2' -- c5 ...
        \              /
         '------------'

You effectively remove c3 and c4 and set your project back to c2. However, c3 and c4 are still available in your project's history if you ever want to see them again.

您有效地删除了 c3 和 c4,并将您的项目设置回 c2。但是,如果您想再次查看 c3 和 c4,您的项目历史记录中仍然可以使用它们。

回答by C0M37

Assuming a commit graph like so:

假设像这样的提交图:

| (A) ---------> (B) ----------> (C)
|                                 ^
|                              (master)

You want to first checkout masterand create a branch that points to where mastercurrently is:

您想先结帐master并创建一个指向master当前位置的分支:

git checkout master
git branch pointer master

Should look like this now:

现在应该是这样的:

| (A) ---------> (B) ----------> (C)
|                                 ^
|                       (HEAD, master, pointer)

Now that you're already on master, we'll tell the masterbranch to move backward one commit:

既然您已经在master,我们将告诉master分支向后移动一次提交:

git reset master~1

Now, mastershould be moved back one space, but the pointerbranch is still on the most recent commit :

现在,master应该向后移动一个空格,但pointer分支仍然在最近的提交上:

| (A) ---------> (B) ----------> (C)
|                 ^               ^
|           (HEAD, master)    (pointer)

At this point, you can push masterto a remote, or where ever, then fast forward merge it back up to the pointerbranch. You can kill the pointerbranch at that point :

此时,您可以推master送到远程或任何地方,然后快进将其合并回pointer分支。您可以pointer在那时杀死分支:

git push origin master
git merge --ff-only pointer
git branch -D pointer

Final :

最终的 :

| (A) ---------> (B) ----------> (C)
|                 ^               ^
|         [ origin/master ]    (HEAD, master)

回答by jtdubs

You can just git checkout <commit-id>, do whatever you need to do, then git checkout masterto get back to the new code.

你可以git checkout <commit-id>做任何你需要做的事情,然后git checkout master回到新的代码。

If you actually need to modify the old code to release it, then you should probably:

如果您确实需要修改旧代码以发布它,那么您可能应该:

git checkout -b my_release <commit-id>
... prepare code for release ...
... release code ...
git checkout master
git merge my_release

Also, I can't recommend git flowenough. It makes all of this quite easy.

另外,我不能充分推荐git flow。它使这一切变得非常容易。

回答by Pablo Fernandez

To move to a previous version:

要移动到以前的版本:

git checkout <version hash>

git checkout <version hash>

do your work here and commit it with

在这里做你的工作并提交

git commit --amend

git commit --amend

To go back to master:

回到主人

git checkout master

git checkout master