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
Want to change my master to an older commit, how can I do this?
提问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_id
commit to origin
as the new head of origin
's master
branch.
这会做什么?它会将$old_commit_id
提交推送到的分支origin
的新负责人。origin
master
If that is what you wanted, you do not need to touch your local master
branch 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 origin
to 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 master
and create a branch that points to where master
currently 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 master
branch to move backward one commit:
既然您已经在master
,我们将告诉master
分支向后移动一次提交:
git reset master~1
Now, master
should be moved back one space, but the pointer
branch is still on the most recent commit :
现在,master
应该向后移动一个空格,但pointer
分支仍然在最近的提交上:
| (A) ---------> (B) ----------> (C)
| ^ ^
| (HEAD, master) (pointer)
At this point, you can push master
to a remote, or where ever, then fast forward merge it back up to the pointer
branch. You can kill the pointer
branch 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 master
to 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