Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

git - 如何正确强制执行Git推送?(How do I properly force a Git push?)

I've set up a remote non-bare "main" repo and cloned it to my computer.

(我已经建立了一个远程的非裸露的“主”仓库,并将其克隆到我的计算机上。)

I made some local changes, updated my local repository, and pushed the changes back to my remote repo.

(我进行了一些本地更改,更新了本地存储库,然后将更改推回到远程仓库中。)

Things were fine up to that point.

(到那时为止一切都很好。)

Now, I had to change something in the remote repo.

(现在,我不得不更改远程仓库中的某些内容。)

Then I changed something in my local repo.

(然后,我更改了本地存储区中的某些内容。)

I realized that the change to the remote repo was not needed.

(我意识到不需要更改远程仓库。)

So I tried to git push from my local repo to my remote repo, but I got an error like:

(所以我尝试将本地存储库中的git push送到远程存储库中,但是出现类似以下错误:)

To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again.

(为了防止丢失历史记录,拒绝了非快进更新,请在合并之前合并远程更改。)

See the 'Note about fast-forwards' section of git push --help for details.

(有关详细信息,请参见git push --help的“关于快进的说明”部分。)

I thought that probably a

(我以为可能)

git push --force

would force my local copy to push changes to the remote one and make it the same.

(会强制我的本地副本将更改推送到远程副本,并使其相同。)

It does force the update , but when I go back to the remote repo and make a commit, I notice that the files contain outdated changes (ones that the main remote repo previously had).

(它确实强制执行更新 ,但是当我返回到远程存储库并进行提交时,我注意到文件包含了过时的更改(主要远程存储库以前具有的更改)。)

As I mentioned in the comments to one of the answers :

(正如我在对答案之一评论中提到 :)

[I] tried forcing, but when going back to master server to save the changes, i get outdated staging.

([我]尝试强制执行,但是当回到主服务器以保存更改时,我得到了过时的暂存。)

Thus, when i commit the repositories are not the same.

(因此,当我提交存储库时,它们是不一样的。)

And when i try to use git push again, i get the same error.

(当我再次尝试使用git push时,出现相同的错误。)

How can I fix this issue?

(如何解决此问题?)

  ask by Spyros translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just do:

(做就是了:)

git push origin <your_branch_name> --force

or if you have a specific repo:

(或者,如果您有特定的仓库:)

git push https://git.... --force

This will delete your previous commit(s) and push your current one.

(这将删除您以前的提交并推送您当前的提交。)

It may not be proper, but if anyone stumbles upon this page, thought they might want a simple solution...

(可能不合适,但是如果有人偶然发现此页面,以为他们可能想要一个简单的解决方案...)

Short flag (短旗)

Also note that -f is short for --force , so

(另请注意, -f--force缩写,因此)

git push origin <your_branch_name> -f

will also work.

(也可以。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...