简体   繁体   English

Git推送到远程存储库,丢弃最新提交并替换为本地提交

[英]Git push to a remote repository, discard the latest commit and replace with local commit

I have done this: 我已经做到了:

  • Forked a repository on Github, made some commits there and pushed, but I ran into troubles. 在Github上创建了一个存储库,在其中做了一些提交并推送,但是我遇到了麻烦。

  • I cloned the original repository (not the fork) and did a commit locally. 我克隆了原始存储库(不是分支),并在本地进行了提交。

Now I want to push the local commit to my forked repository in order make a pull request. 现在,我想将本地提交推送到我的派生存储库中,以发出请求请求。

So I set the origin of the cloned repository to my forked remote. 因此,我将克隆存储库的来源设置为我的分叉远程服务器。

git remote set-url origin https://github.com/MY-USERNAME/REPOSITORY.git

Now I want to make my local repository to completely overwrite the remote. 现在,我要使本地存储库完全覆盖远程数据库。 What is the correct way of doing this? 正确的做法是什么? git push --force ? git push --force吗?

The issue is that git can only have one branch named master . 问题是git只能有一个名为master分支。 If you fork a repo, add a new commit, if someone else also creates a new commit, and then you push it, you will have an error. 如果您分叉一个仓库,请添加一个新的提交,如果其他人也创建了一个新的提交,然后再推送它,则会出现错误。

If the goal is to make a pull request, create a new branch with a new name, and push this new branch. 如果目标是发出拉取请求,请使用新名称创建一个新分支,然后推送该新分支。 Go on the web server and create a full request between your new branch and master. 在Web服务器上,在新分支和主服务器之间创建完整请求。

Don't push --force unless you really want to remove the commits online. 除非您真的想在线删除提交,否则不要push --force Even if it's your fork, this commit is part of the original repo, there is no reason to remove it. 即使是您的fork,此提交也是原始存储库的一部分,没有理由将其删除。

The proper way to go is to create a properly-named feature branch and do a pull request. 正确的做法是创建一个命名正确的功能分支并执行拉取请求。

If I understand you correctly, you have a remote on github, which you want to discard entirely. 如果我对您的理解正确,那么您将在github上拥有一个remote服务器,而您希望将其完全丢弃。

If you do not care about losing remote's history 如果您不关心丢失遥控器的历史记录
Simply delete the repository on github, fork it again, and push your changes to this clean fork remote. 只需删除github上的存储库,再次对其进行分叉,然后将您的更改推送到该干净的fork远程即可。 This way you will not struggle with all the traps git push --force usually brings with it. 这样,您将不会遇到git push --force通常带来的所有陷阱。

If you do care about losing remote's history 如果您确实担心丢失遥控器的历史记录
Pull from your bad-state remote and then create a new commit which contains the good state. 从状态不良的remote拉出,然后创建一个包含状态良好的新提交。 Since this newest commit overwrites all (changed) files, it should work equivalently to not having the bad history around. 由于此最新提交会覆盖所有(更改的)文件,因此它应该等效于不存在不良历史记录。

The problem with git push --force is, that you might change history in such a way that your fork does no longer correspond to the original repository. git push --force的问题是,您可能会更改历史记录,以使您的fork不再与原始存储库相对应。 It's usually better to find a way without force push. 通常最好找到一种无需强制推动的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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