简体   繁体   English

硬推到远程后,Git重置本地存储库

[英]Git Reset local repository after hard push to remote

We have a continuous integration build process pointed at a source git repository. 我们有一个针对源git存储库的持续集成构建过程。 When the process runs the CI process pushes its output into a releases repository. 当流程运行时,CI流程会将其输出推送到发布存储库中。 My question revolves around the following scenario. 我的问题与以下情况有关。

  1. Our CI server builds from source and pushes the results to our releases repository. 我们的CI服务器从源构建,并将结果推送到我们的发布存储库。
  2. A developer checks out the releases repository, adds a schema change script (that isn't added by our CI process), then amends the previous commit and does a force push operation (in order to not add another commit as a result of just adding a script). 开发人员签出发行版本存储库,添加架构更改脚本(我们的CI流程未添加),然后修改先前的提交并执行强制推送操作(以不因添加而导致添加其他提交)脚本)。
  3. The next time the CI process runs and tries to push its output we get the following error "Updates were rejected because the tip of your current branch is behind its remote counterpart." 下次CI进程运行并尝试推送其输出时,我们将收到以下错误“由于当前分支的尖端位于其远程对应的后面,因此更新被拒绝。”

What I would like to do is tell the CI server to reset the local repository match exactly the remote repo. 我想做的是告诉CI服务器将本地存储库重置为与远程仓库完全匹配。 I've looked at several similar problems and they all say to run the following commands (or very similar). 我看过几个类似的问题,它们都说要运行以下命令(或非常类似的命令)。

  1. git fetch --all
  2. git reset --hard [remote repository]

Even after running those commands prior to the build process, I still get the same error. 即使在构建过程之前运行了这些命令,我​​仍然会遇到相同的错误。 Any ideas on what I should do? 关于我应该做什么的任何想法? Or if what I want to do is even possible. 或者,如果我想做的事情甚至是可能的。

So it seems that I was potentially resetting the wrong repository. 因此,似乎我可能正在重置错误的存储库。 I have changed the commands to do the following (with the last step potentially being unnecessary) 我已更改命令以执行以下操作(最后一步可能是不必要的)

git fetch --all 
git reset --hard @{u}
git pull

The @{u} tells git to reset whatever branch is currently checked out, thus eliminating the need to specify the branch manually (which I must have been doing wrong). @{u}告诉git重设当前已检出的任何分支,从而无需手动指定分支(我一定做错了)。

One additional item of interest. 另一项感兴趣的项目。 If you are running the commands in a power shell script (how our CI server runs these actions) you have to write the git reset as follows (as the curly braces cause parse errors). 如果您正在Power Shell脚本中运行命令(我们的CI服务器如何运行这些操作),则必须编写git reset,如下所示(因为花括号会导致解析错误)。

git reset --hard "@{u}"

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

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