如何使用Git Pull覆盖本地文件

当需要放弃所有本地更改并用远程分支的副本重置所有内容时,可以使用Git Pull覆盖本地文件。此操作会丢失所有未推送的本地提交,但未被Git跟踪的文件不受影响。覆盖工作流程包括从远程获取最新版本,然后使用`git reset --hard`将本地分支重置为远程状态。注意,可以创建分支来保留当前的本地提交。

什么时候需要覆盖本地文件? (When do you need to overwrite local files?)

If you feel the need to discard all your local changes and just reset/overwrite everything with a copy from the remote branch, then you should follow this guide.

如果您需要放弃所有本地更改,而只是使用远程分支的副本重置/覆盖所有内容,则应遵循本指南。

Important: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven’t been pushed will be lost.

重要提示:如果您进行任何本地更改,它们将会丢失。 无论是否使用--hard选项,所有尚未推送的本地提交都将丢失。

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.

如果您有Git不能跟踪的任何文件(例如,上载的用户内容),这些文件将不会受到影响。

覆盖工作流程: (The Overwrite workflow:)

To overwrite your local files do:

要覆盖本地文件,请执行以下操作:

git fetch --all
git reset --hard <remote>/<branch_name>

For example:

例如:

git fetch --all
git reset --hard origin/master

这个怎么运作: (How it works:)

git fetch downloads the latest from remote without trying to merge or rebase anything.

git fetch从远程下载最新版本,而无需尝试合并或重新设置任何内容。

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master.

然后git reset将master分支重置为您刚获取的分支。 --hard选项更改工作树中的所有文件,以匹配origin/master中的文件。

附加信息: (Additional Information:)

It’s worth noting that it is possible to maintain current local commits by creating a branch from master or whichever branch you want to work on before resetting:

值得注意的是,可以通过在重置之前从master或您要处理的任何分支创建分支来维护当前的本地提交:

For Example:

例如:

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

After this, all of the old commits will be kept in new-branch-to-save-current-commits. Uncommitted changes however (even staged), will be lost. Make sure to stash and commit anything you need.

在此之后,所有旧提交都将保存在new-branch-to-save-current-commits 。 但是,未提交的更改(即使已分阶段)将丢失。 确保隐藏并提交您需要的任何东西。

归因: (Attribution:)

This article is based on a Stack Overflow question <a href=’http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull/8888015#8888015’ target=’blank’ rel=‘nofollow’>here_

本文基于堆栈溢出问题<a href =' http ://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull/8888015#8888015'target ='空白'rel ='nofollow'>此处_

翻译自: https://www.freecodecamp.org/news/how-to-override-local-files-with-git-pull/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值